Skip to content

MetaPipes

okram edited this page Sep 14, 2010 · 15 revisions

A metapipe is a pipe that “wraps” another pipe. MetaPipes are useful in that they can make decisions based upon the behavior of the pipe(s) they wrap. There are numerous metapipes and this section will discuss a few of the more interesting cases. Moreover, by understanding the cases in which these pipes are used, its possible to create metapipes when such situations make themselves apparent.

  1. Pipelines
  2. And/Or Pipes

Pipelines

A Pipeline is a commonly used pipe. A Pipeline<S,E> implements Pipe<S,E> and as such, a Pipeline is simply a Pipe. A Pipeline takes an ordered list of pipes in its constructor. It connects these pipes, whereby the input of pipe n is connected to the output of pipe n-1. Note that the output type of pipe n-1 must be the same type as the input to pipe n. This idea is elucidated in the diagram above.

The benefit of using a Pipeline is that is greatly reduces the complexity of a process and allows for easy reuse. It is analogous, in many ways, to creating a function to wrap a body of code/instructions. As such, the function serves as a blackbox with input and output.

And/Or Pipes

There are two logical FilterPipe pipes called AndFilterPipe<S> and OrFilterPipe<S>. These pipes take an object of type S and emit an object of type S. However, they only emit the S object if the collection of Pipe<S,Boolean> pipes that they wrap return true. For AndFilterPipe to emit its incoming S object, all of its wrapped pipes must return true that S object. For the OrFilterPipe, only one of its wrapped pipes must return true.

Pipe<Integer,Integer> pipeA = new ObjectFilterPipe<Integer>(1, ComparisonFilterPipe.EQUALS);
Pipe<Integer,Integer> pipeB = new ObjectFilterPipe<Integer>(10, ComparisonFilterPipe.EQUALS);
Pipe<Integer,Integer> pipeC = new ObjectFilterPipe<Integer>(20, ComparisonFilterPipe.EQUALS);
Pipe<Integer,Integer> pipe1 = new OrFilterPipe<Integer>(new HasNextPipe<Integer>(pipeA), new HasNextPipe<Integer>(pipeB), new HasNextPipe<Integer>(pipeC));
pipe1.setStarts(Arrays.asList(1,22,10,136,7,2));
while(pipe1.hasNext()) {
  System.out.print(pipe1.next() + "...");
}

The System.out of the previous code is as follows.

1...10...
Clone this wiki locally