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