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.Function;
22 import org.apache.commons.functor.Predicate;
23 import org.apache.commons.lang3.Validate;
24
25 /**
26 * A {@link Function Function}
27 * similiar to Java's "ternary"
28 * or "conditional" operator (<code>? :</code>).
29 * Given a {@link Predicate predicate}
30 * <i>p</i> and {@link Function functions}
31 * <i>f</i> and <i>g</i>, {@link #evaluate evaluates}
32 * to
33 * <code>p.test() ? f.evaluate() : g.evaluate()</code>.
34 * <p>
35 * Note that although this class implements
36 * {@link Serializable}, a given instance will
37 * only be truly <code>Serializable</code> if all the
38 * underlying functors are. Attempts to serialize
39 * an instance whose delegates are not all
40 * <code>Serializable</code> will result in an exception.
41 * </p>
42 * @param <T> the returned value type.
43 * @version $Revision: 1365329 $ $Date: 2012-07-24 19:34:23 -0300 (Tue, 24 Jul 2012) $
44 */
45 public final class ConditionalFunction<T> implements Function<T>, Serializable {
46 /**
47 * serialVersionUID declaration.
48 */
49 private static final long serialVersionUID = 4214871352184887792L;
50
51 /** Base hash integer used to shift hash. */
52 private static final int HASH_SHIFT = 4;
53 // attributes
54 // ------------------------------------------------------------------------
55 /**
56 * the condition to be evaluated.
57 */
58 private final Predicate ifPred;
59 /**
60 * the function executed if the condition is satisfied.
61 */
62 private final Function<? extends T> thenFunc;
63 /**
64 * the function executed if the condition is not satisfied.
65 */
66 private final Function<? extends T> elseFunc;
67
68 // constructor
69 // ------------------------------------------------------------------------
70 /**
71 * Create a new ConditionalFunction.
72 * @param ifPred if
73 * @param thenFunc then
74 * @param elseFunc else
75 */
76 public ConditionalFunction(Predicate ifPred, Function<? extends T> thenFunc, Function<? extends T> elseFunc) {
77 this.ifPred = Validate.notNull(ifPred, "Predicate argument was null");
78 this.thenFunc = Validate.notNull(thenFunc, "'then' Function argument was null");
79 this.elseFunc = Validate.notNull(elseFunc, "'else' Function argument was null");
80 }
81
82 // predicate interface
83 // ------------------------------------------------------------------------
84 /**
85 * {@inheritDoc}
86 */
87 public T evaluate() {
88 if (ifPred.test()) {
89 return thenFunc.evaluate();
90 } else {
91 return elseFunc.evaluate();
92 }
93 }
94
95 /**
96 * {@inheritDoc}
97 */
98 @Override
99 public boolean equals(Object that) {
100 return that == this || (that instanceof ConditionalFunction<?> && equals((ConditionalFunction<?>) that));
101 }
102
103 /**
104 * Learn whether another ConditionalFunction is equal to this.
105 * @param that ConditionalFunction to test
106 * @return boolean
107 */
108 public boolean equals(ConditionalFunction<?> that) {
109 return null != that
110 && ifPred.equals(that.ifPred)
111 && thenFunc.equals(that.thenFunc)
112 && elseFunc.equals(that.elseFunc);
113 }
114
115 /**
116 * {@inheritDoc}
117 */
118 @Override
119 public int hashCode() {
120 int hash = "ConditionalFunction".hashCode();
121 hash <<= HASH_SHIFT;
122 hash ^= ifPred.hashCode();
123 hash <<= HASH_SHIFT;
124 hash ^= thenFunc.hashCode();
125 hash <<= HASH_SHIFT;
126 hash ^= elseFunc.hashCode();
127 return hash;
128 }
129
130 /**
131 * {@inheritDoc}
132 */
133 @Override
134 public String toString() {
135 return "ConditionalFunction<" + ifPred + "?" + thenFunc + ":" + elseFunc + ">";
136 }
137
138 }