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.BinaryPredicate;
22
23 /**
24 * A {@link BinaryPredicate BinaryPredicate}
25 * similiar to Java's "ternary"
26 * or "conditional" operator (<code>? :</code>).
27 * Given three {@link BinaryPredicate predicates}
28 * <i>p</i>, <i>q</i>, <i>r</i>,
29 * {@link #test tests}
30 * to
31 * <code>p.test(x,y) ? q.test(x,y) : r.test(x,y)</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: 1166383 $ $Date: 2011-09-07 22:21:04 +0200 (Wed, 07 Sep 2011) $
41 * @author Rodney Waldhoff
42 */
43 public final class ConditionalBinaryPredicate<L, R> implements BinaryPredicate<L, R>, Serializable {
44 /**
45 * serialVersionUID declaration.
46 */
47 private static final long serialVersionUID = -4511946801764080748L;
48
49 /** Base hash integer used to shift hash */
50 private static final int HASH_SHIFT = 4;
51 // attributes
52 // ------------------------------------------------------------------------
53 private final BinaryPredicate<? super L, ? super R> ifPred;
54 private final BinaryPredicate<? super L, ? super R> thenPred;
55 private final BinaryPredicate<? super L, ? super R> elsePred;
56
57 // constructor
58 // ------------------------------------------------------------------------
59 /**
60 * Create a new ConditionalBinaryPredicate.
61 * @param ifPred if
62 * @param thenPred then
63 * @param elsePred else
64 */
65 public ConditionalBinaryPredicate(BinaryPredicate<? super L, ? super R> ifPred,
66 BinaryPredicate<? super L, ? super R> thenPred, BinaryPredicate<? super L, ? super R> elsePred) {
67 if (ifPred == null || thenPred == null || elsePred == null) {
68 throw new IllegalArgumentException("One or more BinaryPredicate 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(L left, R right) {
81 return ifPred.test(left, right) ? thenPred.test(left, right) : elsePred.test(left, right);
82 }
83
84 /**
85 * {@inheritDoc}
86 */
87 public boolean equals(Object that) {
88 return that == this || (that instanceof ConditionalBinaryPredicate<?, ?>
89 && equals((ConditionalBinaryPredicate<?, ?>) that));
90 }
91
92 /**
93 * Learn whether another ConditionalBinaryPredicate is equal to this.
94 * @param that ConditionalBinaryPredicate to test
95 * @return boolean
96 */
97 public boolean equals(ConditionalBinaryPredicate<?, ?> 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 = "ConditionalBinaryPredicate".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 "ConditionalBinaryPredicate<" + ifPred + "?" + thenPred + ":" + elsePred + ">";
129 }
130
131 }