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
23 /**
24 * A {@link UnaryPredicate UnaryPredicate}
25 * similiar to Java's "ternary"
26 * or "conditional" operator (<code>? :</code>).
27 * Given three {@link UnaryPredicate predicate}
28 * <i>p</i>, <i>q</i>, <i>r</i>,
29 * {@link #test tests}
30 * to
31 * <code>p.test(x) ? q.test(x) : r.test(x)</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: 1170779 $ $Date: 2011-09-14 21:16:47 +0200 (Wed, 14 Sep 2011) $
41 * @author Rodney Waldhoff
42 */
43 public final class ConditionalUnaryPredicate<A> implements UnaryPredicate<A>, Serializable {
44 /**
45 * serialVersionUID declaration.
46 */
47 private static final long serialVersionUID = 1214714029872180155L;
48
49 /** Base hash integer used to shift hash */
50 private static final int HASH_SHIFT = 4;
51 // attributes
52 // ------------------------------------------------------------------------
53 private final UnaryPredicate<? super A> ifPred;
54 private final UnaryPredicate<? super A> thenPred;
55 private final UnaryPredicate<? super A> elsePred;
56
57 // constructor
58 // ------------------------------------------------------------------------
59 /**
60 * Create a new ConditionalUnaryPredicate.
61 * @param ifPred if
62 * @param thenPred then
63 * @param elsePred else
64 */
65 public ConditionalUnaryPredicate(UnaryPredicate<? super A> ifPred, UnaryPredicate<? super A> thenPred,
66 UnaryPredicate<? super A> elsePred) {
67 if (ifPred == null || thenPred == null || elsePred == null) {
68 throw new IllegalArgumentException("One or more UnaryPredicate arguments was null");
69 }
70 this.ifPred = ifPred;
71 this.thenPred = thenPred;
72 this.elsePred = elsePred;
73 }
74
75 // predicate interface
76 // ------------------------------------------------------------------------
77 /**
78 * {@inheritDoc}
79 */
80 public boolean test(A obj) {
81 return ifPred.test(obj) ? thenPred.test(obj) : elsePred.test(obj);
82 }
83
84 /**
85 * {@inheritDoc}
86 */
87 public boolean equals(Object that) {
88 return that == this || (that instanceof ConditionalUnaryPredicate<?>
89 && equals((ConditionalUnaryPredicate<?>) that));
90 }
91
92 /**
93 * Learn whether another ConditionalUnaryPredicate is equal to this.
94 * @param that ConditionalUnaryPredicate to test
95 * @return boolean
96 */
97 public boolean equals(ConditionalUnaryPredicate<?> that) {
98 return null != that
99 && (null == ifPred ? null == that.ifPred : ifPred.equals(that.ifPred))
100 && (null == thenPred ? null == that.thenPred : thenPred.equals(that.thenPred))
101 && (null == elsePred ? null == that.elsePred : elsePred.equals(that.elsePred));
102 }
103
104 /**
105 * {@inheritDoc}
106 */
107 public int hashCode() {
108 int hash = "ConditionalUnaryPredicate".hashCode();
109 if (null != ifPred) {
110 hash <<= HASH_SHIFT;
111 hash ^= ifPred.hashCode();
112 }
113 if (null != thenPred) {
114 hash <<= HASH_SHIFT;
115 hash ^= thenPred.hashCode();
116 }
117 if (null != elsePred) {
118 hash <<= HASH_SHIFT;
119 hash ^= elsePred.hashCode();
120 }
121 return hash;
122 }
123
124 /**
125 * {@inheritDoc}
126 */
127 public String toString() {
128 return "ConditionalUnaryPredicate<" + ifPred + "?" + thenPred + ":" + elsePred + ">";
129 }
130
131 }