001: /*
002: * Copyright 2001-2004,2006 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.functors;
017:
018: import java.io.Serializable;
019: import java.util.Collection;
020:
021: import org.apache.commons.collections.Predicate;
022:
023: /**
024: * Predicate implementation that returns true if all the
025: * predicates return true.
026: * If the array of predicates is empty, then this predicate returns true.
027: * <p>
028: * NOTE: In versions prior to 3.2 an array size of zero or one
029: * threw an exception.
030: *
031: * @since Commons Collections 3.0
032: * @version $Revision: 406071 $ $Date: 2006-05-13 11:44:37 +0100 (Sat, 13 May 2006) $
033: *
034: * @author Stephen Colebourne
035: * @author Matt Benson
036: */
037: public final class AllPredicate implements Predicate,
038: PredicateDecorator, Serializable {
039:
040: /** Serial version UID */
041: private static final long serialVersionUID = -3094696765038308799L;
042:
043: /** The array of predicates to call */
044: private final Predicate[] iPredicates;
045:
046: /**
047: * Factory to create the predicate.
048: * <p>
049: * If the array is size zero, the predicate always returns true.
050: * If the array is size one, then that predicate is returned.
051: *
052: * @param predicates the predicates to check, cloned, not null
053: * @return the <code>all</code> predicate
054: * @throws IllegalArgumentException if the predicates array is null
055: * @throws IllegalArgumentException if any predicate in the array is null
056: */
057: public static Predicate getInstance(Predicate[] predicates) {
058: FunctorUtils.validate(predicates);
059: if (predicates.length == 0) {
060: return TruePredicate.INSTANCE;
061: }
062: if (predicates.length == 1) {
063: return predicates[0];
064: }
065: predicates = FunctorUtils.copy(predicates);
066: return new AllPredicate(predicates);
067: }
068:
069: /**
070: * Factory to create the predicate.
071: * <p>
072: * If the collection is size zero, the predicate always returns true.
073: * If the collection is size one, then that predicate is returned.
074: *
075: * @param predicates the predicates to check, cloned, not null
076: * @return the <code>all</code> predicate
077: * @throws IllegalArgumentException if the predicates array is null
078: * @throws IllegalArgumentException if any predicate in the array is null
079: */
080: public static Predicate getInstance(Collection predicates) {
081: Predicate[] preds = FunctorUtils.validate(predicates);
082: if (preds.length == 0) {
083: return TruePredicate.INSTANCE;
084: }
085: if (preds.length == 1) {
086: return preds[0];
087: }
088: return new AllPredicate(preds);
089: }
090:
091: /**
092: * Constructor that performs no validation.
093: * Use <code>getInstance</code> if you want that.
094: *
095: * @param predicates the predicates to check, not cloned, not null
096: */
097: public AllPredicate(Predicate[] predicates) {
098: super ();
099: iPredicates = predicates;
100: }
101:
102: /**
103: * Evaluates the predicate returning true if all predicates return true.
104: *
105: * @param object the input object
106: * @return true if all decorated predicates return true
107: */
108: public boolean evaluate(Object object) {
109: for (int i = 0; i < iPredicates.length; i++) {
110: if (iPredicates[i].evaluate(object) == false) {
111: return false;
112: }
113: }
114: return true;
115: }
116:
117: /**
118: * Gets the predicates, do not modify the array.
119: *
120: * @return the predicates
121: * @since Commons Collections 3.1
122: */
123: public Predicate[] getPredicates() {
124: return iPredicates;
125: }
126:
127: }
|