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.Predicate;
22 import org.apache.commons.functor.Procedure;
23 import org.apache.commons.functor.core.NoOp;
24
25 /**
26 * A {@link Procedure Procedure}
27 * similiar to Java's "ternary"
28 * or "conditional" operator (<code>? :</code>).
29 * Given a {@link Predicate predicate}
30 * <i>p</i> and {@link Procedure procedures}
31 * <i>q</i> and <i>r</i>, {@link #run runs}
32 * <code>if (p.test()) { q.run(); } else { r.run(); }</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: 1170777 $ $Date: 2011-09-14 21:13:47 +0200 (Wed, 14 Sep 2011) $
42 * @author Rodney Waldhoff
43 */
44 public final class ConditionalProcedure implements Procedure, Serializable {
45 /**
46 * serialVersionUID declaration.
47 */
48 private static final long serialVersionUID = -4228632798836328605L;
49
50 /** Base hash integer used to shift hash */
51 private static final int HASH_SHIFT = 4;
52
53 // attributes
54 // ------------------------------------------------------------------------
55 private final Predicate ifPred;
56 private final Procedure thenProc;
57 private final Procedure elseProc;
58
59 // constructor
60 // ------------------------------------------------------------------------
61 /**
62 * Create a new ConditionalProcedure.
63 * @param ifPred if
64 * @param thenProc then
65 */
66 public ConditionalProcedure(Predicate ifPred, Procedure thenProc) {
67 this(ifPred, thenProc, NoOp.instance());
68 }
69
70 /**
71 * Create a new ConditionalProcedure.
72 * @param ifPred if
73 * @param thenProc then
74 * @param elseProc else
75 */
76 public ConditionalProcedure(Predicate ifPred, Procedure thenProc, Procedure elseProc) {
77 if (ifPred == null) {
78 throw new IllegalArgumentException("Predicate argument was null");
79 }
80 this.ifPred = ifPred;
81 if (thenProc == null || elseProc == null) {
82 throw new IllegalArgumentException("Procedure argument was null");
83 }
84 this.thenProc = thenProc;
85 this.elseProc = elseProc;
86 }
87
88 // predicate interface
89 // ------------------------------------------------------------------------
90 /**
91 * {@inheritDoc}
92 */
93 public void run() {
94 if (ifPred.test()) {
95 thenProc.run();
96 } else {
97 elseProc.run();
98 }
99 }
100
101 /**
102 * {@inheritDoc}
103 */
104 public boolean equals(Object that) {
105 return that == this || (that instanceof ConditionalProcedure && equals((ConditionalProcedure) that));
106 }
107
108 /**
109 * Learn whether another ConditionalProcecure is equal to this.
110 * @param that the ConditionalProcedure to test
111 * @return boolean
112 */
113 public boolean equals(ConditionalProcedure that) {
114 return null != that
115 && (null == ifPred ? null == that.ifPred : ifPred.equals(that.ifPred))
116 && (null == thenProc ? null == that.thenProc : thenProc.equals(that.thenProc))
117 && (null == elseProc ? null == that.elseProc : elseProc.equals(that.elseProc));
118 }
119
120 /**
121 * {@inheritDoc}
122 */
123 public int hashCode() {
124 int hash = "ConditionalProcedure".hashCode();
125 if (null != ifPred) {
126 hash <<= HASH_SHIFT;
127 hash ^= ifPred.hashCode();
128 }
129 if (null != thenProc) {
130 hash <<= HASH_SHIFT;
131 hash ^= thenProc.hashCode();
132 }
133 if (null != elseProc) {
134 hash <<= HASH_SHIFT;
135 hash ^= elseProc.hashCode();
136 }
137 return hash;
138 }
139
140 /**
141 * {@inheritDoc}
142 */
143 public String toString() {
144 return "ConditionalProcedure<" + ifPred + "?" + thenProc + ":" + elseProc + ">";
145 }
146
147 }