102 lines
3.8 KiB
Java
102 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.BiConsumer;
|
|
import java.util.function.Consumer;
|
|
|
|
@FunctionalInterface
|
|
public interface ThrowingBiConsumer<T, U, TEx extends Throwable> {
|
|
void accept(T var1, U var2) throws TEx;
|
|
|
|
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
|
|
default ThrowingConsumer<Tuple<T, U>, TEx> fromTuple() {
|
|
return (t) -> this.accept(t.left(), t.right());
|
|
}
|
|
|
|
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
|
|
default <V> ThrowingConsumer<V, ? super 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.accept(left.apply(t), right.apply(t));
|
|
}
|
|
|
|
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
|
|
default <V, W> ThrowingBiConsumer<V, W, 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.accept(left.apply(l), right.apply(r));
|
|
}
|
|
|
|
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
|
|
default <V> ThrowingBiConsumer<V, U, TEx> composeLeft(@NotNull ThrowingFunction<? super V, ? extends T, ? extends TEx> before) {
|
|
Objects.requireNonNull(before);
|
|
return (l, r) -> this.accept(before.apply(l), r);
|
|
}
|
|
|
|
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
|
|
default <V> ThrowingBiConsumer<T, V, TEx> composeRight(@NotNull ThrowingFunction<? super V, ? extends U, ? extends TEx> before) {
|
|
Objects.requireNonNull(before);
|
|
return (l, r) -> this.accept(l, before.apply(r));
|
|
}
|
|
|
|
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
|
|
default ThrowingBiConsumer<T, U, TEx> andThen(@NotNull ThrowingBiConsumer<? super T, ? super U, ? extends TEx> after) {
|
|
Objects.requireNonNull(after);
|
|
return (l, r) -> {
|
|
this.accept(l, r);
|
|
after.accept(l, r);
|
|
};
|
|
}
|
|
|
|
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
|
|
default ThrowingBiConsumer<T, U, TEx> andThen(@NotNull ThrowingRunnable<? extends TEx> after) {
|
|
Objects.requireNonNull(after);
|
|
return (l, r) -> {
|
|
this.accept(l, r);
|
|
after.run();
|
|
};
|
|
}
|
|
|
|
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
|
|
default BiConsumer<T, U> addHandler(@NotNull Consumer<Throwable> handler) {
|
|
Objects.requireNonNull(handler);
|
|
return (l, r) -> {
|
|
try {
|
|
this.accept(l, r);
|
|
} catch (Throwable e) {
|
|
handler.accept(e);
|
|
}
|
|
};
|
|
}
|
|
|
|
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
|
|
default BiConsumer<T, U> addHandler(@NotNull Class<TEx> exception, @NotNull Consumer<TEx> handler) {
|
|
Objects.requireNonNull(handler);
|
|
return (l, r) -> {
|
|
try {
|
|
this.accept(l, r);
|
|
} catch (Throwable e) {
|
|
if (exception.isAssignableFrom(e.getClass()))
|
|
handler.accept((TEx) e);
|
|
else throw Try.runtimeException(e);
|
|
}
|
|
};
|
|
}
|
|
|
|
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
|
|
default BiConsumer<T, U> orThrow() {
|
|
return (l, r) -> {
|
|
try {
|
|
this.accept(l, r);
|
|
} catch (Throwable e) {
|
|
throw Try.runtimeException(e);
|
|
}
|
|
};
|
|
}
|
|
}
|