1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.functor.core.composite;
18
19 import java.io.Serializable;
20
21 import org.apache.commons.functor.UnaryFunction;
22 import org.apache.commons.lang3.Validate;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public class CompositeUnaryFunction<A, T> implements UnaryFunction<A, T>, Serializable {
53
54
55
56
57 private static final long serialVersionUID = 4945193629275757281L;
58
59
60 private static final int HASH_SHIFT = 4;
61
62
63
64
65
66
67
68 private static class Helper<X, A, T> implements UnaryFunction<A, T>, Serializable {
69
70
71
72 private static final long serialVersionUID = 8167255331321876718L;
73
74
75
76 private UnaryFunction<? super X, ? extends T> following;
77
78
79
80 private UnaryFunction<? super A, ? extends X> preceding;
81
82
83
84
85
86
87 public Helper(UnaryFunction<? super X, ? extends T> following,
88 UnaryFunction<? super A, ? extends X> preceding) {
89 this.following = Validate.notNull(following, "UnaryFunction argument must not be null");
90 this.preceding = Validate.notNull(preceding, "UnaryFunction argument must not be null");
91 }
92
93
94
95
96 public T evaluate(A obj) {
97 return following.evaluate(preceding.evaluate(obj));
98 }
99
100
101
102
103 @Override
104 public boolean equals(Object obj) {
105 return obj == this || obj instanceof Helper<?, ?, ?> && equals((Helper<?, ?, ?>) obj);
106 }
107
108
109
110
111
112
113
114 private boolean equals(Helper<?, ?, ?> helper) {
115 return helper.following.equals(following) && helper.preceding.equals(preceding);
116 }
117
118
119
120
121 @Override
122 public int hashCode() {
123 int result = "CompositeUnaryFunction$Helper".hashCode();
124 result <<= 2;
125 result |= following.hashCode();
126 result <<= 2;
127 result |= preceding.hashCode();
128 return result;
129 }
130
131
132
133
134 @Override
135 public String toString() {
136 return following.toString() + " of " + preceding.toString();
137 }
138 }
139
140
141
142
143 private final UnaryFunction<? super A, ? extends T> function;
144
145
146
147
148
149 public CompositeUnaryFunction(UnaryFunction<? super A, ? extends T> function) {
150 this.function = Validate.notNull(function, "function must not be null");
151 }
152
153
154
155
156
157
158
159
160 private <X> CompositeUnaryFunction(UnaryFunction<? super X, ? extends T> following,
161 UnaryFunction<? super A, ? extends X> preceding) {
162 this.function = new Helper<X, A, T>(following, preceding);
163 }
164
165
166
167
168 public final T evaluate(A obj) {
169 return function.evaluate(obj);
170 }
171
172
173
174
175
176
177
178 public final <P> CompositeUnaryFunction<P, T> of(UnaryFunction<? super P, ? extends A> preceding) {
179 Validate.notNull(preceding, "preceding function was null");
180 return new CompositeUnaryFunction<P, T>(function, preceding);
181 }
182
183
184
185
186 @Override
187 public final boolean equals(Object that) {
188 return that == this
189 || (that instanceof CompositeUnaryFunction<?, ?> && equals((CompositeUnaryFunction<?, ?>) that));
190 }
191
192
193
194
195
196
197 public final boolean equals(CompositeUnaryFunction<?, ?> that) {
198
199 return null != that && function.equals(that.function);
200 }
201
202
203
204
205 @Override
206 public int hashCode() {
207
208 return ("CompositeUnaryFunction".hashCode() << HASH_SHIFT) ^ function.hashCode();
209 }
210
211
212
213
214 @Override
215 public String toString() {
216 return "CompositeUnaryFunction<" + function + ">";
217 }
218
219 }