OrPredicate.java

  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. import java.io.Serializable;
  19. import java.util.Objects;

  20. import org.apache.commons.collections4.Predicate;

  21. /**
  22.  * Predicate implementation that returns true if either of the predicates return true.
  23.  *
  24.  * @param <T> the type of the input to the predicate.
  25.  * @since 3.0
  26.  */
  27. public final class OrPredicate<T> extends AbstractPredicate<T> implements PredicateDecorator<T>, Serializable {

  28.     /** Serial version UID */
  29.     private static final long serialVersionUID = -8791518325735182855L;

  30.     /**
  31.      * Creates the predicate.
  32.      *
  33.      * @param <T> the type that the predicate queries
  34.      * @param predicate1  the first predicate to check, not null
  35.      * @param predicate2  the second predicate to check, not null
  36.      * @return the {@code and} predicate
  37.      * @throws NullPointerException if either predicate is null
  38.      */
  39.     public static <T> Predicate<T> orPredicate(final Predicate<? super T> predicate1,
  40.                                                final Predicate<? super T> predicate2) {
  41.         return new OrPredicate<>(Objects.requireNonNull(predicate1, "predicate1"),
  42.                 Objects.requireNonNull(predicate2, "predicate2"));
  43.     }
  44.     /** The array of predicates to call */
  45.     private final Predicate<? super T> iPredicate1;

  46.     /** The array of predicates to call */
  47.     private final Predicate<? super T> iPredicate2;

  48.     /**
  49.      * Constructor that performs no validation.
  50.      * Use {@code orPredicate} if you want that.
  51.      *
  52.      * @param predicate1  the first predicate to check, not null
  53.      * @param predicate2  the second predicate to check, not null
  54.      */
  55.     public OrPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2) {
  56.         iPredicate1 = predicate1;
  57.         iPredicate2 = predicate2;
  58.     }

  59.     /**
  60.      * Gets the two predicates being decorated as an array.
  61.      *
  62.      * @return the predicates
  63.      * @since 3.1
  64.      */
  65.     @Override
  66.     @SuppressWarnings("unchecked")
  67.     public Predicate<? super T>[] getPredicates() {
  68.         return new Predicate[] {iPredicate1, iPredicate2};
  69.     }

  70.     /**
  71.      * Evaluates the predicate returning true if either predicate returns true.
  72.      *
  73.      * @param object  the input object
  74.      * @return true if either decorated predicate returns true
  75.      */
  76.     @Override
  77.     public boolean test(final T object) {
  78.         return iPredicate1.test(object) || iPredicate2.test(object);
  79.     }

  80. }