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