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 static org.apache.commons.collections4.functors.FunctorUtils.coerce;
020import static org.apache.commons.collections4.functors.FunctorUtils.validate;
021import static org.apache.commons.collections4.functors.TruePredicate.truePredicate;
022
023import java.util.Collection;
024
025import org.apache.commons.collections4.Predicate;
026
027/**
028 * Predicate implementation that returns true if all the
029 * predicates return true.
030 * If the array of predicates is empty, then this predicate returns true.
031 * <p>
032 * NOTE: In versions prior to 3.2 an array size of zero or one
033 * threw an exception.
034 *
035 * @since 3.0
036 */
037public final class AllPredicate<T> extends AbstractQuantifierPredicate<T> {
038
039    /** Serial version UID */
040    private static final long serialVersionUID = -3094696765038308799L;
041
042    /**
043     * Factory to create the predicate.
044     * <p>
045     * If the array is size zero, the predicate always returns true.
046     * If the array is size one, then that predicate is returned.
047     *
048     * @param <T> the type that the predicate queries
049     * @param predicates  the predicates to check, cloned, not null
050     * @return the <code>all</code> predicate
051     * @throws NullPointerException if the predicates array is null
052     * @throws NullPointerException if any predicate in the array is null
053     */
054    public static <T> Predicate<T> allPredicate(final Predicate<? super T>... predicates) {
055        FunctorUtils.validate(predicates);
056        if (predicates.length == 0) {
057            return truePredicate();
058        }
059        if (predicates.length == 1) {
060            return coerce(predicates[0]);
061        }
062
063        return new AllPredicate<>(FunctorUtils.copy(predicates));
064    }
065
066    /**
067     * Factory to create the predicate.
068     * <p>
069     * If the collection is size zero, the predicate always returns true.
070     * If the collection is size one, then that predicate is returned.
071     *
072     * @param <T> the type that the predicate queries
073     * @param predicates  the predicates to check, cloned, not null
074     * @return the <code>all</code> predicate
075     * @throws NullPointerException if the predicates array is null
076     * @throws NullPointerException if any predicate in the array is null
077     */
078    public static <T> Predicate<T> allPredicate(final Collection<? extends Predicate<? super T>> predicates) {
079        final Predicate<? super T>[] preds = validate(predicates);
080        if (preds.length == 0) {
081            return truePredicate();
082        }
083        if (preds.length == 1) {
084            return coerce(preds[0]);
085        }
086        return new AllPredicate<>(preds);
087    }
088
089    /**
090     * Constructor that performs no validation.
091     * Use <code>allPredicate</code> if you want that.
092     *
093     * @param predicates  the predicates to check, not cloned, not null
094     */
095    public AllPredicate(final Predicate<? super T>... predicates) {
096        super(predicates);
097    }
098
099    /**
100     * Evaluates the predicate returning true if all predicates return true.
101     *
102     * @param object  the input object
103     * @return true if all decorated predicates return true
104     */
105    @Override
106    public boolean evaluate(final T object) {
107        for (final Predicate<? super T> iPredicate : iPredicates) {
108            if (!iPredicate.evaluate(object)) {
109                return false;
110            }
111        }
112        return true;
113    }
114
115}