-
Notifications
You must be signed in to change notification settings - Fork 55
Basic Pipes

A Pipe<S,E> is a Java interface that extends both the Iterable<E> and Iterator<E> interface. A Pipe<S,E> takes, as input, an iterator or iterable yielding objects of type S and produces/emits objects of type E. The character “S” stands for “starts” and the character “E” stands for “ends”.
Here is a simple example demonstrating a single pipe that capitalizes the characters of the strings that come into it.
Pipe<String,String> capsPipe = new CapitalizePipe();
capsPipe.setStarts(Arrays.asList("this", "is", "the", "end."));
while(capsPipe.hasNext()) {
System.out.print(capPipe.next() + " ");
}This pipe will produce the following output.
THIS IS THE END.Given that Pipe<S,E> extends Iterator<E> and Iterable<E>, its possible to string together pipes to create a processing line.
Pipe<String,String> capsPipe = new CapitalizePipe();
Pipe<String,Integer> countPipe = new CountPipe();
capsPipe.setStarts(Arrays.asList("this", "is", "the", "end."));
countPipe.setStarts(capsPipe);
while(countPipe.hasNext()) {
System.out.print(countPipe.next() + " ");
}If CountPipe takes a String and emits the number of characters in that String, then the previous code will yield the following output.
4 2 3 4Realize that the output of one pipe must be of the same type as the input of the next pipe. Given that Pipe<S,E> extends Iterator<E>, The E of the first pipe becomes the S of the second pipe. In order to make it easier to create chains of pipes, there is a handy Pipeline<S,E> class. This class implements Pipe<S,E> and thus, Pipeline<S,E> objects can be combined like any other pipe (i.e. you can create pipelines of pipelines). Here is an example using a Pipeline<S,E> object.
Pipe<String,String> capsPipe = new CapitalizePipe();
Pipe<String,Integer> countPipe = new CountPipe();
Pipe<Integer,String> wordPipe = new WordPipe();
Pipeline<String,String> pipeline = new Pipeline<String,String>(capsPipe, countPipe, wordPipe);
pipeline.setStarts(Arrays.asList("this", "is", "the", "end."));
while(pipeline.hasNext()) {
System.out.print(pipeline.next() + " ");
}Assuming that WordPipe emits the word version of an incoming integer, the pipeline will produce the following output.
four two three fourThat’s Pipes in a nutshell.