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