FailableBiFunction.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.BiFunction;
  20. import java.util.function.Function;

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

  32.     /** NOP singleton */
  33.     @SuppressWarnings("rawtypes")
  34.     FailableBiFunction NOP = (t, u) -> null;

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

  48.     /**
  49.      * Returns a composed {@link FailableBiFunction} that like {@link BiFunction#andThen(Function)}.
  50.      *
  51.      * @param <V> the output type of the {@code after} function, and of the composed function.
  52.      * @param after the operation to perform after this one.
  53.      * @return a composed {@link FailableBiFunction} that like {@link BiFunction#andThen(Function)}.
  54.      * @throws NullPointerException when {@code after} is null.
  55.      */
  56.     default <V> FailableBiFunction<T, U, V, E> andThen(final FailableFunction<? super R, ? extends V, E> after) {
  57.         Objects.requireNonNull(after);
  58.         return (final T t, final U u) -> after.apply(apply(t, u));
  59.     }

  60.     /**
  61.      * Applies this function.
  62.      *
  63.      * @param input1 the first input for the function
  64.      * @param input2 the second input for the function
  65.      * @return the result of the function
  66.      * @throws E Thrown when the function fails.
  67.      */
  68.     R apply(T input1, U input2) throws E;
  69. }