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 java.io.Serializable;
20  
21  import org.apache.commons.functor.Predicate;
22  
23  /**
24   * A {@link Predicate Predicate}
25   * similiar to Java's "ternary"
26   * or "conditional" operator (<code>&#x3F; &#x3A;</code>).
27   * Given three {@link Predicate predicates}
28   * <i>p</i>, <i>q</i>, <i>r</i>,
29   * {@link #test tests}
30   * to
31   * <code>p.test() ? q.test() : r.test()</code>.
32   * <p>
33   * Note that although this class implements
34   * {@link Serializable}, a given instance will
35   * only be truly <code>Serializable</code> if all the
36   * underlying functors are.  Attempts to serialize
37   * an instance whose delegates are not all
38   * <code>Serializable</code> will result in an exception.
39   * </p>
40   * @version $Revision: 1166388 $ $Date: 2011-09-07 22:29:02 +0200 (Wed, 07 Sep 2011) $
41   * @author Rodney Waldhoff
42   */
43  public final class ConditionalPredicate implements Predicate, Serializable {
44      /**
45       * serialVersionUID declaration.
46       */
47      private static final long serialVersionUID = 7333505000745854098L;
48  
49      /** Base hash integer used to shift hash */
50      private static final int HASH_SHIFT = 4;
51      // attributes
52      // ------------------------------------------------------------------------
53      private final Predicate ifPred;
54      private final Predicate thenPred;
55      private final Predicate elsePred;
56  
57      // constructor
58      // ------------------------------------------------------------------------
59      /**
60       * Create a new ConditionalPredicate.
61       * @param ifPred if
62       * @param thenPred then
63       * @param elsePred else
64       */
65      public ConditionalPredicate(Predicate ifPred, Predicate thenPred, Predicate elsePred) {
66          if (ifPred == null || thenPred == null || elsePred == null) {
67              throw new IllegalArgumentException("One or more Predicate arguments was null");
68          }
69          this.ifPred = ifPred;
70          this.thenPred = thenPred;
71          this.elsePred = elsePred;
72      }
73  
74      // predicate interface
75      // ------------------------------------------------------------------------
76      /**
77       * {@inheritDoc}
78       */
79      public boolean test() {
80          return ifPred.test() ? thenPred.test() : elsePred.test();
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      public boolean equals(Object that) {
87          return that == this || (that instanceof ConditionalPredicate && equals((ConditionalPredicate) that));
88      }
89  
90      /**
91       * Learn whether another ConditionalPredicate is equal to this.
92       * @param that ConditionalPredicate to test
93       * @return boolean
94       */
95      public boolean equals(ConditionalPredicate that) {
96          return null != that
97                  && (null == ifPred ? null == that.ifPred : ifPred.equals(that.ifPred))
98                  && (null == thenPred ? null == that.thenPred : thenPred.equals(that.thenPred))
99                  && (null == elsePred ? null == that.elsePred : elsePred.equals(that.elsePred));
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     public int hashCode() {
106         int hash = "ConditionalPredicate".hashCode();
107         if (null != ifPred) {
108             hash <<= HASH_SHIFT;
109             hash ^= ifPred.hashCode();
110         }
111         if (null != thenPred) {
112             hash <<= HASH_SHIFT;
113             hash ^= thenPred.hashCode();
114         }
115         if (null != elsePred) {
116             hash <<= HASH_SHIFT;
117             hash ^= elsePred.hashCode();
118         }
119         return hash;
120     }
121 
122     /**
123      * {@inheritDoc}
124      */
125     public String toString() {
126         return "ConditionalPredicate<" + ifPred + "?" + thenPred + ":" + elsePred + ">";
127     }
128 
129 }