FailablePredicate.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.Predicate;

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

  29.     /** FALSE singleton */
  30.     @SuppressWarnings("rawtypes")
  31.     FailablePredicate FALSE = t -> false;

  32.     /** TRUE singleton */
  33.     @SuppressWarnings("rawtypes")
  34.     FailablePredicate TRUE = t -> true;

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

  46.     /**
  47.      * Returns The TRUE singleton.
  48.      *
  49.      * @param <T> Predicate type.
  50.      * @param <E> The kind of thrown exception or error.
  51.      * @return The NOP singleton.
  52.      */
  53.     @SuppressWarnings("unchecked")
  54.     static <T, E extends Throwable> FailablePredicate<T, E> truePredicate() {
  55.         return TRUE;
  56.     }

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

  68.     /**
  69.      * Returns a predicate that negates this predicate.
  70.      *
  71.      * @return a predicate that negates this predicate.
  72.      */
  73.     default FailablePredicate<T, E> negate() {
  74.         return t -> !test(t);
  75.     }

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

  87.     /**
  88.      * Tests the predicate.
  89.      *
  90.      * @param object the object to test the predicate on
  91.      * @return the predicate's evaluation
  92.      * @throws E if the predicate fails
  93.      */
  94.     boolean test(T object) throws E;
  95. }