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.lang3.Validate; 23 24 /** 25 * A {@link Predicate Predicate} 26 * similiar to Java's "ternary" 27 * or "conditional" operator (<code>? :</code>). 28 * Given three {@link Predicate predicates} 29 * <i>p</i>, <i>q</i>, <i>r</i>, 30 * {@link #test tests} 31 * to 32 * <code>p.test() ? q.test() : r.test()</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 * @version $Revision: 1365329 $ $Date: 2012-07-24 19:34:23 -0300 (Tue, 24 Jul 2012) $ 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 /** 54 * the condition to be evaluated. 55 */ 56 private final Predicate ifPred; 57 /** 58 * the predicate executed if the condition is satisfied. 59 */ 60 private final Predicate thenPred; 61 /** 62 * the predicate executed if the condition is not satisfied. 63 */ 64 private final Predicate elsePred; 65 66 // constructor 67 // ------------------------------------------------------------------------ 68 /** 69 * Create a new ConditionalPredicate. 70 * @param ifPred if 71 * @param thenPred then 72 * @param elsePred else 73 */ 74 public ConditionalPredicate(Predicate ifPred, Predicate thenPred, Predicate elsePred) { 75 this.ifPred = Validate.notNull(ifPred, "'if' Predicate argument was null"); 76 this.thenPred = Validate.notNull(thenPred, "'then' Predicate argument was null"); 77 this.elsePred = Validate.notNull(elsePred, "'else' Predicate argument was null"); 78 } 79 80 // predicate interface 81 // ------------------------------------------------------------------------ 82 /** 83 * {@inheritDoc} 84 */ 85 public boolean test() { 86 return ifPred.test() ? thenPred.test() : elsePred.test(); 87 } 88 89 /** 90 * {@inheritDoc} 91 */ 92 @Override 93 public boolean equals(Object that) { 94 return that == this || (that instanceof ConditionalPredicate && equals((ConditionalPredicate) that)); 95 } 96 97 /** 98 * Learn whether another ConditionalPredicate is equal to this. 99 * @param that ConditionalPredicate to test 100 * @return boolean 101 */ 102 public boolean equals(ConditionalPredicate that) { 103 return null != that 104 && ifPred.equals(that.ifPred) 105 && thenPred.equals(that.thenPred) 106 && elsePred.equals(that.elsePred); 107 } 108 109 /** 110 * {@inheritDoc} 111 */ 112 @Override 113 public int hashCode() { 114 int hash = "ConditionalPredicate".hashCode(); 115 hash <<= HASH_SHIFT; 116 hash ^= ifPred.hashCode(); 117 hash <<= HASH_SHIFT; 118 hash ^= thenPred.hashCode(); 119 hash <<= HASH_SHIFT; 120 hash ^= elsePred.hashCode(); 121 return hash; 122 } 123 124 /** 125 * {@inheritDoc} 126 */ 127 @Override 128 public String toString() { 129 return "ConditionalPredicate<" + ifPred + "?" + thenPred + ":" + elsePred + ">"; 130 } 131 132 }