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.UnaryPredicate;
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 ConditionalUnaryPredicate<A> implements UnaryPredicate<A>, Serializable {
44
45
46
47 private static final long serialVersionUID = 1214714029872180155L;
48
49
50 private static final int HASH_SHIFT = 4;
51
52
53 private final UnaryPredicate<? super A> ifPred;
54 private final UnaryPredicate<? super A> thenPred;
55 private final UnaryPredicate<? super A> elsePred;
56
57
58
59
60
61
62
63
64
65 public ConditionalUnaryPredicate(UnaryPredicate<? super A> ifPred, UnaryPredicate<? super A> thenPred,
66 UnaryPredicate<? super A> elsePred) {
67 if (ifPred == null || thenPred == null || elsePred == null) {
68 throw new IllegalArgumentException("One or more UnaryPredicate 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(A obj) {
81 return ifPred.test(obj) ? thenPred.test(obj) : elsePred.test(obj);
82 }
83
84
85
86
87 public boolean equals(Object that) {
88 return that == this || (that instanceof ConditionalUnaryPredicate<?>
89 && equals((ConditionalUnaryPredicate<?>) that));
90 }
91
92
93
94
95
96
97 public boolean equals(ConditionalUnaryPredicate<?> 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 = "ConditionalUnaryPredicate".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 "ConditionalUnaryPredicate<" + ifPred + "?" + thenPred + ":" + elsePred + ">";
129 }
130
131 }