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.Predicate; 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 * @version $Revision: 1345136 $ $Date: 2012-06-01 09:47:06 -0300 (Fri, 01 Jun 2012) $ 35 */ 36 public final class Or extends BasePredicateList { 37 38 /** 39 * serialVersionUID declaration. 40 */ 41 private static final long serialVersionUID = -1636233158061690073L; 42 43 // constructor 44 // ------------------------------------------------------------------------ 45 /** 46 * Create a new Or. 47 */ 48 public Or() { 49 super(); 50 } 51 52 /** 53 * Create a new Or instance. 54 * 55 * @param predicates predicates have to be put in or condition. 56 */ 57 public Or(Iterable<Predicate> predicates) { 58 super(predicates); 59 } 60 61 /** 62 * Create a new Or instance. 63 * 64 * @param predicates predicates have to be put in or condition. 65 */ 66 public Or(Predicate... predicates) { 67 super(predicates); 68 } 69 70 /** 71 * Fluently add a Predicate. 72 * @param p Predicate to add 73 * @return this 74 */ 75 public Or or(Predicate p) { 76 super.addPredicate(p); 77 return this; 78 } 79 80 // predicate interface 81 // ------------------------------------------------------------------------ 82 /** 83 * {@inheritDoc} 84 */ 85 public boolean test() { 86 for (Predicate p : getPredicateList()) { 87 if (p.test()) { 88 return true; 89 } 90 } 91 return false; 92 } 93 94 /** 95 * {@inheritDoc} 96 */ 97 @Override 98 public boolean equals(Object that) { 99 return that == this || (that instanceof Or && equals((Or) that)); 100 } 101 102 /** 103 * Learn whether another Or is equal to this. 104 * @param that Or to test 105 * @return boolean 106 */ 107 public boolean equals(Or that) { 108 return getPredicateListEquals(that); 109 } 110 111 /** 112 * {@inheritDoc} 113 */ 114 @Override 115 public int hashCode() { 116 return "Or".hashCode() ^ getPredicateListHashCode(); 117 } 118 119 /** 120 * {@inheritDoc} 121 */ 122 @Override 123 public String toString() { 124 return "Or<" + getPredicateListToString() + ">"; 125 } 126 127 }