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 import org.apache.commons.lang3.Validate;
24
25
26
27
28
29
30
31
32
33 public class TransformedBinaryFunction<L, R, T> implements BinaryFunction<L, R, T>, Serializable {
34
35
36
37 private static final long serialVersionUID = 3312781645741807814L;
38
39
40
41
42
43
44 private static final class Helper<X, L, R, T> implements BinaryFunction<L, R, T>, Serializable {
45
46
47
48 private static final long serialVersionUID = 8141488776884860650L;
49
50
51
52 private BinaryFunction<? super L, ? super R, ? extends X> preceding;
53
54
55
56 private UnaryFunction<? super X, ? extends T> following;
57
58
59
60
61
62
63 private Helper(BinaryFunction<? super L, ? super R, ? extends X> preceding,
64 UnaryFunction<? super X, ? extends T> following) {
65 this.preceding = Validate.notNull(preceding, "BinaryFunction argument was null");
66 this.following = Validate.notNull(following, "UnaryFunction argument was null");
67 }
68
69
70
71
72 public T evaluate(L left, R right) {
73 return following.evaluate(preceding.evaluate(left, right));
74 }
75 }
76
77
78
79
80 private final Helper<?, L, R, T> helper;
81
82
83
84
85
86
87
88 public <X> TransformedBinaryFunction(BinaryFunction<? super L, ? super R, ? extends X> preceding,
89 UnaryFunction<? super X, ? extends T> following) {
90 this.helper = new Helper<X, L, R, T>(preceding, following);
91 }
92
93
94
95
96 public final T evaluate(L left, R right) {
97 return helper.evaluate(left, right);
98 }
99
100
101
102
103 @Override
104 public final boolean equals(Object obj) {
105 return obj == this || obj instanceof TransformedBinaryFunction<?, ?, ?>
106 && equals((TransformedBinaryFunction<?, ?, ?>) obj);
107 }
108
109
110
111
112
113
114 public final boolean equals(TransformedBinaryFunction<?, ?, ?> that) {
115 return that != null && that.helper.preceding.equals(this.helper.preceding)
116 && that.helper.following.equals(this.helper.following);
117 }
118
119
120
121
122 @Override
123 public int hashCode() {
124 int result = "TransformedBinaryFunction".hashCode();
125 result <<= 2;
126 result |= helper.following.hashCode();
127 result <<= 2;
128 result |= helper.preceding.hashCode();
129 return result;
130 }
131
132
133
134
135 @Override
136 public String toString() {
137 return "TransformedBinaryFunction<" + helper.preceding + "; " + helper.following + ">";
138 }
139 }