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.Predicate; 020 021 /** 022 * {@link #test Tests} <code>true</code> iff 023 * none of its children test <code>false</code>. 024 * Note that by this definition, the "and" of 025 * an empty collection of predicates tests <code>true</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 And extends BasePredicateList { 038 039 // constructor 040 // ------------------------------------------------------------------------ 041 042 /** 043 * serialVersionUID declaration. 044 */ 045 private static final long serialVersionUID = -6053343095016685571L; 046 047 /** 048 * Create a new And. 049 */ 050 public And() { 051 super(); 052 } 053 054 /** 055 * Create a new And instance. 056 * 057 * @param predicates 058 */ 059 public And(Iterable<Predicate> predicates) { 060 super(predicates); 061 } 062 063 /** 064 * Create a new And instance. 065 * 066 * @param predicates 067 */ 068 public And(Predicate... predicates) { 069 super(predicates); 070 } 071 072 // modifiers 073 // ------------------------------------------------------------------------ 074 /** 075 * Add a Predicate. 076 * @param p Predicate to add 077 * @return this 078 */ 079 public And and(Predicate p) { 080 super.addPredicate(p); 081 return this; 082 } 083 084 // predicate interface 085 // ------------------------------------------------------------------------ 086 /** 087 * {@inheritDoc} 088 */ 089 public boolean test() { 090 for (Predicate p : getPredicateList()) { 091 if (!p.test()) { 092 return false; 093 } 094 } 095 return true; 096 } 097 098 /** 099 * {@inheritDoc} 100 */ 101 public boolean equals(Object that) { 102 return that == this || (that instanceof And && equals((And) that)); 103 } 104 105 /** 106 * Learn whether a given And is equal to this. 107 * @param that the And to test 108 * @return boolean 109 */ 110 public boolean equals(And that) { 111 return getPredicateListEquals(that); 112 } 113 114 /** 115 * {@inheritDoc} 116 */ 117 public int hashCode() { 118 return "And".hashCode() ^ getPredicateListHashCode(); 119 } 120 121 /** 122 * {@inheritDoc} 123 */ 124 public String toString() { 125 return "And<" + getPredicateListToString() + ">"; 126 } 127 128 }