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.BinaryFunction;
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 public class BinaryCompositeBinaryFunction<L, R, T> implements BinaryFunction<L, R, T>, Serializable {
43
44
45
46
47 private static final long serialVersionUID = 2570517284319064043L;
48
49
50 private static final int HASH_SHIFT = 4;
51
52
53
54
55
56
57
58 private static class Helper<G, H, L, R, T> implements BinaryFunction<L, R, T>, Serializable {
59
60
61
62 private static final long serialVersionUID = 6013646799505641592L;
63
64
65
66 private BinaryFunction<? super G, ? super H, ? extends T> f;
67
68
69
70 private BinaryFunction<? super L, ? super R, ? extends G> g;
71
72
73
74 private BinaryFunction<? super L, ? super R, ? extends H> h;
75
76
77
78
79
80
81
82 public Helper(BinaryFunction<? super G, ? super H, ? extends T> f,
83 BinaryFunction<? super L, ? super R, ? extends G> g,
84 BinaryFunction<? super L, ? super R, ? extends H> h) {
85 this.f = f;
86 this.g = g;
87 this.h = h;
88 }
89
90
91
92
93 public T evaluate(L left, R right) {
94 return f.evaluate(g.evaluate(left, right), h.evaluate(left, right));
95 }
96 }
97
98
99
100
101 private final Helper<?, ?, L, R, T> helper;
102
103
104
105
106
107
108
109
110
111
112
113
114 public <G, H> BinaryCompositeBinaryFunction(BinaryFunction<? super G, ? super H, ? extends T> f,
115 BinaryFunction<? super L, ? super R, ? extends G> g, BinaryFunction<? super L, ? super R, ? extends H> h) {
116 this.helper = new Helper<G, H, L, R, T>(
117 Validate.notNull(f, "final BinaryFunction argument must not be null"),
118 Validate.notNull(g, "left preceding BinaryFunction argument must not be null"),
119 Validate.notNull(h, "right preceding BinaryFunction argument must not be null")
120 );
121 }
122
123
124
125
126
127
128 public final T evaluate(L left, R right) {
129 return helper.evaluate(left, right);
130 }
131
132
133
134
135 @Override
136 public final boolean equals(Object that) {
137 return that == this || (that instanceof BinaryCompositeBinaryFunction<?, ?, ?>
138 && equals((BinaryCompositeBinaryFunction<?, ?, ?>) that));
139 }
140
141
142
143
144
145
146 public final boolean equals(BinaryCompositeBinaryFunction<?, ?, ?> that) {
147 return null != that
148 && helper.f.equals(that.helper.f)
149 && helper.g.equals(that.helper.g)
150 && helper.h.equals(that.helper.h);
151 }
152
153
154
155
156 @Override
157 public int hashCode() {
158 int hash = "BinaryCompositeBinaryFunction".hashCode();
159 hash <<= HASH_SHIFT;
160 hash ^= helper.f.hashCode();
161 hash <<= HASH_SHIFT;
162 hash ^= helper.g.hashCode();
163 hash <<= HASH_SHIFT;
164 hash ^= helper.h.hashCode();
165 return hash;
166 }
167
168
169
170
171 @Override
172 public String toString() {
173 return "BinaryCompositeBinaryFunction<" + helper.f + ";" + helper.g + ";" + helper.h + ">";
174 }
175
176 }