java-commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingBiFunction.java
2022-04-28 20:52:32 +02:00

96 lines
3.8 KiB
Java

package io.gitlab.jfronny.commons.throwable;
import io.gitlab.jfronny.commons.tuple.Tuple;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Function;
@FunctionalInterface
public interface ThrowingBiFunction<T, U, R, TEx extends Throwable> {
R apply(T var1, U var2) throws TEx;
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingFunction<Tuple<T, U>, R, TEx> fromTuple() {
return (t) -> this.apply(t.left(), t.right());
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <V> ThrowingFunction<V, R, TEx> compose(@NotNull ThrowingFunction<? super V, ? extends T, ? extends TEx> left, @NotNull ThrowingFunction<? super V, ? extends U, ? extends TEx> right) {
Objects.requireNonNull(left);
Objects.requireNonNull(right);
return (t) -> this.apply(left.apply(t), right.apply(t));
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <V, W> ThrowingBiFunction<V, W, R, TEx> biCompose(@NotNull ThrowingFunction<? super V, ? extends T, ? extends TEx> left, @NotNull ThrowingFunction<? super W, ? extends U, ? extends TEx> right) {
Objects.requireNonNull(left);
Objects.requireNonNull(right);
return (l, r) -> this.apply(left.apply(l), right.apply(r));
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <V> ThrowingBiFunction<V, U, R, TEx> composeLeft(@NotNull ThrowingFunction<? super V, ? extends T, ? extends TEx> before) {
Objects.requireNonNull(before);
return (l, r) -> this.apply(before.apply(l), r);
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <V> ThrowingBiFunction<T, V, R, TEx> composeRight(@NotNull ThrowingFunction<? super V, ? extends U, ? extends TEx> before) {
Objects.requireNonNull(before);
return (l, r) -> this.apply(l, before.apply(r));
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <V> ThrowingBiFunction<T, U, V, TEx> andThen(@NotNull ThrowingFunction<? super R, ? extends V, ? extends TEx> after) {
Objects.requireNonNull(after);
return (t, u) -> after.apply(this.apply(t, u));
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingBiConsumer<T, U, TEx> andThen(@NotNull ThrowingConsumer<? super R, ? extends TEx> after) {
Objects.requireNonNull(after);
return (t, u) -> after.accept(this.apply(t, u));
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default BiFunction<T, U, R> addHandler(@NotNull Function<Throwable, ? extends R> handler) {
Objects.requireNonNull(handler);
return (t, u) -> {
try {
return this.apply(t, u);
} catch (Throwable e) {
return handler.apply(e);
}
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default BiFunction<T, U, R> addHandler(@NotNull Class<TEx> exception, @NotNull Function<TEx, ? extends R> handler) {
Objects.requireNonNull(handler);
return (t, u) -> {
try {
return this.apply(t, u);
} catch (Throwable e) {
if (exception.isAssignableFrom(e.getClass()))
return handler.apply((TEx) e);
else throw Try.runtimeException(e);
}
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default BiFunction<T, U, R> orThrow() {
return (t, u) -> {
try {
return this.apply(t, u);
} catch (Throwable e) {
throw Try.runtimeException(e);
}
};
}
}