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 org.apache.commons.functor.BinaryPredicate;
20  import org.apache.commons.functor.BinaryProcedure;
21  import org.apache.commons.functor.core.NoOp;
22  import org.apache.commons.lang3.Validate;
23  
24  /**
25   * A {@link BinaryProcedure BinaryProcedure}
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 BinaryProcedure procedures}
30   * <i>q</i> and <i>r</i>, {@link #run runs}
31   * <code>if (p.test(x,y)) { q.run(x,y); } else { r.run(x,y); }</code>.
32   * @param <L> the left argument type.
33   * @param <R> the right argument type.
34   * @version $Revision: 1537602 $ $Date: 2013-10-31 20:53:09 +0100 (Do, 31 Okt 2013) $
35   */
36  public final class ConditionalBinaryProcedure<L, R> implements BinaryProcedure<L, R> {
37  
38      /** Base hash integer used to shift hash. */
39      private static final int HASH_SHIFT = 4;
40      // attributes
41      // ------------------------------------------------------------------------
42      /**
43       * the condition to be evaluated.
44       */
45      private final BinaryPredicate<? super L, ? super R> ifPred;
46      /**
47       * the predicate executed if the condition is satisfied.
48       */
49      private final BinaryProcedure<? super L, ? super R> thenProc;
50      /**
51       * the predicate executed if the condition is not satisfied.
52       */
53      private final BinaryProcedure<? super L, ? super R> elseProc;
54  
55      // constructor
56      // ------------------------------------------------------------------------
57  
58      /**
59       * Create a new ConditionalBinaryProcedure.
60       * @param ifPred to evaluate
61       * @param thenProc if <code>ifPred</code> yields <code>true</code>
62       */
63      public ConditionalBinaryProcedure(BinaryPredicate<? super L, ? super R> ifPred,
64              BinaryProcedure<? super L, ? super R> thenProc) {
65          this(ifPred, thenProc, NoOp.instance());
66      }
67  
68      /**
69       * Create a new ConditionalBinaryProcedure.
70       * @param ifPred to evaluate
71       * @param thenProc if <code>ifPred</code> yields <code>true</code>
72       * @param elseProc if <code>ifPred</code> yields <code>false</code>
73       */
74      public ConditionalBinaryProcedure(BinaryPredicate<? super L, ? super R> ifPred,
75              BinaryProcedure<? super L, ? super R> thenProc, BinaryProcedure<? super L, ? super R> elseProc) {
76          this.ifPred = Validate.notNull(ifPred, "BinaryPredicate argument was null");
77          this.thenProc = Validate.notNull(thenProc, "'then' BinaryProcedure argument was null");
78          this.elseProc = Validate.notNull(elseProc, "'else' BinaryProcedure argument was null");
79      }
80  
81      // predicate interface
82      // ------------------------------------------------------------------------
83  
84      /**
85       * {@inheritDoc}
86       */
87      public void run(L left, R right) {
88          if (ifPred.test(left, right)) {
89              thenProc.run(left, right);
90          } else {
91              elseProc.run(left, right);
92          }
93      }
94  
95      /**
96       * {@inheritDoc}
97       */
98      @Override
99      public boolean equals(Object obj) {
100         if (obj == this) {
101             return true;
102         }
103         if (!(obj instanceof ConditionalBinaryProcedure<?, ?>)) {
104             return false;
105         }
106         ConditionalBinaryProcedure<?, ?> that = (ConditionalBinaryProcedure<?, ?>) obj;
107         return this.ifPred.equals(that.ifPred)
108                 && this.thenProc.equals(that.thenProc)
109                 && this.elseProc.equals(that.elseProc);
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
115     @Override
116     public int hashCode() {
117         int hash = "ConditionalBinaryProcedure".hashCode();
118         hash <<= HASH_SHIFT;
119         hash ^= ifPred.hashCode();
120         hash <<= HASH_SHIFT;
121         hash ^= thenProc.hashCode();
122         hash <<= HASH_SHIFT;
123         hash ^= elseProc.hashCode();
124         return hash;
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     @Override
131     public String toString() {
132         return "ConditionalBinaryProcedure<" + ifPred + "?" + thenProc + ":" + elseProc + ">";
133     }
134 }