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 org.apache.commons.functor.UnaryPredicate; 20 21 /** 22 * {@link #test Tests} <code>true</code> iff 23 * at least one of its children test <code>true</code>. 24 * Note that by this definition, the "or" of 25 * an empty collection of predicates tests <code>false</code>. 26 * <p> 27 * Note that although this class implements 28 * {@link java.io.Serializable Serializable}, a given instance will 29 * only be truly <code>Serializable</code> if all the 30 * underlying functors are. Attempts to serialize 31 * an instance whose delegates are not all 32 * <code>Serializable</code> will result in an exception. 33 * </p> 34 * @param <A> the predicate argument type. 35 * @version $Revision: 1345136 $ $Date: 2012-06-01 09:47:06 -0300 (Fri, 01 Jun 2012) $ 36 */ 37 public final class UnaryOr<A> extends BaseUnaryPredicateList<A> { 38 39 /** 40 * serialVersionUID declaration. 41 */ 42 private static final long serialVersionUID = -4072936447932983645L; 43 44 // constructor 45 // ------------------------------------------------------------------------ 46 /** 47 * Create a new UnaryOr. 48 */ 49 public UnaryOr() { 50 super(); 51 } 52 53 /** 54 * Create a new UnaryOr instance. 55 * 56 * @param predicates the predicates to put in unary or. 57 */ 58 public UnaryOr(Iterable<UnaryPredicate<? super A>> predicates) { 59 super(predicates); 60 } 61 62 /** 63 * Create a new UnaryOr instance. 64 * 65 * @param predicates the predicates to put in unary or. 66 */ 67 public UnaryOr(UnaryPredicate<? super A>... predicates) { 68 super(predicates); 69 } 70 71 // modifiers 72 // ------------------------------------------------------------------------ 73 /** 74 * Fluently add a Predicate. 75 * @param p Predicate to add 76 * @return this 77 */ 78 public UnaryOr<A> or(UnaryPredicate<? super A> p) { 79 super.addUnaryPredicate(p); 80 return this; 81 } 82 83 // predicate interface 84 // ------------------------------------------------------------------------ 85 /** 86 * {@inheritDoc} 87 */ 88 public boolean test(A a) { 89 for (UnaryPredicate<? super A> p : getUnaryPredicateList()) { 90 if (p.test(a)) { 91 return true; 92 } 93 } 94 return false; 95 } 96 97 /** 98 * {@inheritDoc} 99 */ 100 @Override 101 public boolean equals(Object that) { 102 return that == this || (that instanceof UnaryOr<?> && equals((UnaryOr<?>) that)); 103 } 104 105 /** 106 * Learn whether another UnaryOr is equal to this. 107 * @param that UnaryOr to test 108 * @return boolean 109 */ 110 public boolean equals(UnaryOr<?> that) { 111 return getUnaryPredicateListEquals(that); 112 } 113 114 /** 115 * {@inheritDoc} 116 */ 117 @Override 118 public int hashCode() { 119 return "UnaryOr".hashCode() ^ getUnaryPredicateListHashCode(); 120 } 121 122 /** 123 * {@inheritDoc} 124 */ 125 @Override 126 public String toString() { 127 return "UnaryOr<" + getUnaryPredicateListToString() + ">"; 128 } 129 130 }