View Javadoc
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  
19  import java.util.Collection;
20  
21  import org.apache.commons.collections4.Predicate;
22  
23  /**
24   * Predicate implementation that returns true if none of 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   * @since 3.0
33   */
34  public final class NonePredicate<T> extends AbstractQuantifierPredicate<T> {
35  
36      /** Serial version UID */
37      private static final long serialVersionUID = 2007613066565892961L;
38  
39      /**
40       * Factory to create the predicate.
41       * <p>
42       * If the collection is size zero, the predicate always returns true.
43       *
44       * @param <T> the type that the predicate queries
45       * @param predicates  the predicates to check, cloned, not null
46       * @return the {@code one} predicate
47       * @throws NullPointerException if the predicates array is null
48       * @throws NullPointerException if any predicate in the array is null
49       */
50      public static <T> Predicate<T> nonePredicate(final Collection<? extends Predicate<? super T>> predicates) {
51          final Predicate<? super T>[] preds = FunctorUtils.validate(predicates);
52          if (preds.length == 0) {
53              return TruePredicate.<T>truePredicate();
54          }
55          return new NonePredicate<>(preds);
56      }
57  
58      /**
59       * Factory to create the predicate.
60       * <p>
61       * If the array is size zero, the predicate always returns true.
62       *
63       * @param <T> the type that the predicate queries
64       * @param predicates  the predicates to check, cloned, not null
65       * @return the {@code any} predicate
66       * @throws NullPointerException if the predicates array is null
67       * @throws NullPointerException if any predicate in the array is null
68       */
69      public static <T> Predicate<T> nonePredicate(final Predicate<? super T>... predicates) {
70          FunctorUtils.validate(predicates);
71          if (predicates.length == 0) {
72              return TruePredicate.<T>truePredicate();
73          }
74          return new NonePredicate<>(FunctorUtils.copy(predicates));
75      }
76  
77      /**
78       * Constructor that performs no validation.
79       * Use {@code nonePredicate} if you want that.
80       *
81       * @param predicates  the predicates to check, not cloned, not null
82       */
83      public NonePredicate(final Predicate<? super T>... predicates) {
84          super(predicates);
85      }
86  
87      /**
88       * Evaluates the predicate returning false if any stored predicate returns false.
89       *
90       * @param object  the input object
91       * @return true if none of decorated predicates return true
92       */
93      @Override
94      public boolean evaluate(final T object) {
95          for (final Predicate<? super T> iPredicate : iPredicates) {
96              if (iPredicate.evaluate(object)) {
97                  return false;
98              }
99          }
100         return true;
101     }
102 
103 }