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
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 public class CompositeUnaryFunction<A, T> implements UnaryFunction<A, T>, Serializable {
52
53
54
55
56 private static final long serialVersionUID = 4945193629275757281L;
57
58
59 private static final int HASH_SHIFT = 4;
60
61
62
63
64
65
66
67 private static class Helper<X, A, T> implements UnaryFunction<A, T>, Serializable {
68
69
70
71 private static final long serialVersionUID = 8167255331321876718L;
72 private UnaryFunction<? super X, ? extends T> following;
73 private UnaryFunction<? super A, ? extends X> preceding;
74
75
76
77
78
79
80 public Helper(UnaryFunction<? super X, ? extends T> following,
81 UnaryFunction<? super A, ? extends X> preceding) {
82 this.following = following;
83 this.preceding = preceding;
84 }
85
86
87
88
89 public T evaluate(A obj) {
90 return following.evaluate(preceding.evaluate(obj));
91 }
92
93
94
95
96 @Override
97 public boolean equals(Object obj) {
98 return obj == this || obj instanceof Helper<?, ?, ?> && equals((Helper<?, ?, ?>) obj);
99 }
100
101 private boolean equals(Helper<?, ?, ?> helper) {
102 return helper.following.equals(following) && helper.preceding.equals(preceding);
103 }
104
105
106
107
108 @Override
109 public int hashCode() {
110 int result = "CompositeUnaryFunction$Helper".hashCode();
111 result <<= 2;
112 result |= following.hashCode();
113 result <<= 2;
114 result |= preceding.hashCode();
115 return result;
116 }
117
118
119
120
121 @Override
122 public String toString() {
123 return following.toString() + " of " + preceding.toString();
124 }
125 }
126
127 private final UnaryFunction<? super A, ? extends T> function;
128
129
130
131
132
133 public CompositeUnaryFunction(UnaryFunction<? super A, ? extends T> function) {
134 if (function == null) {
135 throw new IllegalArgumentException("function must not be null");
136 }
137 this.function = function;
138 }
139
140 private <X> CompositeUnaryFunction(UnaryFunction<? super X, ? extends T> following,
141 UnaryFunction<? super A, ? extends X> preceding) {
142 this.function = new Helper<X, A, T>(following, preceding);
143 }
144
145
146
147
148 public final T evaluate(A obj) {
149 return function.evaluate(obj);
150 }
151
152
153
154
155
156
157
158 public final <P> CompositeUnaryFunction<P, T> of(UnaryFunction<? super P, ? extends A> preceding) {
159 if (preceding == null) {
160 throw new IllegalArgumentException("preceding function was null");
161 }
162 return new CompositeUnaryFunction<P, T>(function, preceding);
163 }
164
165
166
167
168 public final boolean equals(Object that) {
169 return that == this
170 || (that instanceof CompositeUnaryFunction<?, ?> && equals((CompositeUnaryFunction<?, ?>) that));
171 }
172
173
174
175
176
177
178 public final boolean equals(CompositeUnaryFunction<?, ?> that) {
179
180 return null != that && function.equals(that.function);
181 }
182
183
184
185
186 public int hashCode() {
187
188 return ("CompositeUnaryFunction".hashCode() << HASH_SHIFT) ^ function.hashCode();
189 }
190
191
192
193
194 public String toString() {
195 return "CompositeUnaryFunction<" + function + ">";
196 }
197
198 }