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