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