115 lines
4.2 KiB
Java
115 lines
4.2 KiB
Java
package io.gitlab.jfronny.commons.throwable;
|
|
|
|
import org.jetbrains.annotations.ApiStatus;
|
|
import org.jetbrains.annotations.Contract;
|
|
import org.jetbrains.annotations.NotNull;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import java.util.Objects;
|
|
import java.util.function.Predicate;
|
|
|
|
@FunctionalInterface
|
|
public interface ThrowingPredicate<T, TEx extends Throwable> {
|
|
boolean test(T var1) throws TEx;
|
|
|
|
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
|
|
default <V> ThrowingPredicate<V, TEx> compose(@NotNull ThrowingFunction<? super V, ? extends T, ? extends TEx> before) {
|
|
Objects.requireNonNull(before);
|
|
return (t) -> this.test(before.apply(t));
|
|
}
|
|
|
|
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
|
|
default ThrowingBooleanSupplier<TEx> compose(@NotNull ThrowingSupplier<? extends T, ? extends TEx> before) {
|
|
Objects.requireNonNull(before);
|
|
return () -> this.test(before.get());
|
|
}
|
|
|
|
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
|
|
default ThrowingPredicate<T, TEx> and(@NotNull ThrowingPredicate<? super T, ? extends TEx> other) {
|
|
Objects.requireNonNull(other);
|
|
return (t) -> this.test(t) && other.test(t);
|
|
}
|
|
|
|
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
|
|
default ThrowingPredicate<T, TEx> and(@NotNull ThrowingBooleanSupplier<? extends TEx> other) {
|
|
Objects.requireNonNull(other);
|
|
return (t) -> this.test(t) && other.get();
|
|
}
|
|
|
|
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
|
|
default ThrowingPredicate<T, TEx> or(@NotNull ThrowingPredicate<? super T, ? extends TEx> other) {
|
|
Objects.requireNonNull(other);
|
|
return (t) -> this.test(t) || other.test(t);
|
|
}
|
|
|
|
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
|
|
default ThrowingPredicate<T, TEx> or(@NotNull ThrowingBooleanSupplier<? extends TEx> other) {
|
|
Objects.requireNonNull(other);
|
|
return (t) -> this.test(t) || other.get();
|
|
}
|
|
|
|
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
|
|
default ThrowingPredicate<T, TEx> xor(@NotNull ThrowingPredicate<? super T, ? extends TEx> other) {
|
|
Objects.requireNonNull(other);
|
|
return (t) -> this.test(t) ^ other.test(t);
|
|
}
|
|
|
|
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
|
|
default ThrowingPredicate<T, TEx> xor(@NotNull ThrowingBooleanSupplier<? extends TEx> other) {
|
|
Objects.requireNonNull(other);
|
|
return (t) -> this.test(t) ^ other.get();
|
|
}
|
|
|
|
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
|
|
default ThrowingPredicate<T, TEx> negate() {
|
|
return (t) -> !this.test(t);
|
|
}
|
|
|
|
@Contract(pure = true) @NotNull
|
|
static <T> ThrowingPredicate<T, Throwable> isEqual(@Nullable Object targetRef) {
|
|
return null == targetRef ? Objects::isNull : targetRef::equals;
|
|
}
|
|
|
|
@Contract(pure = true) @NotNull
|
|
static <T, TEx extends Throwable> ThrowingPredicate<T, TEx> not(@NotNull ThrowingPredicate<T, TEx> target) {
|
|
return Objects.requireNonNull(target).negate();
|
|
}
|
|
|
|
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
|
|
default Predicate<T> addHandler(@NotNull Predicate<Throwable> handler) {
|
|
Objects.requireNonNull(handler);
|
|
return (r) -> {
|
|
try {
|
|
return this.test(r);
|
|
} catch (Throwable e) {
|
|
return handler.test(e);
|
|
}
|
|
};
|
|
}
|
|
|
|
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
|
|
default Predicate<T> addHandler(@NotNull Class<TEx> exception, @NotNull Predicate<TEx> handler) {
|
|
Objects.requireNonNull(handler);
|
|
return (r) -> {
|
|
try {
|
|
return this.test(r);
|
|
} 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 Predicate<T> orThrow() {
|
|
return (r) -> {
|
|
try {
|
|
return this.test(r);
|
|
} catch (Throwable e) {
|
|
throw Try.runtimeException(e);
|
|
}
|
|
};
|
|
}
|
|
}
|