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.BinaryPredicate; 20 21 /** 22 * {@link #test Tests} <code>true</code> iff 23 * none of its children test <code>false</code>. 24 * Note that by this definition, the "and" of 25 * an empty collection of predicates tests <code>true</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 <L> the left argument type. 35 * @param <R> the right argument type. 36 * @version $Revision: 1345136 $ $Date: 2012-06-01 09:47:06 -0300 (Fri, 01 Jun 2012) $ 37 */ 38 public final class BinaryAnd<L, R> extends BaseBinaryPredicateList<L, R> { 39 40 /** 41 * serialVersionUID declaration. 42 */ 43 private static final long serialVersionUID = -6741089498337923680L; 44 45 // constructor 46 // ------------------------------------------------------------------------ 47 /** 48 * Create a new BinaryAnd. 49 */ 50 public BinaryAnd() { 51 super(); 52 } 53 54 /** 55 * Create a new BinaryAnd instance. 56 * 57 * @param predicates the predicates to add 58 */ 59 public BinaryAnd(BinaryPredicate<? super L, ? super R>... predicates) { 60 super(predicates); 61 } 62 63 /** 64 * Create a new BinaryAnd instance. 65 * 66 * @param predicates the predicates to add 67 */ 68 public BinaryAnd(Iterable<BinaryPredicate<? super L, ? super R>> predicates) { 69 super(predicates); 70 } 71 72 // modifiers 73 // ------------------------------------------------------------------------ 74 /** 75 * And in a BinaryPredicate. 76 * @param p BinaryPredicate to add 77 * @return this 78 */ 79 public BinaryAnd<L, R> and(BinaryPredicate<? super L, ? super R> p) { 80 super.addBinaryPredicate(p); 81 return this; 82 } 83 84 // predicate interface 85 // ------------------------------------------------------------------------ 86 /** 87 * {@inheritDoc} 88 */ 89 public boolean test(L a, R b) { 90 for (BinaryPredicate<? super L, ? super R> p : getBinaryPredicateList()) { 91 if (!p.test(a, b)) { 92 return false; 93 } 94 } 95 return true; 96 } 97 98 /** 99 * {@inheritDoc} 100 */ 101 @Override 102 public boolean equals(Object that) { 103 return that == this || (that instanceof BinaryAnd<?, ?> && equals((BinaryAnd<?, ?>) that)); 104 } 105 106 /** 107 * Learn whether another BinaryAnd is equal to this. 108 * @param that the BinaryAnd to test 109 * @return boolean 110 */ 111 public boolean equals(BinaryAnd<?, ?> that) { 112 return getBinaryPredicateListEquals(that); 113 } 114 115 /** 116 * {@inheritDoc} 117 */ 118 @Override 119 public int hashCode() { 120 return "BinaryAnd".hashCode() ^ getBinaryPredicateListHashCode(); 121 } 122 123 /** 124 * {@inheritDoc} 125 */ 126 @Override 127 public String toString() { 128 return "BinaryAnd<" + getBinaryPredicateListToString() + ">"; 129 } 130 131 }