NonePredicate.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 none of the
  22.  * predicates return true.
  23.  * If the array of predicates is empty, then this predicate returns true.
  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 NonePredicate<T> extends AbstractQuantifierPredicate<T> {

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

  35.     /**
  36.      * Creates the predicate.
  37.      * <p>
  38.      * If the collection is size zero, the predicate always returns true.
  39.      * </p>
  40.      *
  41.      * @param <T> the type that the predicate queries
  42.      * @param predicates  the predicates to check, cloned, not null
  43.      * @return the {@code one} predicate
  44.      * @throws NullPointerException if the predicates array is null
  45.      * @throws NullPointerException if any predicate in the array is null
  46.      */
  47.     public static <T> Predicate<T> nonePredicate(final Collection<? extends Predicate<? super T>> predicates) {
  48.         final Predicate<? super T>[] preds = FunctorUtils.validate(predicates);
  49.         if (preds.length == 0) {
  50.             return TruePredicate.<T>truePredicate();
  51.         }
  52.         return new NonePredicate<>(preds);
  53.     }

  54.     /**
  55.      * Creates the predicate.
  56.      * <p>
  57.      * If the array is size zero, the predicate always returns true.
  58.      * </p>
  59.      *
  60.      * @param <T> the type that the predicate queries
  61.      * @param predicates  the predicates to check, cloned, not null
  62.      * @return the {@code any} predicate
  63.      * @throws NullPointerException if the predicates array is null
  64.      * @throws NullPointerException if any predicate in the array is null
  65.      */
  66.     public static <T> Predicate<T> nonePredicate(final Predicate<? super T>... predicates) {
  67.         FunctorUtils.validate(predicates);
  68.         if (predicates.length == 0) {
  69.             return TruePredicate.<T>truePredicate();
  70.         }
  71.         // <T> not needed in Eclipse but needed by the command line compiler
  72.         return new NonePredicate<T>(FunctorUtils.copy(predicates));
  73.     }

  74.     /**
  75.      * Constructor that performs no validation.
  76.      * Use {@code nonePredicate} if you want that.
  77.      *
  78.      * @param predicates  the predicates to check, not cloned, not null
  79.      */
  80.     public NonePredicate(final Predicate<? super T>... predicates) {
  81.         super(predicates);
  82.     }

  83.     /**
  84.      * Evaluates the predicate returning false if any stored predicate returns false.
  85.      *
  86.      * @param object  the input object
  87.      * @return true if none of decorated predicates return true
  88.      */
  89.     @Override
  90.     public boolean test(final T object) {
  91.         for (final Predicate<? super T> iPredicate : iPredicates) {
  92.             if (iPredicate.test(object)) {
  93.                 return false;
  94.             }
  95.         }
  96.         return true;
  97.     }

  98. }