| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.apache.commons.functor.generator; |
| 18 | |
|
| 19 | |
import org.apache.commons.functor.UnaryPredicate; |
| 20 | |
import org.apache.commons.functor.UnaryProcedure; |
| 21 | |
import org.apache.commons.functor.core.composite.ConditionalUnaryProcedure; |
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
public class FilteredGenerator<E> extends BaseGenerator<E> { |
| 30 | |
private final UnaryPredicate<? super E> pred; |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
public FilteredGenerator(Generator<? extends E> wrapped, UnaryPredicate<? super E> pred) { |
| 38 | 28 | super(wrapped); |
| 39 | 28 | if (wrapped == null) { |
| 40 | 0 | throw new IllegalArgumentException("Generator argument was null"); |
| 41 | |
} |
| 42 | 28 | if (pred == null) { |
| 43 | 0 | throw new IllegalArgumentException("UnaryPredicate argument was null"); |
| 44 | |
} |
| 45 | 28 | this.pred = pred; |
| 46 | 28 | } |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
public void run(UnaryProcedure<? super E> proc) { |
| 52 | 28 | getWrappedGenerator().run(new ConditionalUnaryProcedure<E>(pred, proc)); |
| 53 | 28 | } |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
@SuppressWarnings("unchecked") |
| 59 | |
@Override |
| 60 | |
protected Generator<? extends E> getWrappedGenerator() { |
| 61 | 28 | return (Generator<? extends E>) super.getWrappedGenerator(); |
| 62 | |
} |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
public boolean equals(Object obj) { |
| 68 | 0 | if (obj == this) { |
| 69 | 0 | return true; |
| 70 | |
} |
| 71 | 0 | if (!(obj instanceof FilteredGenerator<?>)) { |
| 72 | 0 | return false; |
| 73 | |
} |
| 74 | 0 | FilteredGenerator<?> other = (FilteredGenerator<?>) obj; |
| 75 | 0 | return other.getWrappedGenerator().equals(getWrappedGenerator()) && other.pred.equals(pred); |
| 76 | |
} |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
public int hashCode() { |
| 82 | 0 | int result = "FilteredGenerator".hashCode(); |
| 83 | 0 | result <<= 2; |
| 84 | 0 | Generator<?> gen = getWrappedGenerator(); |
| 85 | 0 | result ^= gen == null ? 0 : gen.hashCode(); |
| 86 | 0 | result <<= 2; |
| 87 | 0 | result ^= pred == null ? 0 : pred.hashCode(); |
| 88 | 0 | return result; |
| 89 | |
} |
| 90 | |
} |