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.UnaryPredicate; 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 UnaryAnd<A> extends BaseUnaryPredicateList<A> { 038 039 /** 040 * serialVersionUID declaration. 041 */ 042 private static final long serialVersionUID = 8324861737107307302L; 043 044 // constructor 045 // ------------------------------------------------------------------------ 046 /** 047 * Create a new UnaryAnd. 048 */ 049 public UnaryAnd() { 050 super(); 051 } 052 053 /** 054 * Create a new UnaryAnd instance. 055 * 056 * @param predicates 057 */ 058 public UnaryAnd(Iterable<UnaryPredicate<? super A>> predicates) { 059 super(predicates); 060 } 061 062 /** 063 * Create a new UnaryAnd instance. 064 * 065 * @param predicates 066 */ 067 public UnaryAnd(UnaryPredicate<? super A>... predicates) { 068 super(predicates); 069 } 070 071 // modifiers 072 // ------------------------------------------------------------------------ 073 /** 074 * Fluently add a UnaryPredicate. 075 * @param p UnaryPredicate to add 076 * @return this 077 */ 078 public UnaryAnd<A> and(UnaryPredicate<? super A> p) { 079 super.addUnaryPredicate(p); 080 return this; 081 } 082 083 // predicate interface 084 // ------------------------------------------------------------------------ 085 /** 086 * {@inheritDoc} 087 */ 088 public boolean test(A obj) { 089 for (UnaryPredicate<? super A> p : getUnaryPredicateList()) { 090 if (!p.test(obj)) { 091 return false; 092 } 093 } 094 return true; 095 } 096 097 /** 098 * {@inheritDoc} 099 */ 100 public boolean equals(Object that) { 101 return that == this || (that instanceof UnaryAnd<?> && equals((UnaryAnd<?>) that)); 102 } 103 104 /** 105 * Learn whether another UnaryAnd is equal to this. 106 * @param that UnaryAnd to test 107 * @return boolean 108 */ 109 public boolean equals(UnaryAnd<?> that) { 110 return getUnaryPredicateListEquals(that); 111 } 112 113 /** 114 * {@inheritDoc} 115 */ 116 public int hashCode() { 117 return "UnaryAnd".hashCode() ^ getUnaryPredicateListHashCode(); 118 } 119 120 /** 121 * {@inheritDoc} 122 */ 123 public String toString() { 124 return "UnaryAnd<" + getUnaryPredicateListToString() + ">"; 125 } 126 127 }