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