Source code
Description: source: package: org.apache.commons.lang3.function, interface: FailableFunction

    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     * Starts a fluent chain like {@code function(foo::bar).andThen(...).andThen(...).apply(...);}
    040     *
    041     * @param <T> Input type.
    042     * @param <R> Return type.
    043     * @param <E> The kind of thrown exception or error.
    044     * @param function   the argument to return.
    045     * @return the argument
    046     * @since 3.14.0
    047     */
    048    static <T, R, E extends Throwable> FailableFunction<T, R, E> function(final FailableFunction<T, R, E> function) {
    049        return function;
    050    }
    051
    052    /**
    053     * Returns a function that always returns its input argument.
    054     *
    055     * @param <T> the type of the input and output objects to the function
    056     * @param <E> The kind of thrown exception or error.
    057     * @return a function that always returns its input argument
    058     */
    059    static <T, E extends Throwable> FailableFunction<T, T, E> identity() {
    060        return t -> t;
    061    }
    062
    063    /**
    064     * Returns The NOP singleton.
    065     *
    066     * @param <T> Consumed type.
    067     * @param <R> Return type.
    068     * @param <E> The kind of thrown exception or error.
    069     * @return The NOP singleton.
    070     */
    071    @SuppressWarnings("unchecked")
    072    static <T, R, E extends Throwable> FailableFunction<T, R, E> nop() {
    073        return NOP;
    074    }
    075
    076    /**
    077     * Returns a composed {@link FailableFunction} like {@link Function#andThen(Function)}.
    078     *
    079     * @param <V> the output type of the {@code after} function, and of the composed function.
    080     * @return a composed {@link FailableFunction} like {@link Function#andThen(Function)}.
    081     * @param after the operation to perform after this one.
    082     * @throws NullPointerException when {@code after} is null.
    083     */
    084    default <V> FailableFunction<T, V, E> andThen(final FailableFunction<? super R, ? extends V, E> after) {
    085        Objects.requireNonNull(after);
    086        return (final T t) -> after.apply(apply(t));
    087    }
    088
    089    /**
    090     * Applies this function.
    091     *
    092     * @param input the input for the function
    093     * @return the result of the function
    094     * @throws E Thrown when the function fails.
    095     */
    096    R apply(T input) throws E;
    097
    098    /**
    099     * Returns a composed {@link FailableFunction} like {@link Function#compose(Function)}.
    100     *
    101     * @param <V> the input type to the {@code before} function, and to the composed function.
    102     * @param before the operator to apply before this one.
    103     * @return a composed {@link FailableFunction} like {@link Function#compose(Function)}.
    104     * @throws NullPointerException if before is null.
    105     * @see #andThen(FailableFunction)
    106     */
    107    default <V> FailableFunction<V, R, E> compose(final FailableFunction<? super V, ? extends T, E> before) {
    108        Objects.requireNonNull(before);
    109        return (final V v) -> apply(before.apply(v));
    110    }
    111}