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