[main] Allow logic compounds between ThrowingBooleanSupplier and ThrowingPredicate

This commit is contained in:
Johannes Frohnmeyer 2022-04-22 19:42:46 +02:00
parent c2681c29c5
commit 10d62e5a61
Signed by: Johannes
GPG Key ID: E76429612C2929F4
2 changed files with 61 additions and 4 deletions

View File

@ -8,6 +8,44 @@ import java.util.function.Predicate;
public interface ThrowingBooleanSupplier<TEx extends Throwable> {
boolean get() throws TEx;
default <T> ThrowingPredicate<T, TEx> and(ThrowingPredicate<? super T, ? extends TEx> other) {
Objects.requireNonNull(other);
return (t) -> this.get() && other.test(t);
}
default ThrowingBooleanSupplier<TEx> and(ThrowingBooleanSupplier<? extends TEx> other) {
Objects.requireNonNull(other);
return () -> this.get() && other.get();
}
default <T> ThrowingPredicate<T, TEx> or(ThrowingPredicate<? super T, ? extends TEx> other) {
Objects.requireNonNull(other);
return (t) -> this.get() || other.test(t);
}
default ThrowingBooleanSupplier<TEx> or(ThrowingBooleanSupplier<? extends TEx> other) {
Objects.requireNonNull(other);
return () -> this.get() || other.get();
}
default <T> ThrowingPredicate<T, TEx> xor(ThrowingPredicate<? super T, ? extends TEx> other) {
Objects.requireNonNull(other);
return (t) -> this.get() ^ other.test(t);
}
default ThrowingBooleanSupplier<TEx> xor(ThrowingBooleanSupplier<? extends TEx> other) {
Objects.requireNonNull(other);
return () -> this.get() ^ other.get();
}
default ThrowingBooleanSupplier<TEx> negate() {
return () -> !this.get();
}
static <TEx extends Throwable> ThrowingBooleanSupplier<TEx> not(ThrowingBooleanSupplier<TEx> target) {
return Objects.requireNonNull(target).negate();
}
default BooleanSupplier addHandler(Predicate<Throwable> handler) {
Objects.requireNonNull(handler);
return () -> {

View File

@ -22,8 +22,9 @@ public interface ThrowingPredicate<T, TEx extends Throwable> {
return (t) -> this.test(t) && other.test(t);
}
default ThrowingPredicate<T, TEx> negate() {
return (t) -> !this.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) {
@ -31,13 +32,31 @@ public interface ThrowingPredicate<T, TEx extends Throwable> {
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) {
Objects.requireNonNull(target);
return target.negate();
return Objects.requireNonNull(target).negate();
}
default Predicate<T> addHandler(Predicate<Throwable> handler) {