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 */
017package org.apache.commons.collections4.functors;
018
019import java.util.Collection;
020
021import org.apache.commons.collections4.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 3.0
032 */
033public final class NonePredicate<T> extends AbstractQuantifierPredicate<T> {
034
035    /** Serial version UID */
036    private static final long serialVersionUID = 2007613066565892961L;
037
038    /**
039     * Factory to create the predicate.
040     * <p>
041     * If the array is size zero, the predicate always returns true.
042     *
043     * @param <T> the type that the predicate queries
044     * @param predicates  the predicates to check, cloned, not null
045     * @return the <code>any</code> predicate
046     * @throws NullPointerException if the predicates array is null
047     * @throws NullPointerException if any predicate in the array is null
048     */
049    public static <T> Predicate<T> nonePredicate(final Predicate<? super T>... predicates) {
050        FunctorUtils.validate(predicates);
051        if (predicates.length == 0) {
052            return TruePredicate.<T>truePredicate();
053        }
054        return new NonePredicate<>(FunctorUtils.copy(predicates));
055    }
056
057    /**
058     * Factory to create the predicate.
059     * <p>
060     * If the collection is size zero, the predicate always returns true.
061     *
062     * @param <T> the type that the predicate queries
063     * @param predicates  the predicates to check, cloned, not null
064     * @return the <code>one</code> predicate
065     * @throws NullPointerException if the predicates array is null
066     * @throws NullPointerException if any predicate in the array is null
067     */
068    public static <T> Predicate<T> nonePredicate(final Collection<? extends Predicate<? super T>> predicates) {
069        final Predicate<? super T>[] preds = FunctorUtils.validate(predicates);
070        if (preds.length == 0) {
071            return TruePredicate.<T>truePredicate();
072        }
073        return new NonePredicate<>(preds);
074    }
075
076    /**
077     * Constructor that performs no validation.
078     * Use <code>nonePredicate</code> if you want that.
079     *
080     * @param predicates  the predicates to check, not cloned, not null
081     */
082    public NonePredicate(final Predicate<? super T>... predicates) {
083        super(predicates);
084    }
085
086    /**
087     * Evaluates the predicate returning false if any stored predicate returns false.
088     *
089     * @param object  the input object
090     * @return true if none of decorated predicates return true
091     */
092    @Override
093    public boolean evaluate(final T object) {
094        for (final Predicate<? super T> iPredicate : iPredicates) {
095            if (iPredicate.evaluate(object)) {
096                return false;
097            }
098        }
099        return true;
100    }
101
102}