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