package org.xbib.logging.expression;

/**
 * A one-argument function which can throw an exception.
 */
@FunctionalInterface
public interface ExceptionFunction<T, R, E extends Exception> {
    /**
     * Applies this function to the given arguments.
     *
     * @param t the argument
     * @return the function result
     * @throws E if an exception occurs
     */
    R apply(T t) throws E;

    default <R2> ExceptionFunction<T, R2, E> andThen(ExceptionFunction<? super R, ? extends R2, ? extends E> after) {
        return t -> after.apply(apply(t));
    }

    default <R2> ExceptionFunction<T, R2, E> andThen(
            ExceptionBiFunction<? super T, ? super R, ? extends R2, ? extends E> after) {
        return t -> after.apply(t, apply(t));
    }

    @SuppressWarnings("overloads")
    default <T2> ExceptionFunction<T2, R, E> compose(ExceptionFunction<? super T2, ? extends T, ? extends E> before) {
        return t -> apply(before.apply(t));
    }

    @SuppressWarnings("overloads")
    default ExceptionConsumer<T, E> andThen(ExceptionConsumer<? super R, ? extends E> after) {
        return t -> after.accept(apply(t));
    }

    @SuppressWarnings("overloads")
    default ExceptionConsumer<T, E> andThen(ExceptionBiConsumer<? super T, ? super R, ? extends E> after) {
        return t -> after.accept(t, apply(t));
    }

    @SuppressWarnings("overloads")
    default ExceptionPredicate<T, E> andThen(ExceptionPredicate<? super R, ? extends E> after) {
        return t -> after.test(apply(t));
    }

    @SuppressWarnings("overloads")
    default ExceptionPredicate<T, E> andThen(ExceptionBiPredicate<? super T, ? super R, ? extends E> after) {
        return t -> after.test(t, apply(t));
    }

    default ExceptionSupplier<R, E> compose(ExceptionSupplier<? extends T, ? extends E> before) {
        return () -> apply(before.get());
    }
}
