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