|
| 1 | +!https://github.com/tinkerpop/pipes/raw/master/doc/images/duck-plumber.jpg! |
| 2 | + |
| 3 | +A common Pipes pattern is the filtering of objects. A filter-based pipe will consume objects and either emit them or not. If they are not emitted, then they are considered filtered by the pipe. A useful interface to implement that describes this behavior is @FilterPipe<S> extends Pipe<S,S>@. |
| 4 | + |
| 5 | +h2. Generic Filter Pipe |
| 6 | + |
| 7 | +The generic filter pipe is @FilterFunctionPipe@. A @FilterFunctionPipe@ takes a @PipeFunction@ (see [[Pipe Types]]) that computes on @S@ and either emits it or not. An example @PipeFunction@ is provided below: |
| 8 | + |
| 9 | +```java |
| 10 | +public class CharCountPipeFunction implements PipeFunction<String,Boolean> { |
| 11 | + |
| 12 | + private final int number; |
| 13 | + |
| 14 | + public CharCountPipeFunction(int number) { |
| 15 | + this.number = number; |
| 16 | + } |
| 17 | + |
| 18 | + public Boolean compute(String argument) { |
| 19 | + return argument.length() == this.number; |
| 20 | + } |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +When put in the context of a @FilterFunctionPipe@, the code looks as follows: |
| 25 | + |
| 26 | +```java |
| 27 | +Pipe<String, String> pipe = new FilterFunctionPipe<String>(new CharCountPipeFunction(4)); |
| 28 | +pipe.setStarts(Arrays.asList("tell", "me", "your", "name")); |
| 29 | +// the results of the iteration are: "tell", "your", "name" |
| 30 | +``` |
| 31 | + |
| 32 | +h2. Basic Filtering |
| 33 | + |
| 34 | +The @RandomFilterPipe@ comes with the Pipes distribution. @RandomFilterPipe@ will only allow a consumed object to be emitted if a biased coin toss lands on "heads." At the extremes, if @bias@ is 0.0 then no incoming objects are emitted and if @bias@ is 1.0, then every incoming object is emitted. |
| 35 | + |
| 36 | +```java |
| 37 | +public class RandomFilterPipe<S> extends AbstractPipe<S, S> implements FilterPipe<S> { |
| 38 | + private static final Random RANDOM = new Random(); |
| 39 | + private final double bias; |
| 40 | + public SampleFilterPipe(final double bias) { |
| 41 | + this.bias = bias; |
| 42 | + } |
| 43 | + protected S processNextStart() { |
| 44 | + while (this.starts.hasNext()) { |
| 45 | + S s = this.starts.next(); |
| 46 | + if (bias >= RANDOM.nextDouble()) { |
| 47 | + return s; |
| 48 | + } |
| 49 | + } |
| 50 | + throw new NoSuchElementException(); |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +The @processNextStart()@ method structure above is a common pattern in filter-based pipes. In short, a @while(this.starts.hasNext())@ is evaluated until an incoming start object meets the criteria and is returned (i.e. emitted). If there are no more starts and the criteria is not met, then a @NoSuchElementException@ is thrown. Beware of using recursion to accomplish a similar behavior. As the amount of data grows that is streaming through, recursion-based methods can easily yield a @StackOverflowError@. |
| 56 | + |
| 57 | +h2. Comparison-Based Filtering |
| 58 | + |
| 59 | +More complicated filters can be created than what was presented above. These filters usually check objects to determine whether to emit them or not. There is an interface called @ComparisonFilterPipe<S,T>@. Implementations of this interface consume objects of type @S@. If a check of that object passes (i.e. return @true@), then the @S@ object is emitted, else it is filtered. @ComparisonFilterPipe@ has the following signature: |
| 60 | + |
| 61 | +```java |
| 62 | +public interface ComparisonFilterPipe<S, T> extends FilterPipe<S> { |
| 63 | + public enum Filter { |
| 64 | + EQUAL, NOT_EQUAL, GREATER_THAN, LESS_THAN, GREATER_THAN_EQUAL, LESS_THAN_EQUAL |
| 65 | + } |
| 66 | + public boolean compareObjects(T leftObject, T rightObject); |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +The important method is @compareObjects()@. This method returns @true@ if the left hand object is @EQUAL@, @NOT_EQUAL@, etc. to the right hand object. |
| 71 | + |
| 72 | +Next, there is an abstract class called @AbstractComparisonFilterPipe<S,T>@ that implements @ComparisonFilterPipe@. More specifically, it provides a standard implementation of @ComparisonFilterPipe.compareObjects()@. For most situations, this method is sufficient. However, if not, simply override the method with an implementation that meets the requirements of the designed pipe. |
| 73 | + |
| 74 | +```java |
| 75 | +public boolean compareObjects(final T leftObject, final T rightObject) { |
| 76 | + switch (this.filter) { |
| 77 | + case EQUAL: |
| 78 | + if (null == leftObject) |
| 79 | + return rightObject == null; |
| 80 | + return leftObject.equals(rightObject); |
| 81 | + case NOT_EQUAL: |
| 82 | + if (null == leftObject) |
| 83 | + return rightObject != null; |
| 84 | + return !leftObject.equals(rightObject); |
| 85 | + case GREATER_THAN: |
| 86 | + if (null == leftObject || rightObject == null) |
| 87 | + return false; |
| 88 | + return ((Comparable) leftObject).compareTo(rightObject) == 1; |
| 89 | + case LESS_THAN: |
| 90 | + if (null == leftObject || rightObject == null) |
| 91 | + return false; |
| 92 | + return ((Comparable) leftObject).compareTo(rightObject) == -1; |
| 93 | + case GREATER_THAN_EQUAL: |
| 94 | + if (null == leftObject || rightObject == null) |
| 95 | + return false; |
| 96 | + return ((Comparable) leftObject).compareTo(rightObject) >= 0; |
| 97 | + case LESS_THAN_EQUAL: |
| 98 | + if (null == leftObject || rightObject == null) |
| 99 | + return false; |
| 100 | + return ((Comparable) leftObject).compareTo(rightObject) <= 0; |
| 101 | + default: |
| 102 | + throw new RuntimeException("Invalid state as no valid filter was provided"); |
| 103 | + } |
| 104 | + } |
| 105 | +``` |
| 106 | + |
| 107 | +Note: it many situations, such as @ObjectFilterPipe@, the right hand object to allow or disallow is stored in the pipe and compared with each object passed through it. |
| 108 | + |
| 109 | +!http://github.com/tinkerpop/pipes/raw/master/doc/images/filter-example.png! |
0 commit comments