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 static org.apache.commons.collections4.functors.FunctorUtils.coerce;
20  import static org.apache.commons.collections4.functors.FunctorUtils.validate;
21  import static org.apache.commons.collections4.functors.TruePredicate.truePredicate;
22  
23  import java.util.Collection;
24  
25  import org.apache.commons.collections4.Predicate;
26  
27  /**
28   * Predicate implementation that returns true if all the
29   * predicates return true.
30   * If the array of predicates is empty, then this predicate returns true.
31   * <p>
32   * NOTE: In versions prior to 3.2 an array size of zero or one
33   * threw an exception.
34   * </p>
35   *
36   * @since 3.0
37   */
38  public final class AllPredicate<T> extends AbstractQuantifierPredicate<T> {
39  
40      /** Serial version UID */
41      private static final long serialVersionUID = -3094696765038308799L;
42  
43      /**
44       * Factory to create the predicate.
45       * <p>
46       * If the collection is size zero, the predicate always returns true.
47       * If the collection is size one, then that predicate is returned.
48       *
49       * @param <T> the type that the predicate queries
50       * @param predicates  the predicates to check, cloned, not null
51       * @return the {@code all} predicate
52       * @throws NullPointerException if the predicates array is null
53       * @throws NullPointerException if any predicate in the array is null
54       */
55      public static <T> Predicate<T> allPredicate(final Collection<? extends Predicate<? super T>> predicates) {
56          final Predicate<? super T>[] preds = validate(predicates);
57          if (preds.length == 0) {
58              return truePredicate();
59          }
60          if (preds.length == 1) {
61              return coerce(preds[0]);
62          }
63          return new AllPredicate<>(preds);
64      }
65  
66      /**
67       * Factory to create the predicate.
68       * <p>
69       * If the array is size zero, the predicate always returns true.
70       * If the array is size one, then that predicate is returned.
71       *
72       * @param <T> the type that the predicate queries
73       * @param predicates  the predicates to check, cloned, not null
74       * @return the {@code all} predicate
75       * @throws NullPointerException if the predicates array is null
76       * @throws NullPointerException if any predicate in the array is null
77       */
78      public static <T> Predicate<T> allPredicate(final Predicate<? super T>... predicates) {
79          validate(predicates);
80          if (predicates.length == 0) {
81              return truePredicate();
82          }
83          if (predicates.length == 1) {
84              return coerce(predicates[0]);
85          }
86  
87          return new AllPredicate<>(FunctorUtils.copy(predicates));
88      }
89  
90      /**
91       * Constructor that performs no validation.
92       * Use {@code allPredicate} if you want that.
93       *
94       * @param predicates  the predicates to check, not cloned, not null
95       */
96      public AllPredicate(final Predicate<? super T>... predicates) {
97          super(predicates);
98      }
99  
100     /**
101      * Evaluates the predicate returning true if all predicates return true.
102      *
103      * @param object  the input object
104      * @return true if all decorated predicates return true
105      */
106     @Override
107     public boolean evaluate(final T object) {
108         for (final Predicate<? super T> iPredicate : iPredicates) {
109             if (!iPredicate.evaluate(object)) {
110                 return false;
111             }
112         }
113         return true;
114     }
115 
116 }