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