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