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 * @param <T> the type of the input to the predicate.
37 * @since 3.0
38 */
39 public final class AllPredicate<T> extends AbstractQuantifierPredicate<T> {
40
41 /** Serial version UID */
42 private static final long serialVersionUID = -3094696765038308799L;
43
44 /**
45 * Creates the predicate.
46 * <p>
47 * If the collection is size zero, the predicate always returns true.
48 * If the collection is size one, then that predicate is returned.
49 * </p>
50 *
51 * @param <T> the type that the predicate queries
52 * @param predicates the predicates to check, cloned, not null
53 * @return the {@code all} predicate
54 * @throws NullPointerException if the predicates array is null
55 * @throws NullPointerException if any predicate in the array is null
56 */
57 public static <T> Predicate<T> allPredicate(final Collection<? extends Predicate<? super T>> predicates) {
58 final Predicate<? super T>[] preds = validate(predicates);
59 if (preds.length == 0) {
60 return truePredicate();
61 }
62 if (preds.length == 1) {
63 return coerce(preds[0]);
64 }
65 return new AllPredicate<>(preds);
66 }
67
68 /**
69 * Creates the predicate.
70 * <p>
71 * If the array is size zero, the predicate always returns true.
72 * If the array is size one, then that predicate is returned.
73 * </p>
74 *
75 * @param <T> the type that the predicate queries
76 * @param predicates the predicates to check, cloned, not null
77 * @return the {@code all} predicate
78 * @throws NullPointerException if the predicates array is null
79 * @throws NullPointerException if any predicate in the array is null
80 */
81 public static <T> Predicate<T> allPredicate(final Predicate<? super T>... predicates) {
82 validate(predicates);
83 if (predicates.length == 0) {
84 return truePredicate();
85 }
86 if (predicates.length == 1) {
87 return coerce(predicates[0]);
88 }
89 // <T> not needed in Eclipse but needed by the command line compiler
90 return new AllPredicate<T>(FunctorUtils.copy(predicates));
91 }
92
93 /**
94 * Constructor that performs no validation.
95 * Use {@code allPredicate} if you want that.
96 *
97 * @param predicates the predicates to check, not cloned, not null
98 */
99 public AllPredicate(final Predicate<? super T>... predicates) {
100 super(predicates);
101 }
102
103 /**
104 * Evaluates the predicate returning true if all predicates return true.
105 *
106 * @param object the input object
107 * @return true if all decorated predicates return true
108 */
109 @Override
110 public boolean test(final T object) {
111 for (final Predicate<? super T> iPredicate : iPredicates) {
112 if (!iPredicate.test(object)) {
113 return false;
114 }
115 }
116 return true;
117 }
118
119 }