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 {@link Throwable}.
025 *
026 * @param <T> Input type 1.
027 * @param <R> Return type.
028 * @param <E> The kind of thrown exception or error.
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> The kind of thrown exception or error.
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> The kind of thrown exception or error.
055     * @return The NOP singleton.
056     */
057    @SuppressWarnings("unchecked")
058    static <T, R, E extends Throwable> FailableFunction<T, R, E> nop() {
059        return NOP;
060    }
061
062    /**
063     * Returns a composed {@link FailableFunction} like {@link Function#andThen(Function)}.
064     *
065     * @param <V> the output type of the {@code after} function, and of the composed function.
066     * @return a composed {@link FailableFunction} like {@link Function#andThen(Function)}.
067     * @param after the operation to perform after this one.
068     * @throws NullPointerException when {@code after} is null.
069     */
070    default <V> FailableFunction<T, V, E> andThen(final FailableFunction<? super R, ? extends V, E> after) {
071        Objects.requireNonNull(after);
072        return (final T t) -> after.apply(apply(t));
073    }
074
075    /**
076     * Applies this function.
077     *
078     * @param input the input for the function
079     * @return the result of the function
080     * @throws E Thrown when the function fails.
081     */
082    R apply(T input) throws E;
083
084    /**
085     * Returns a composed {@link FailableFunction} like {@link Function#compose(Function)}.
086     *
087     * @param <V> the input type to the {@code before} function, and to the composed function.
088     * @param before the operator to apply before this one.
089     * @return a composed {@link FailableFunction} like {@link Function#compose(Function)}.
090     * @throws NullPointerException if before is null.
091     * @see #andThen(FailableFunction)
092     */
093    default <V> FailableFunction<V, R, E> compose(final FailableFunction<? super V, ? extends T, E> before) {
094        Objects.requireNonNull(before);
095        return (final V v) -> apply(before.apply(v));
096    }
097}