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