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