FailableBiPredicate.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.lang3.function;

  18. import java.util.Objects;
  19. import java.util.function.BiPredicate;

  20. /**
  21.  * A functional interface like {@link BiPredicate} that declares a {@link Throwable}.
  22.  *
  23.  * @param <T> Predicate type 1.
  24.  * @param <U> Predicate type 2.
  25.  * @param <E> The kind of thrown exception or error.
  26.  * @since 3.11
  27.  */
  28. @FunctionalInterface
  29. public interface FailableBiPredicate<T, U, E extends Throwable> {

  30.     /** FALSE singleton */
  31.     @SuppressWarnings("rawtypes")
  32.     FailableBiPredicate FALSE = (t, u) -> false;

  33.     /** TRUE singleton */
  34.     @SuppressWarnings("rawtypes")
  35.     FailableBiPredicate TRUE = (t, u) -> true;

  36.     /**
  37.      * Returns The FALSE singleton.
  38.      *
  39.      * @param <T> Consumed type 1.
  40.      * @param <U> Consumed type 2.
  41.      * @param <E> The kind of thrown exception or error.
  42.      * @return The NOP singleton.
  43.      */
  44.     @SuppressWarnings("unchecked")
  45.     static <T, U, E extends Throwable> FailableBiPredicate<T, U, E> falsePredicate() {
  46.         return FALSE;
  47.     }

  48.     /**
  49.      * Returns The TRUE singleton.
  50.      *
  51.      * @param <T> Consumed type 1.
  52.      * @param <U> Consumed type 2.
  53.      * @param <E> The kind of thrown exception or error.
  54.      * @return The NOP singleton.
  55.      */
  56.     @SuppressWarnings("unchecked")
  57.     static <T, U, E extends Throwable> FailableBiPredicate<T, U, E> truePredicate() {
  58.         return TRUE;
  59.     }

  60.     /**
  61.      * Returns a composed {@link FailableBiPredicate} like {@link BiPredicate#and(BiPredicate)}.
  62.      *
  63.      * @param other a predicate that will be logically-ANDed with this predicate.
  64.      * @return a composed {@link FailableBiPredicate} like {@link BiPredicate#and(BiPredicate)}.
  65.      * @throws NullPointerException if other is null
  66.      */
  67.     default FailableBiPredicate<T, U, E> and(final FailableBiPredicate<? super T, ? super U, E> other) {
  68.         Objects.requireNonNull(other);
  69.         return (final T t, final U u) -> test(t, u) && other.test(t, u);
  70.     }

  71.     /**
  72.      * Returns a predicate that negates this predicate.
  73.      *
  74.      * @return a predicate that negates this predicate.
  75.      */
  76.     default FailableBiPredicate<T, U, E> negate() {
  77.         return (final T t, final U u) -> !test(t, u);
  78.     }

  79.     /**
  80.      * Returns a composed {@link FailableBiPredicate} like {@link BiPredicate#and(BiPredicate)}.
  81.      *
  82.      * @param other a predicate that will be logically-ORed with this predicate.
  83.      * @return a composed {@link FailableBiPredicate} like {@link BiPredicate#and(BiPredicate)}.
  84.      * @throws NullPointerException if other is null
  85.      */
  86.     default FailableBiPredicate<T, U, E> or(final FailableBiPredicate<? super T, ? super U, E> other) {
  87.         Objects.requireNonNull(other);
  88.         return (final T t, final U u) -> test(t, u) || other.test(t, u);
  89.     }

  90.     /**
  91.      * Tests the predicate.
  92.      *
  93.      * @param object1 the first object to test the predicate on
  94.      * @param object2 the second object to test the predicate on
  95.      * @return the predicate's evaluation
  96.      * @throws E Thrown when this predicate fails.
  97.      */
  98.     boolean test(T object1, U object2) throws E;
  99. }