FailableFunction.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.Function;

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

  30.     /** NOP singleton */
  31.     @SuppressWarnings("rawtypes")
  32.     FailableFunction NOP = t -> null;

  33.     /**
  34.      * Starts a fluent chain like {@code function(foo::bar).andThen(...).andThen(...).apply(...);}
  35.      *
  36.      * @param <T> Input type.
  37.      * @param <R> Return type.
  38.      * @param <E> The kind of thrown exception or error.
  39.      * @param function   the argument to return.
  40.      * @return the argument
  41.      * @since 3.14.0
  42.      */
  43.     static <T, R, E extends Throwable> FailableFunction<T, R, E> function(final FailableFunction<T, R, E> function) {
  44.         return function;
  45.     }

  46.     /**
  47.      * Returns a function that always returns its input argument.
  48.      *
  49.      * @param <T> the type of the input and output objects to the function
  50.      * @param <E> The kind of thrown exception or error.
  51.      * @return a function that always returns its input argument
  52.      */
  53.     static <T, E extends Throwable> FailableFunction<T, T, E> identity() {
  54.         return t -> t;
  55.     }

  56.     /**
  57.      * Returns The NOP singleton.
  58.      *
  59.      * @param <T> Consumed type.
  60.      * @param <R> Return type.
  61.      * @param <E> The kind of thrown exception or error.
  62.      * @return The NOP singleton.
  63.      */
  64.     @SuppressWarnings("unchecked")
  65.     static <T, R, E extends Throwable> FailableFunction<T, R, E> nop() {
  66.         return NOP;
  67.     }

  68.     /**
  69.      * Returns a composed {@link FailableFunction} like {@link Function#andThen(Function)}.
  70.      *
  71.      * @param <V> the output type of the {@code after} function, and of the composed function.
  72.      * @return a composed {@link FailableFunction} like {@link Function#andThen(Function)}.
  73.      * @param after the operation to perform after this one.
  74.      * @throws NullPointerException when {@code after} is null.
  75.      */
  76.     default <V> FailableFunction<T, V, E> andThen(final FailableFunction<? super R, ? extends V, E> after) {
  77.         Objects.requireNonNull(after);
  78.         return (final T t) -> after.apply(apply(t));
  79.     }

  80.     /**
  81.      * Applies this function.
  82.      *
  83.      * @param input the input for the function
  84.      * @return the result of the function
  85.      * @throws E Thrown when the function fails.
  86.      */
  87.     R apply(T input) throws E;

  88.     /**
  89.      * Returns a composed {@link FailableFunction} like {@link Function#compose(Function)}.
  90.      *
  91.      * @param <V> the input type to the {@code before} function, and to the composed function.
  92.      * @param before the operator to apply before this one.
  93.      * @return a composed {@link FailableFunction} like {@link Function#compose(Function)}.
  94.      * @throws NullPointerException if before is null.
  95.      * @see #andThen(FailableFunction)
  96.      */
  97.     default <V> FailableFunction<V, R, E> compose(final FailableFunction<? super V, ? extends T, E> before) {
  98.         Objects.requireNonNull(before);
  99.         return (final V v) -> apply(before.apply(v));
  100.     }
  101. }