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
21 import org.apache.commons.collections.Predicate;
22
23 /**
24 * Predicate implementation that returns true if either of the predicates return true.
25 *
26 * @since 3.0
27 * @version $Id: OrPredicate.java 1435965 2013-01-20 21:13:18Z tn $
28 */
29 public final class OrPredicate<T> implements Predicate<T>, PredicateDecorator<T>, Serializable {
30
31 /** Serial version UID */
32 private static final long serialVersionUID = -8791518325735182855L;
33
34 /** The array of predicates to call */
35 private final Predicate<? super T> iPredicate1;
36 /** The array of predicates to call */
37 private final Predicate<? super T> iPredicate2;
38
39 /**
40 * Factory to create the predicate.
41 *
42 * @param <T> the type that the predicate queries
43 * @param predicate1 the first predicate to check, not null
44 * @param predicate2 the second predicate to check, not null
45 * @return the <code>and</code> predicate
46 * @throws IllegalArgumentException if either predicate is null
47 */
48 public static <T> Predicate<T> orPredicate(final Predicate<? super T> predicate1,
49 final Predicate<? super T> predicate2) {
50 if (predicate1 == null || predicate2 == null) {
51 throw new IllegalArgumentException("Predicate must not be null");
52 }
53 return new OrPredicate<T>(predicate1, predicate2);
54 }
55
56 /**
57 * Constructor that performs no validation.
58 * Use <code>getInstance</code> if you want that.
59 *
60 * @param predicate1 the first predicate to check, not null
61 * @param predicate2 the second predicate to check, not null
62 */
63 public OrPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2) {
64 super();
65 iPredicate1 = predicate1;
66 iPredicate2 = predicate2;
67 }
68
69 /**
70 * Evaluates the predicate returning true if either predicate returns true.
71 *
72 * @param object the input object
73 * @return true if either decorated predicate returns true
74 */
75 public boolean evaluate(final T object) {
76 return iPredicate1.evaluate(object) || iPredicate2.evaluate(object);
77 }
78
79 /**
80 * Gets the two predicates being decorated as an array.
81 *
82 * @return the predicates
83 * @since 3.1
84 */
85 @SuppressWarnings("unchecked")
86 public Predicate<? super T>[] getPredicates() {
87 return new Predicate[] {iPredicate1, iPredicate2};
88 }
89
90 }