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