LibJF/libjf-base/src/main/java/io/gitlab/jfronny/libjf/generic/ThrowingFunction.java

30 lines
915 B
Java

package io.gitlab.jfronny.libjf.generic;
import java.util.Objects;
import java.util.function.Function;
@FunctionalInterface
public interface ThrowingFunction<T, R, TEx extends Throwable> {
R apply(T var1) throws TEx;
default <V> ThrowingFunction<V, R, TEx> compose(ThrowingFunction<? super V, ? extends T, ? extends TEx> before) {
Objects.requireNonNull(before);
return (v) -> this.apply(before.apply(v));
}
default <V> ThrowingFunction<T, V, TEx> andThen(ThrowingFunction<? super R, ? extends V, ? extends TEx> after) {
Objects.requireNonNull(after);
return (t) -> after.apply(this.apply(t));
}
default Function<T, R> addHandler(Function<Throwable, R> handler) {
return (t) -> {
try {
return this.apply(t);
} catch (Throwable e) {
return handler.apply(e);
}
};
}
}