83 lines
2.7 KiB
Java
83 lines
2.7 KiB
Java
package io.gitlab.jfronny.libjf.generic;
|
|
|
|
import java.util.Objects;
|
|
import java.util.function.Predicate;
|
|
|
|
@FunctionalInterface
|
|
public interface ThrowingPredicate<T, TEx extends Throwable> {
|
|
boolean test(T var1) throws TEx;
|
|
|
|
default <V> ThrowingPredicate<V, TEx> compose(ThrowingFunction<? super V, ? extends T, ? extends TEx> before) {
|
|
Objects.requireNonNull(before);
|
|
return (t) -> this.test(before.apply(t));
|
|
}
|
|
|
|
default ThrowingBooleanSupplier<TEx> compose(ThrowingSupplier<? extends T, ? extends TEx> before) {
|
|
Objects.requireNonNull(before);
|
|
return () -> this.test(before.get());
|
|
}
|
|
|
|
default ThrowingPredicate<T, TEx> and(ThrowingPredicate<? super T, ? extends TEx> other) {
|
|
Objects.requireNonNull(other);
|
|
return (t) -> this.test(t) && other.test(t);
|
|
}
|
|
|
|
default ThrowingPredicate<T, TEx> and(ThrowingBooleanSupplier<? extends TEx> other) {
|
|
Objects.requireNonNull(other);
|
|
return (t) -> this.test(t) && other.get();
|
|
}
|
|
|
|
default ThrowingPredicate<T, TEx> or(ThrowingPredicate<? super T, ? extends TEx> other) {
|
|
Objects.requireNonNull(other);
|
|
return (t) -> this.test(t) || other.test(t);
|
|
}
|
|
|
|
default ThrowingPredicate<T, TEx> or(ThrowingBooleanSupplier<? extends TEx> other) {
|
|
Objects.requireNonNull(other);
|
|
return (t) -> this.test(t) || other.get();
|
|
}
|
|
|
|
default ThrowingPredicate<T, TEx> xor(ThrowingPredicate<? super T, ? extends TEx> other) {
|
|
Objects.requireNonNull(other);
|
|
return (t) -> this.test(t) ^ other.test(t);
|
|
}
|
|
|
|
default ThrowingPredicate<T, TEx> xor(ThrowingBooleanSupplier<? extends TEx> other) {
|
|
Objects.requireNonNull(other);
|
|
return (t) -> this.test(t) ^ other.get();
|
|
}
|
|
|
|
default ThrowingPredicate<T, TEx> negate() {
|
|
return (t) -> !this.test(t);
|
|
}
|
|
|
|
static <T> ThrowingPredicate<T, Throwable> isEqual(Object targetRef) {
|
|
return null == targetRef ? Objects::isNull : targetRef::equals;
|
|
}
|
|
|
|
static <T, TEx extends Throwable> ThrowingPredicate<T, TEx> not(ThrowingPredicate<T, TEx> target) {
|
|
return Objects.requireNonNull(target).negate();
|
|
}
|
|
|
|
default Predicate<T> addHandler(Predicate<Throwable> handler) {
|
|
Objects.requireNonNull(handler);
|
|
return (r) -> {
|
|
try {
|
|
return this.test(r);
|
|
} catch (Throwable e) {
|
|
return handler.test(e);
|
|
}
|
|
};
|
|
}
|
|
|
|
default Predicate<T> orThrow() {
|
|
return (r) -> {
|
|
try {
|
|
return this.test(r);
|
|
} catch (Throwable e) {
|
|
throw Try.runtimeException(e);
|
|
}
|
|
};
|
|
}
|
|
}
|