1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
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 * A {@link BinaryProcedure BinaryProcedure}
27 * similiar to Java's "ternary"
28 * or "conditional" operator (<code>? :</code>).
29 * Given a {@link BinaryPredicate predicate}
30 * <i>p</i> and {@link BinaryProcedure procedures}
31 * <i>q</i> and <i>r</i>, {@link #run runs}
32 * <code>if (p.test(x,y)) { q.run(x,y); } else { r.run(x,y); }</code>.
33 * <p>
34 * Note that although this class implements
35 * {@link Serializable}, a given instance will
36 * only be truly <code>Serializable</code> if all the
37 * underlying functors are. Attempts to serialize
38 * an instance whose delegates are not all
39 * <code>Serializable</code> will result in an exception.
40 * </p>
41 * @version $Revision: 1166384 $ $Date: 2011-09-07 22:22:00 +0200 (Wed, 07 Sep 2011) $
42 * @author Rodney Waldhoff
43 */
44 public final class ConditionalBinaryProcedure<L, R> implements BinaryProcedure<L, R>, Serializable {
45
46 /**
47 * serialVersionUID declaration.
48 */
49 private static final long serialVersionUID = -3521992036791188475L;
50
51 /** Base hash integer used to shift hash */
52 private static final int HASH_SHIFT = 4;
53 // attributes
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 // constructor
60 // ------------------------------------------------------------------------
61
62 /**
63 * Create a new ConditionalBinaryProcedure.
64 * @param ifPred to evaluate
65 * @param thenProc if <code>ifPred</code> yields <code>true</code>
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 * Create a new ConditionalBinaryProcedure.
74 * @param ifPred to evaluate
75 * @param thenProc if <code>ifPred</code> yields <code>true</code>
76 * @param elseProc if <code>ifPred</code> yields <code>false</code>
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 // predicate interface
92 // ------------------------------------------------------------------------
93
94 /**
95 * {@inheritDoc}
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 * {@inheritDoc}
107 */
108 public boolean equals(Object that) {
109 return that == this || (that instanceof ConditionalBinaryProcedure<?, ?>
110 && equals((ConditionalBinaryProcedure<?, ?>) that));
111 }
112
113 /**
114 * Learn whether a given ConditionalBinaryProcedure is equal to this.
115 * @param that compared object
116 * @return boolean
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 * {@inheritDoc}
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 * {@inheritDoc}
147 */
148 public String toString() {
149 return "ConditionalBinaryProcedure<" + ifPred + "?" + thenProc + ":" + elseProc + ">";
150 }
151 }