FailableIntPredicate.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.IntPredicate;

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

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

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

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

  44.     /**
  45.      * Returns The TRUE singleton.
  46.      *
  47.      * @param <E> The kind of thrown exception or error.
  48.      * @return The NOP singleton.
  49.      */
  50.     @SuppressWarnings("unchecked")
  51.     static <E extends Throwable> FailableIntPredicate<E> truePredicate() {
  52.         return TRUE;
  53.     }

  54.     /**
  55.      * Returns a composed {@link FailableIntPredicate} like {@link IntPredicate#and(IntPredicate)}.
  56.      *
  57.      * @param other a predicate that will be logically-ANDed with this predicate.
  58.      * @return a composed {@link FailableIntPredicate} like {@link IntPredicate#and(IntPredicate)}.
  59.      * @throws NullPointerException if other is null
  60.      */
  61.     default FailableIntPredicate<E> and(final FailableIntPredicate<E> other) {
  62.         Objects.requireNonNull(other);
  63.         return t -> test(t) && other.test(t);
  64.     }

  65.     /**
  66.      * Returns a predicate that negates this predicate.
  67.      *
  68.      * @return a predicate that negates this predicate.
  69.      */
  70.     default FailableIntPredicate<E> negate() {
  71.         return t -> !test(t);
  72.     }

  73.     /**
  74.      * Returns a composed {@link FailableIntPredicate} like {@link IntPredicate#and(IntPredicate)}.
  75.      *
  76.      * @param other a predicate that will be logically-ORed with this predicate.
  77.      * @return a composed {@link FailableIntPredicate} like {@link IntPredicate#and(IntPredicate)}.
  78.      * @throws NullPointerException if other is null
  79.      */
  80.     default FailableIntPredicate<E> or(final FailableIntPredicate<E> other) {
  81.         Objects.requireNonNull(other);
  82.         return t -> test(t) || other.test(t);
  83.     }

  84.     /**
  85.      * Tests the predicate.
  86.      *
  87.      * @param value the parameter for the predicate to accept.
  88.      * @return {@code true} if the input argument matches the predicate, {@code false} otherwise.
  89.      * @throws E Thrown when the consumer fails.
  90.      */
  91.     boolean test(int value) throws E;
  92. }