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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public final class ConditionalBinaryPredicate<L, R> implements BinaryPredicate<L, R>, Serializable {
44
45
46
47 private static final long serialVersionUID = -4511946801764080748L;
48
49
50 private static final int HASH_SHIFT = 4;
51
52
53 private final BinaryPredicate<? super L, ? super R> ifPred;
54 private final BinaryPredicate<? super L, ? super R> thenPred;
55 private final BinaryPredicate<? super L, ? super R> elsePred;
56
57
58
59
60
61
62
63
64
65 public ConditionalBinaryPredicate(BinaryPredicate<? super L, ? super R> ifPred,
66 BinaryPredicate<? super L, ? super R> thenPred, BinaryPredicate<? super L, ? super R> elsePred) {
67 if (ifPred == null || thenPred == null || elsePred == null) {
68 throw new IllegalArgumentException("One or more BinaryPredicate arguments was null");
69 }
70 this.ifPred = ifPred;
71 this.thenPred = thenPred;
72 this.elsePred = elsePred;
73 }
74
75
76
77
78
79
80 public boolean test(L left, R right) {
81 return ifPred.test(left, right) ? thenPred.test(left, right) : elsePred.test(left, right);
82 }
83
84
85
86
87 public boolean equals(Object that) {
88 return that == this || (that instanceof ConditionalBinaryPredicate<?, ?>
89 && equals((ConditionalBinaryPredicate<?, ?>) that));
90 }
91
92
93
94
95
96
97 public boolean equals(ConditionalBinaryPredicate<?, ?> that) {
98 return null != that
99 && (null == ifPred ? null == that.ifPred : ifPred.equals(that.ifPred))
100 && (null == thenPred ? null == that.thenPred : thenPred.equals(that.thenPred))
101 && (null == elsePred ? null == that.elsePred : elsePred.equals(that.elsePred));
102 }
103
104
105
106
107 public int hashCode() {
108 int hash = "ConditionalBinaryPredicate".hashCode();
109 if (null != ifPred) {
110 hash <<= HASH_SHIFT;
111 hash ^= ifPred.hashCode();
112 }
113 if (null != thenPred) {
114 hash <<= HASH_SHIFT;
115 hash ^= thenPred.hashCode();
116 }
117 if (null != elsePred) {
118 hash <<= HASH_SHIFT;
119 hash ^= elsePred.hashCode();
120 }
121 return hash;
122 }
123
124
125
126
127 public String toString() {
128 return "ConditionalBinaryPredicate<" + ifPred + "?" + thenPred + ":" + elsePred + ">";
129 }
130
131 }