    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.lang3.function;
    019
    020import java.util.Objects;
    021import java.util.function.Function;
    022
    023/**
    024 * A functional interface like {@link Function} that declares a {@code Throwable}.
    025 *
    026 * @param <T> Input type 1.
    027 * @param <R> Return type.
    028 * @param <E> Thrown exception.
    029 * @since 3.11
    030 */
    031@FunctionalInterface
    032public interface FailableFunction<T, R, E extends Throwable> {
    033
    034    /** NOP singleton */
    035    @SuppressWarnings("rawtypes")
    036    FailableFunction NOP = t -> null;
    037
    038    /**
    039     * Returns a function that always returns its input argument.
    040     *
    041     * @param <T> the type of the input and output objects to the function
    042     * @param <E> Thrown exception.
    043     * @return a function that always returns its input argument
    044     */
    045    static <T, E extends Throwable> FailableFunction<T, T, E> identity() {
    046        return t -> t;
    047    }
    048
    049    /**
    050     * Returns The NOP singleton.
    051     *
    052     * @param <T> Consumed type 1.
    053     * @param <R> Return type.
    054     * @param <E> Thrown exception.
    055     * @return The NOP singleton.
    056     */
    057    static <T, R, E extends Throwable> FailableFunction<T, R, E> nop() {
    058        return NOP;
    059    }
    060
    061    /**
    062     * Returns a composed {@code FailableFunction} like {@link Function#andThen(Function)}.
    063     *
    064     * @param <V> the output type of the {@code after} function, and of the composed function.
    065     * @return a composed {@code FailableFunction} like {@link Function#andThen(Function)}.
    066     * @param after the operation to perform after this one.
    067     * @throws NullPointerException when {@code after} is null.
    068     */
    069    default <V> FailableFunction<T, V, E> andThen(final FailableFunction<? super R, ? extends V, E> after) {
    070        Objects.requireNonNull(after);
    071        return (final T t) -> after.apply(apply(t));
    072    }
    073
    074    /**
    075     * Applies this function.
    076     *
    077     * @param input the input for the function
    078     * @return the result of the function
    079     * @throws E Thrown when the function fails.
    080     */
    081    R apply(T input) throws E;
    082
    083    /**
    084     * Returns a composed {@code FailableFunction} like {@link Function#compose(Function)}.
    085     *
    086     * @param <V> the input type to the {@code before} function, and to the composed function.
    087     * @param before the operator to apply before this one.
    088     * @return a a composed {@code FailableFunction} like {@link Function#compose(Function)}.
    089     * @throws NullPointerException if before is null.
    090     * @see #andThen(FailableFunction)
    091     */
    092    default <V> FailableFunction<V, R, E> compose(final FailableFunction<? super V, ? extends T, E> before) {
    093        Objects.requireNonNull(before);
    094        return (final V v) -> apply(before.apply(v));
    095    }
    096}