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.functor.BinaryProcedure;
23  import org.apache.commons.functor.core.NoOp;
24  import org.apache.commons.lang3.Validate;
25  
26  /**
27   * A {@link BinaryProcedure BinaryProcedure}
28   * similiar to Java's "ternary"
29   * or "conditional" operator (<code>&#x3F; &#x3A;</code>).
30   * Given a {@link BinaryPredicate predicate}
31   * <i>p</i> and {@link BinaryProcedure procedures}
32   * <i>q</i> and <i>r</i>, {@link #run runs}
33   * <code>if (p.test(x,y)) { q.run(x,y); } else { r.run(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   * @version $Revision: 1365329 $ $Date: 2012-07-24 19:34:23 -0300 (Tue, 24 Jul 2012) $
45   */
46  public final class ConditionalBinaryProcedure<L, R> implements BinaryProcedure<L, R>, Serializable {
47  
48      /**
49       * serialVersionUID declaration.
50       */
51      private static final long serialVersionUID = -3521992036791188475L;
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 predicate executed if the condition is satisfied.
63       */
64      private final BinaryProcedure<? super L, ? super R> thenProc;
65      /**
66       * the predicate executed if the condition is not satisfied.
67       */
68      private final BinaryProcedure<? super L, ? super R> elseProc;
69  
70      // constructor
71      // ------------------------------------------------------------------------
72  
73      /**
74       * Create a new ConditionalBinaryProcedure.
75       * @param ifPred to evaluate
76       * @param thenProc if <code>ifPred</code> yields <code>true</code>
77       */
78      public ConditionalBinaryProcedure(BinaryPredicate<? super L, ? super R> ifPred,
79              BinaryProcedure<? super L, ? super R> thenProc) {
80          this(ifPred, thenProc, NoOp.instance());
81      }
82  
83      /**
84       * Create a new ConditionalBinaryProcedure.
85       * @param ifPred to evaluate
86       * @param thenProc if <code>ifPred</code> yields <code>true</code>
87       * @param elseProc if <code>ifPred</code> yields <code>false</code>
88       */
89      public ConditionalBinaryProcedure(BinaryPredicate<? super L, ? super R> ifPred,
90              BinaryProcedure<? super L, ? super R> thenProc, BinaryProcedure<? super L, ? super R> elseProc) {
91          this.ifPred = Validate.notNull(ifPred, "BinaryPredicate argument was null");
92          this.thenProc = Validate.notNull(thenProc, "'then' BinaryProcedure argument was null");
93          this.elseProc = Validate.notNull(elseProc, "'else' BinaryProcedure argument was null");
94      }
95  
96      // predicate interface
97      // ------------------------------------------------------------------------
98  
99      /**
100      * {@inheritDoc}
101      */
102     public void run(L left, R right) {
103         if (ifPred.test(left, right)) {
104             thenProc.run(left, right);
105         } else {
106             elseProc.run(left, right);
107         }
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     public boolean equals(Object that) {
115         return that == this || (that instanceof ConditionalBinaryProcedure<?, ?>
116                                     && equals((ConditionalBinaryProcedure<?, ?>) that));
117     }
118 
119     /**
120      * Learn whether a given ConditionalBinaryProcedure is equal to this.
121      * @param that compared object
122      * @return boolean
123      */
124     public boolean equals(ConditionalBinaryProcedure<?, ?> that) {
125         return null != that
126                 && ifPred.equals(that.ifPred)
127                 && thenProc.equals(that.thenProc)
128                 && elseProc.equals(that.elseProc);
129     }
130 
131     /**
132      * {@inheritDoc}
133      */
134     @Override
135     public int hashCode() {
136         int hash = "ConditionalBinaryProcedure".hashCode();
137         hash <<= HASH_SHIFT;
138         hash ^= ifPred.hashCode();
139         hash <<= HASH_SHIFT;
140         hash ^= thenProc.hashCode();
141         hash <<= HASH_SHIFT;
142         hash ^= elseProc.hashCode();
143         return hash;
144     }
145 
146     /**
147      * {@inheritDoc}
148      */
149     @Override
150     public String toString() {
151         return "ConditionalBinaryProcedure<" + ifPred + "?" + thenProc + ":" + elseProc + ">";
152     }
153 }