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