View Javadoc

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