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.lang3.Validate;
23  
24  /**
25   * A {@link BinaryPredicate BinaryPredicate}
26   * similiar to Java's "ternary"
27   * or "conditional" operator (<code>&#x3F; &#x3A;</code>).
28   * Given three {@link BinaryPredicate predicates}
29   * <i>p</i>, <i>q</i>, <i>r</i>,
30   * {@link #test tests}
31   * to
32   * <code>p.test(x,y) ? q.test(x,y) : r.test(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   * @param <L> the left argument type.
42   * @param <R> the right argument type.
43   * @version $Revision: 1365329 $ $Date: 2012-07-24 19:34:23 -0300 (Tue, 24 Jul 2012) $
44   */
45  public final class ConditionalBinaryPredicate<L, R> implements BinaryPredicate<L, R>, Serializable {
46      /**
47       * serialVersionUID declaration.
48       */
49      private static final long serialVersionUID = -4511946801764080748L;
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 BinaryPredicate<? super L, ? super R> ifPred;
59      /**
60       * the predicate executed if the condition is satisfied.
61       */
62      private final BinaryPredicate<? super L, ? super R> thenPred;
63      /**
64       * the predicate executed if the condition is not satisfied.
65       */
66      private final BinaryPredicate<? super L, ? super R> elsePred;
67  
68      // constructor
69      // ------------------------------------------------------------------------
70      /**
71       * Create a new ConditionalBinaryPredicate.
72       * @param ifPred if
73       * @param thenPred then
74       * @param elsePred else
75       */
76      public ConditionalBinaryPredicate(BinaryPredicate<? super L, ? super R> ifPred,
77              BinaryPredicate<? super L, ? super R> thenPred, BinaryPredicate<? super L, ? super R> elsePred) {
78          this.ifPred = Validate.notNull(ifPred, "'if' BinaryPredicate argument was null");
79          this.thenPred = Validate.notNull(thenPred, "'then' BinaryPredicate argument was null");
80          this.elsePred = Validate.notNull(elsePred, "'else' BinaryPredicate argument was null");
81      }
82  
83      // predicate interface
84      // ------------------------------------------------------------------------
85      /**
86       * {@inheritDoc}
87       */
88      public boolean test(L left, R right) {
89          return ifPred.test(left, right) ? thenPred.test(left, right) : elsePred.test(left, right);
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public boolean equals(Object that) {
97          return that == this || (that instanceof ConditionalBinaryPredicate<?, ?>
98                                      && equals((ConditionalBinaryPredicate<?, ?>) that));
99      }
100 
101     /**
102      * Learn whether another ConditionalBinaryPredicate is equal to this.
103      * @param that ConditionalBinaryPredicate to test
104      * @return boolean
105      */
106     public boolean equals(ConditionalBinaryPredicate<?, ?> that) {
107         return null != that
108                 && ifPred.equals(that.ifPred)
109                 && thenPred.equals(that.thenPred)
110                 && elsePred.equals(that.elsePred);
111     }
112 
113     /**
114      * {@inheritDoc}
115      */
116     @Override
117     public int hashCode() {
118         int hash = "ConditionalBinaryPredicate".hashCode();
119         hash <<= HASH_SHIFT;
120         hash ^= ifPred.hashCode();
121         hash <<= HASH_SHIFT;
122         hash ^= thenPred.hashCode();
123         hash <<= HASH_SHIFT;
124         hash ^= elsePred.hashCode();
125         return hash;
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     @Override
132     public String toString() {
133         return "ConditionalBinaryPredicate<" + ifPred + "?" + thenPred + ":" + elsePred + ">";
134     }
135 
136 }