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 none of 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 NonePredicate implements Predicate,
038: PredicateDecorator, Serializable {
039:
040: /** Serial version UID */
041: private static final long serialVersionUID = 2007613066565892961L;
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: *
051: * @param predicates the predicates to check, cloned, not null
052: * @return the <code>any</code> predicate
053: * @throws IllegalArgumentException if the predicates array is null
054: * @throws IllegalArgumentException if any predicate in the array is null
055: */
056: public static Predicate getInstance(Predicate[] predicates) {
057: FunctorUtils.validate(predicates);
058: if (predicates.length == 0) {
059: return TruePredicate.INSTANCE;
060: }
061: predicates = FunctorUtils.copy(predicates);
062: return new NonePredicate(predicates);
063: }
064:
065: /**
066: * Factory to create the predicate.
067: * <p>
068: * If the collection is size zero, the predicate always returns true.
069: *
070: * @param predicates the predicates to check, cloned, not null
071: * @return the <code>one</code> predicate
072: * @throws IllegalArgumentException if the predicates array is null
073: * @throws IllegalArgumentException if any predicate in the array is null
074: */
075: public static Predicate getInstance(Collection predicates) {
076: Predicate[] preds = FunctorUtils.validate(predicates);
077: if (preds.length == 0) {
078: return TruePredicate.INSTANCE;
079: }
080: return new NonePredicate(preds);
081: }
082:
083: /**
084: * Constructor that performs no validation.
085: * Use <code>getInstance</code> if you want that.
086: *
087: * @param predicates the predicates to check, not cloned, not null
088: */
089: public NonePredicate(Predicate[] predicates) {
090: super ();
091: iPredicates = predicates;
092: }
093:
094: /**
095: * Evaluates the predicate returning false if any stored predicate returns false.
096: *
097: * @param object the input object
098: * @return true if none of decorated predicates return true
099: */
100: public boolean evaluate(Object object) {
101: for (int i = 0; i < iPredicates.length; i++) {
102: if (iPredicates[i].evaluate(object)) {
103: return false;
104: }
105: }
106: return true;
107: }
108:
109: /**
110: * Gets the predicates, do not modify the array.
111: *
112: * @return the predicates
113: * @since Commons Collections 3.1
114: */
115: public Predicate[] getPredicates() {
116: return iPredicates;
117: }
118:
119: }
|