AnyPredicate.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.functors;

  18. import java.util.Collection;

  19. import org.apache.commons.collections4.Predicate;

  20. /**
  21.  * Predicate implementation that returns true if any of the
  22.  * predicates return true.
  23.  * If the array of predicates is empty, then this predicate returns false.
  24.  * <p>
  25.  * NOTE: In versions prior to 3.2 an array size of zero or one
  26.  * threw an exception.
  27.  * </p>
  28.  *
  29.  * @param <T> the type of the input to the predicate.
  30.  * @since 3.0
  31.  */
  32. public final class AnyPredicate<T> extends AbstractQuantifierPredicate<T> {

  33.     /** Serial version UID */
  34.     private static final long serialVersionUID = 7429999530934647542L;

  35.     /**
  36.      * Creates the predicate.
  37.      * <p>
  38.      * If the collection is size zero, the predicate always returns false.
  39.      * If the collection is size one, then that predicate is returned.
  40.      * </p>
  41.      *
  42.      * @param <T> the type that the predicate queries
  43.      * @param predicates  the predicates to check, cloned, not null
  44.      * @return the {@code all} predicate
  45.      * @throws NullPointerException if the predicates array is null
  46.      * @throws NullPointerException if any predicate in the array is null
  47.      */
  48.     @SuppressWarnings("unchecked")
  49.     public static <T> Predicate<T> anyPredicate(final Collection<? extends Predicate<? super T>> predicates) {
  50.         final Predicate<? super T>[] preds = FunctorUtils.validate(predicates);
  51.         if (preds.length == 0) {
  52.             return FalsePredicate.<T>falsePredicate();
  53.         }
  54.         if (preds.length == 1) {
  55.             return (Predicate<T>) preds[0];
  56.         }
  57.         return new AnyPredicate<>(preds);
  58.     }

  59.     /**
  60.      * Creates the predicate.
  61.      * <p>
  62.      * If the array is size zero, the predicate always returns false.
  63.      * If the array is size one, then that predicate is returned.
  64.      * </p>
  65.      *
  66.      * @param <T> the type that the predicate queries
  67.      * @param predicates  the predicates to check, cloned, not null
  68.      * @return the {@code any} predicate
  69.      * @throws NullPointerException if the predicates array is null
  70.      * @throws NullPointerException if any predicate in the array is null
  71.      */
  72.     @SuppressWarnings("unchecked")
  73.     public static <T> Predicate<T> anyPredicate(final Predicate<? super T>... predicates) {
  74.         FunctorUtils.validate(predicates);
  75.         if (predicates.length == 0) {
  76.             return FalsePredicate.<T>falsePredicate();
  77.         }
  78.         if (predicates.length == 1) {
  79.             return (Predicate<T>) predicates[0];
  80.         }
  81.         return new AnyPredicate<T>(FunctorUtils.copy(predicates));
  82.     }

  83.     /**
  84.      * Constructor that performs no validation.
  85.      * Use {@code anyPredicate} if you want that.
  86.      *
  87.      * @param predicates  the predicates to check, not cloned, not null
  88.      */
  89.     public AnyPredicate(final Predicate<? super T>... predicates) {
  90.         super(predicates);
  91.     }

  92.     /**
  93.      * Evaluates the predicate returning true if any predicate returns true.
  94.      *
  95.      * @param object  the input object
  96.      * @return true if any decorated predicate return true
  97.      */
  98.     @Override
  99.     public boolean test(final T object) {
  100.         for (final Predicate<? super T> iPredicate : iPredicates) {
  101.             if (iPredicate.test(object)) {
  102.                 return true;
  103.             }
  104.         }
  105.         return false;
  106.     }

  107. }