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    *      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.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      /** The array of predicates to call */
50      private final Predicate<? super T> iPredicate1;
51  
52      /** The array of predicates to call */
53      private final Predicate<? super T> iPredicate2;
54  
55      /**
56       * Constructor that performs no validation.
57       * Use {@code orPredicate} if you want that.
58       *
59       * @param predicate1  the first predicate to check, not null
60       * @param predicate2  the second predicate to check, not null
61       */
62      public OrPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2) {
63          iPredicate1 = predicate1;
64          iPredicate2 = predicate2;
65      }
66  
67      /**
68       * Gets the two predicates being decorated as an array.
69       *
70       * @return the predicates
71       * @since 3.1
72       */
73      @Override
74      @SuppressWarnings("unchecked")
75      public Predicate<? super T>[] getPredicates() {
76          return new Predicate[] {iPredicate1, iPredicate2};
77      }
78  
79      /**
80       * Evaluates the predicate returning true if either predicate returns true.
81       *
82       * @param object  the input object
83       * @return true if either decorated predicate returns true
84       */
85      @Override
86      public boolean test(final T object) {
87          return iPredicate1.test(object) || iPredicate2.test(object);
88      }
89  
90  }