failsafe-lib/failsafe

Why does CheckedPredicate exist?

spartanhooah opened this issue · 2 comments

Is there any reason that CheckedPredicate cannot be replaced with java.util.function.Predicate? The former seems like a simplified version of the latter.

The signature of the CheckedPredicate::test method is

boolean test(T t) throws Throwable;

and for plain Predicate::test, it is just

boolean test(T t);

They are both functional interfaces, though. You can use a lambda or method reference that conforms to java.util.function.Predicate in contexts that accept a CheckedPredicate, but the latter allows you to write handlers that throw checked exceptions, e.g.,

RetryPolicy retryPolicy = RetryPolicy.builder()
    .handleResultIf(result -> needsRetry(result))
    .build();

where needsRetry can throw a checked exception. (If it does, the policy will not handle the result.)

I see. Thank you for the explanation.