AllPredicate.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 static org.apache.commons.collections4.functors.FunctorUtils.coerce;
  19. import static org.apache.commons.collections4.functors.FunctorUtils.validate;
  20. import static org.apache.commons.collections4.functors.TruePredicate.truePredicate;

  21. import java.util.Collection;

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

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

  36.     /** Serial version UID */
  37.     private static final long serialVersionUID = -3094696765038308799L;

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

  61.     /**
  62.      * Creates the predicate.
  63.      * <p>
  64.      * If the array is size zero, the predicate always returns true.
  65.      * If the array is size one, then that predicate is returned.
  66.      * </p>
  67.      *
  68.      * @param <T> the type that the predicate queries
  69.      * @param predicates  the predicates to check, cloned, not null
  70.      * @return the {@code all} predicate
  71.      * @throws NullPointerException if the predicates array is null
  72.      * @throws NullPointerException if any predicate in the array is null
  73.      */
  74.     public static <T> Predicate<T> allPredicate(final Predicate<? super T>... predicates) {
  75.         validate(predicates);
  76.         if (predicates.length == 0) {
  77.             return truePredicate();
  78.         }
  79.         if (predicates.length == 1) {
  80.             return coerce(predicates[0]);
  81.         }
  82.         // <T> not needed in Eclipse but needed by the command line compiler
  83.         return new AllPredicate<T>(FunctorUtils.copy(predicates));
  84.     }

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

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

  109. }