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