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