001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.functor.core.composite; 018 019 import java.io.Serializable; 020 021 import org.apache.commons.functor.Predicate; 022 023 /** 024 * A {@link Predicate Predicate} 025 * similiar to Java's "ternary" 026 * or "conditional" operator (<code>? :</code>). 027 * Given three {@link Predicate predicates} 028 * <i>p</i>, <i>q</i>, <i>r</i>, 029 * {@link #test tests} 030 * to 031 * <code>p.test() ? q.test() : r.test()</code>. 032 * <p> 033 * Note that although this class implements 034 * {@link Serializable}, a given instance will 035 * only be truly <code>Serializable</code> if all the 036 * underlying functors are. Attempts to serialize 037 * an instance whose delegates are not all 038 * <code>Serializable</code> will result in an exception. 039 * </p> 040 * @version $Revision: 1166388 $ $Date: 2011-09-07 22:29:02 +0200 (Wed, 07 Sep 2011) $ 041 * @author Rodney Waldhoff 042 */ 043 public final class ConditionalPredicate implements Predicate, Serializable { 044 /** 045 * serialVersionUID declaration. 046 */ 047 private static final long serialVersionUID = 7333505000745854098L; 048 049 /** Base hash integer used to shift hash */ 050 private static final int HASH_SHIFT = 4; 051 // attributes 052 // ------------------------------------------------------------------------ 053 private final Predicate ifPred; 054 private final Predicate thenPred; 055 private final Predicate elsePred; 056 057 // constructor 058 // ------------------------------------------------------------------------ 059 /** 060 * Create a new ConditionalPredicate. 061 * @param ifPred if 062 * @param thenPred then 063 * @param elsePred else 064 */ 065 public ConditionalPredicate(Predicate ifPred, Predicate thenPred, Predicate elsePred) { 066 if (ifPred == null || thenPred == null || elsePred == null) { 067 throw new IllegalArgumentException("One or more Predicate arguments was null"); 068 } 069 this.ifPred = ifPred; 070 this.thenPred = thenPred; 071 this.elsePred = elsePred; 072 } 073 074 // predicate interface 075 // ------------------------------------------------------------------------ 076 /** 077 * {@inheritDoc} 078 */ 079 public boolean test() { 080 return ifPred.test() ? thenPred.test() : elsePred.test(); 081 } 082 083 /** 084 * {@inheritDoc} 085 */ 086 public boolean equals(Object that) { 087 return that == this || (that instanceof ConditionalPredicate && equals((ConditionalPredicate) that)); 088 } 089 090 /** 091 * Learn whether another ConditionalPredicate is equal to this. 092 * @param that ConditionalPredicate to test 093 * @return boolean 094 */ 095 public boolean equals(ConditionalPredicate that) { 096 return null != that 097 && (null == ifPred ? null == that.ifPred : ifPred.equals(that.ifPred)) 098 && (null == thenPred ? null == that.thenPred : thenPred.equals(that.thenPred)) 099 && (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 }