aQute.lib.exceptions.PredicateWithException Maven / Gradle / Ivy
package aQute.lib.exceptions;
import static java.util.Objects.requireNonNull;
import java.util.function.BooleanSupplier;
import java.util.function.Predicate;
/**
* Predicate interface that allows exceptions.
*
* @param the type of the argument
*/
@FunctionalInterface
public interface PredicateWithException {
boolean test(T t) throws Exception;
default Predicate orElseThrow() {
return t -> {
try {
return test(t);
} catch (Exception e) {
throw Exceptions.duck(e);
}
};
}
default Predicate orElse(boolean orElse) {
return t -> {
try {
return test(t);
} catch (Exception e) {
return orElse;
}
};
}
default Predicate orElseGet(BooleanSupplier orElseGet) {
requireNonNull(orElseGet);
return t -> {
try {
return test(t);
} catch (Exception e) {
return orElseGet.getAsBoolean();
}
};
}
static Predicate asPredicate(PredicateWithException unchecked) {
return unchecked.orElseThrow();
}
static Predicate asPredicateOrElse(PredicateWithException unchecked, boolean orElse) {
return unchecked.orElse(orElse);
}
static Predicate asPredicateOrElseGet(PredicateWithException unchecked, BooleanSupplier orElseGet) {
return unchecked.orElseGet(orElseGet);
}
}