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 * 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 * @param <A> the predicate argument type. 27 * @version $Revision: 1537602 $ $Date: 2013-10-31 20:53:09 +0100 (Do, 31 Okt 2013) $ 28 */ 29 public final class And<A> extends BasePredicateList<A> { 30 31 // constructor 32 // ------------------------------------------------------------------------ 33 /** 34 * Create a new And. 35 */ 36 public And() { 37 super(); 38 } 39 40 /** 41 * Create a new And instance. 42 * 43 * @param predicates the predicates to put in and. 44 */ 45 public And(Iterable<Predicate<? super A>> predicates) { 46 super(predicates); 47 } 48 49 /** 50 * Create a new And instance. 51 * 52 * @param predicates the predicates to put in and. 53 */ 54 public And(Predicate<? super A>... predicates) { 55 super(predicates); 56 } 57 58 // modifiers 59 // ------------------------------------------------------------------------ 60 /** 61 * Fluently add a Predicate. 62 * @param p Predicate to add 63 * @return this 64 */ 65 public And<A> and(Predicate<? super A> p) { 66 super.addPredicate(p); 67 return this; 68 } 69 70 // predicate interface 71 // ------------------------------------------------------------------------ 72 /** 73 * {@inheritDoc} 74 */ 75 public boolean test(A obj) { 76 for (Predicate<? super A> p : getPredicateList()) { 77 if (!p.test(obj)) { 78 return false; 79 } 80 } 81 return true; 82 } 83 84 /** 85 * {@inheritDoc} 86 */ 87 @Override 88 public boolean equals(Object obj) { 89 if (obj == this) { 90 return true; 91 } 92 if (!(obj instanceof And<?>)) { 93 return false; 94 } 95 And<?> that = (And<?>) obj; 96 return getPredicateListEquals(that); 97 } 98 99 /** 100 * {@inheritDoc} 101 */ 102 @Override 103 public int hashCode() { 104 return "And".hashCode() ^ getPredicateListHashCode(); 105 } 106 107 /** 108 * {@inheritDoc} 109 */ 110 @Override 111 public String toString() { 112 return "And<" + getPredicateListToString() + ">"; 113 } 114 115 }