failsafe-lib / failsafe

Fault tolerance and resilience patterns for the JVM
https://failsafe.dev
Apache License 2.0
4.16k stars 295 forks source link

Why does CheckedPredicate exist? #363

Closed spartanhooah closed 1 year ago

spartanhooah commented 1 year ago

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

Tembrel commented 1 year ago

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.)

spartanhooah commented 1 year ago

I see. Thank you for the explanation.