| 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 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | 8 | public class WhileGenerate<E> extends BaseGenerator<E> { |
| 29 | |
private final UnaryPredicate<? super E> test; |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
public WhileGenerate(UnaryPredicate<? super E> test, Generator<? extends E> wrapped) { |
| 37 | 2 | super(wrapped); |
| 38 | 2 | if (wrapped == null) { |
| 39 | 0 | throw new IllegalArgumentException("Generator argument was null"); |
| 40 | |
} |
| 41 | 2 | if (test == null) { |
| 42 | 0 | throw new IllegalArgumentException("UnaryPredicate argument was null"); |
| 43 | |
} |
| 44 | 2 | this.test = test; |
| 45 | 2 | } |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
public void run(final UnaryProcedure<? super E> proc) { |
| 51 | 2 | getWrappedGenerator().run(new UnaryProcedure<E>() { |
| 52 | |
public void run(E obj) { |
| 53 | 8 | if (!test.test(obj)) { |
| 54 | 2 | getWrappedGenerator().stop(); |
| 55 | |
} else { |
| 56 | 6 | proc.run(obj); |
| 57 | |
} |
| 58 | 8 | } |
| 59 | |
}); |
| 60 | 2 | } |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
@SuppressWarnings("unchecked") |
| 66 | |
@Override |
| 67 | |
protected Generator<? extends E> getWrappedGenerator() { |
| 68 | 4 | return (Generator<? extends E>) super.getWrappedGenerator(); |
| 69 | |
} |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
public boolean equals(Object obj) { |
| 75 | 0 | if (obj == this) { |
| 76 | 0 | return true; |
| 77 | |
} |
| 78 | 0 | if (!(obj instanceof WhileGenerate<?>)) { |
| 79 | 0 | return false; |
| 80 | |
} |
| 81 | 0 | WhileGenerate<?> other = (WhileGenerate<?>) obj; |
| 82 | 0 | return other.getWrappedGenerator().equals(getWrappedGenerator()) && other.test.equals(test); |
| 83 | |
} |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
public int hashCode() { |
| 89 | 0 | int result = "WhileGenerate".hashCode(); |
| 90 | 0 | result <<= 2; |
| 91 | 0 | Generator<?> gen = getWrappedGenerator(); |
| 92 | 0 | result ^= gen == null ? 0 : gen.hashCode(); |
| 93 | 0 | result <<= 2; |
| 94 | 0 | result ^= test == null ? 0 : test.hashCode(); |
| 95 | 0 | return result; |
| 96 | |
} |
| 97 | |
} |