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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 public class BinaryCompositeBinaryFunction<L, R, T> implements BinaryFunction<L, R, T>, Serializable {
40
41
42
43
44 private static final long serialVersionUID = 2570517284319064043L;
45
46
47 private static final int HASH_SHIFT = 4;
48
49
50
51
52
53
54
55 private static class Helper<G, H, L, R, T> implements BinaryFunction<L, R, T>, Serializable {
56
57
58
59 private static final long serialVersionUID = 6013646799505641592L;
60 private BinaryFunction<? super G, ? super H, ? extends T> f;
61 private BinaryFunction<? super L, ? super R, ? extends G> g;
62 private BinaryFunction<? super L, ? super R, ? extends H> h;
63
64
65
66
67
68
69
70 public Helper(BinaryFunction<? super G, ? super H, ? extends T> f,
71 BinaryFunction<? super L, ? super R, ? extends G> g,
72 BinaryFunction<? super L, ? super R, ? extends H> h) {
73 this.f = f;
74 this.g = g;
75 this.h = h;
76 }
77
78
79
80
81 public T evaluate(L left, R right) {
82 return f.evaluate(g.evaluate(left, right), h.evaluate(left, right));
83 }
84 }
85
86 private final Helper<?, ?, L, R, T> helper;
87
88
89
90
91
92
93
94
95
96 public <G, H> BinaryCompositeBinaryFunction(BinaryFunction<? super G, ? super H, ? extends T> f,
97 BinaryFunction<? super L, ? super R, ? extends G> g, BinaryFunction<? super L, ? super R, ? extends H> h) {
98 if (f == null || g == null || h == null) {
99 throw new IllegalArgumentException("BinaryFunction arguments may not be null");
100 }
101 this.helper = new Helper<G, H, L, R, T>(f, g, h);
102 }
103
104
105
106
107
108
109 public final T evaluate(L left, R right) {
110 return helper.evaluate(left, right);
111 }
112
113
114
115
116 public final boolean equals(Object that) {
117 return that == this || (that instanceof BinaryCompositeBinaryFunction<?, ?, ?>
118 && equals((BinaryCompositeBinaryFunction<?, ?, ?>) that));
119 }
120
121
122
123
124
125
126 public final boolean equals(BinaryCompositeBinaryFunction<?, ?, ?> that) {
127 return null != that
128 && helper.f.equals(that.helper.f)
129 && helper.g.equals(that.helper.g)
130 && helper.h.equals(that.helper.h);
131 }
132
133
134
135
136 public int hashCode() {
137 int hash = "BinaryCompositeBinaryFunction".hashCode();
138 hash <<= HASH_SHIFT;
139 hash ^= helper.f.hashCode();
140 hash <<= HASH_SHIFT;
141 hash ^= helper.g.hashCode();
142 hash <<= HASH_SHIFT;
143 hash ^= helper.h.hashCode();
144 return hash;
145 }
146
147
148
149
150 public String toString() {
151 return "BinaryCompositeBinaryFunction<" + helper.f + ";" + helper.g + ";" + helper.h + ">";
152 }
153
154 }