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