Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why does CheckedPredicate exist? #363

Closed
spartanhooah opened this issue Apr 13, 2023 · 2 comments
Closed

Why does CheckedPredicate exist? #363

spartanhooah opened this issue Apr 13, 2023 · 2 comments

Comments

@spartanhooah
Copy link

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
Copy link
Contributor

Tembrel commented Apr 13, 2023

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
Copy link
Author

I see. Thank you for the explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants