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