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.lang3.Validate; 23 24 /** 25 * A {@link UnaryPredicate UnaryPredicate} 26 * similiar to Java's "ternary" 27 * or "conditional" operator (<code>? :</code>). 28 * Given three {@link UnaryPredicate predicate} 29 * <i>p</i>, <i>q</i>, <i>r</i>, 30 * {@link #test tests} 31 * to 32 * <code>p.test(x) ? q.test(x) : r.test(x)</code>. 33 * <p> 34 * Note that although this class implements 35 * {@link Serializable}, a given instance will 36 * only be truly <code>Serializable</code> if all the 37 * underlying functors are. Attempts to serialize 38 * an instance whose delegates are not all 39 * <code>Serializable</code> will result in an exception. 40 * </p> 41 * @param <A> the predicate argument type. 42 * @version $Revision: 1365329 $ $Date: 2012-07-24 19:34:23 -0300 (Tue, 24 Jul 2012) $ 43 */ 44 public final class ConditionalUnaryPredicate<A> implements UnaryPredicate<A>, Serializable { 45 /** 46 * serialVersionUID declaration. 47 */ 48 private static final long serialVersionUID = 1214714029872180155L; 49 50 /** Base hash integer used to shift hash. */ 51 private static final int HASH_SHIFT = 4; 52 // attributes 53 // ------------------------------------------------------------------------ 54 /** 55 * the condition to be evaluated. 56 */ 57 private final UnaryPredicate<? super A> ifPred; 58 /** 59 * the predicate executed if the condition is satisfied. 60 */ 61 private final UnaryPredicate<? super A> thenPred; 62 /** 63 * the predicate executed if the condition is not satisfied. 64 */ 65 private final UnaryPredicate<? super A> elsePred; 66 67 // constructor 68 // ------------------------------------------------------------------------ 69 /** 70 * Create a new ConditionalUnaryPredicate. 71 * @param ifPred if 72 * @param thenPred then 73 * @param elsePred else 74 */ 75 public ConditionalUnaryPredicate(UnaryPredicate<? super A> ifPred, UnaryPredicate<? super A> thenPred, 76 UnaryPredicate<? super A> elsePred) { 77 this.ifPred = Validate.notNull(ifPred, "'if' UnaryPredicate argument was null"); 78 this.thenPred = Validate.notNull(thenPred, "'then' UnaryPredicate argument was null"); 79 this.elsePred = Validate.notNull(elsePred, "'else' UnaryPredicate argument was null"); 80 } 81 82 // predicate interface 83 // ------------------------------------------------------------------------ 84 /** 85 * {@inheritDoc} 86 */ 87 public boolean test(A obj) { 88 return ifPred.test(obj) ? thenPred.test(obj) : elsePred.test(obj); 89 } 90 91 /** 92 * {@inheritDoc} 93 */ 94 @Override 95 public boolean equals(Object that) { 96 return that == this || (that instanceof ConditionalUnaryPredicate<?> 97 && equals((ConditionalUnaryPredicate<?>) that)); 98 } 99 100 /** 101 * Learn whether another ConditionalUnaryPredicate is equal to this. 102 * @param that ConditionalUnaryPredicate to test 103 * @return boolean 104 */ 105 public boolean equals(ConditionalUnaryPredicate<?> that) { 106 return null != that 107 && ifPred.equals(that.ifPred) 108 && thenPred.equals(that.thenPred) 109 && elsePred.equals(that.elsePred); 110 } 111 112 /** 113 * {@inheritDoc} 114 */ 115 @Override 116 public int hashCode() { 117 int hash = "ConditionalUnaryPredicate".hashCode(); 118 hash <<= HASH_SHIFT; 119 hash ^= ifPred.hashCode(); 120 hash <<= HASH_SHIFT; 121 hash ^= thenPred.hashCode(); 122 hash <<= HASH_SHIFT; 123 hash ^= elsePred.hashCode(); 124 return hash; 125 } 126 127 /** 128 * {@inheritDoc} 129 */ 130 @Override 131 public String toString() { 132 return "ConditionalUnaryPredicate<" + ifPred + "?" + thenPred + ":" + elsePred + ">"; 133 } 134 135 }