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 org.apache.commons.functor.BinaryPredicate; 020 021 /** 022 * {@link #test Tests} <code>true</code> iff 023 * at least one of its children test <code>true</code>. 024 * Note that by this definition, the "or" of 025 * an empty collection of predicates tests <code>false</code>. 026 * <p> 027 * Note that although this class implements 028 * {@link java.io.Serializable Serializable}, a given instance will 029 * only be truly <code>Serializable</code> if all the 030 * underlying functors are. Attempts to serialize 031 * an instance whose delegates are not all 032 * <code>Serializable</code> will result in an exception. 033 * </p> 034 * @version $Revision: 1156320 $ $Date: 2011-08-10 21:14:50 +0200 (Wed, 10 Aug 2011) $ 035 * @author Rodney Waldhoff 036 */ 037 public final class BinaryOr<L, R> extends BaseBinaryPredicateList<L, R> { 038 039 /** 040 * serialVersionUID declaration. 041 */ 042 private static final long serialVersionUID = -831325131082968810L; 043 044 // constructor 045 // ------------------------------------------------------------------------ 046 /** 047 * Create a new BinaryOr. 048 */ 049 public BinaryOr() { 050 super(); 051 } 052 053 /** 054 * Create a new BinaryOr instance. 055 * 056 * @param predicates 057 */ 058 public BinaryOr(BinaryPredicate<? super L, ? super R>... predicates) { 059 super(predicates); 060 } 061 062 /** 063 * Create a new BinaryOr instance. 064 * 065 * @param predicates 066 */ 067 public BinaryOr(Iterable<BinaryPredicate<? super L, ? super R>> predicates) { 068 super(predicates); 069 } 070 071 // modifiers 072 // ------------------------------------------------------------------------ 073 /** 074 * Fluently add a BinaryPredicate. 075 * @param p BinaryPredicate to add 076 * @return this 077 */ 078 public BinaryOr<L, R> or(BinaryPredicate<? super L, ? super R> p) { 079 super.addBinaryPredicate(p); 080 return this; 081 } 082 083 // predicate interface 084 // ------------------------------------------------------------------------ 085 /** 086 * {@inheritDoc} 087 */ 088 public boolean test(L a, R b) { 089 for (BinaryPredicate<? super L, ? super R> p : getBinaryPredicateList()) { 090 if (p.test(a, b)) { 091 return true; 092 } 093 } 094 return false; 095 } 096 097 /** 098 * {@inheritDoc} 099 */ 100 public boolean equals(Object that) { 101 return that == this || (that instanceof BinaryOr<?, ?> && equals((BinaryOr<?, ?>) that)); 102 } 103 104 /** 105 * Learn whether another BinaryOr is equal to this. 106 * @param that BinaryOr to test 107 * @return boolean 108 */ 109 public boolean equals(BinaryOr<?, ?> that) { 110 return getBinaryPredicateListEquals(that); 111 } 112 113 /** 114 * {@inheritDoc} 115 */ 116 public int hashCode() { 117 return "BinaryOr".hashCode() ^ getBinaryPredicateListHashCode(); 118 } 119 120 /** 121 * {@inheritDoc} 122 */ 123 public String toString() { 124 return "BinaryOr<" + getBinaryPredicateListToString() + ">"; 125 } 126 127 }