java-commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingBooleanSupplier.java

98 lines
3.4 KiB
Java

package io.gitlab.jfronny.commons.throwable;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import java.util.function.BooleanSupplier;
import java.util.function.Predicate;
@FunctionalInterface
public interface ThrowingBooleanSupplier<TEx extends Throwable> {
boolean get() throws TEx;
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <T> ThrowingPredicate<T, TEx> and(@NotNull ThrowingPredicate<? super T, ? extends TEx> other) {
Objects.requireNonNull(other);
return (t) -> this.get() && other.test(t);
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingBooleanSupplier<TEx> and(@NotNull ThrowingBooleanSupplier<? extends TEx> other) {
Objects.requireNonNull(other);
return () -> this.get() && other.get();
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <T> ThrowingPredicate<T, TEx> or(@NotNull ThrowingPredicate<? super T, ? extends TEx> other) {
Objects.requireNonNull(other);
return (t) -> this.get() || other.test(t);
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingBooleanSupplier<TEx> or(@NotNull ThrowingBooleanSupplier<? extends TEx> other) {
Objects.requireNonNull(other);
return () -> this.get() || other.get();
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <T> ThrowingPredicate<T, TEx> xor(@NotNull ThrowingPredicate<? super T, ? extends TEx> other) {
Objects.requireNonNull(other);
return (t) -> this.get() ^ other.test(t);
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingBooleanSupplier<TEx> xor(@NotNull ThrowingBooleanSupplier<? extends TEx> other) {
Objects.requireNonNull(other);
return () -> this.get() ^ other.get();
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingBooleanSupplier<TEx> negate() {
return () -> !this.get();
}
@Contract(pure = true) @NotNull
static <TEx extends Throwable> ThrowingBooleanSupplier<TEx> not(@NotNull ThrowingBooleanSupplier<TEx> target) {
return Objects.requireNonNull(target).negate();
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default BooleanSupplier addHandler(@NotNull Predicate<Throwable> handler) {
Objects.requireNonNull(handler);
return () -> {
try {
return this.get();
} catch (Throwable e) {
return handler.test(e);
}
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default BooleanSupplier addHandler(@NotNull Class<TEx> exception, @NotNull Predicate<TEx> handler) {
Objects.requireNonNull(handler);
return () -> {
try {
return this.get();
} catch (Throwable e) {
if (exception.isAssignableFrom(e.getClass()))
return handler.test((TEx) e);
else throw Try.runtimeException(e);
}
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default BooleanSupplier orThrow() {
return () -> {
try {
return this.get();
} catch (Throwable e) {
throw Try.runtimeException(e);
}
};
}
}