1515 */
1616package dev .failsafe ;
1717
18+ import dev .failsafe .function .CheckedBiPredicate ;
19+ import dev .failsafe .function .CheckedPredicate ;
1820import dev .failsafe .internal .util .Assert ;
1921
2022import java .util .Arrays ;
2123import java .util .List ;
2224import java .util .Objects ;
23- import java .util .function .BiPredicate ;
2425import java .util .function .Predicate ;
2526
2627/**
2728 * A Policy that allows configurable conditions to determine whether an execution is a failure.
2829 * <ul>
2930 * <li>By default, any exception is considered a failure and will be handled by the policy. You can override this by
3031 * specifying your own {@code handle} conditions. The default exception handling condition will only be overridden by
31- * another condition that handles failure exceptions such as {@link #handle(Class)} or {@link #handleIf(BiPredicate )}.
32+ * another condition that handles failure exceptions such as {@link #handle(Class)} or {@link #handleIf(CheckedBiPredicate )}.
3233 * Specifying a condition that only handles results, such as {@link #handleResult(Object)} or
33- * {@link #handleResultIf(Predicate )} will not replace the default exception handling condition.</li>
34+ * {@link #handleResultIf(CheckedPredicate )} will not replace the default exception handling condition.</li>
3435 * <li>If multiple {@code handle} conditions are specified, any condition that matches an execution result or failure
3536 * will trigger policy handling.</li>
3637 * </ul>
@@ -84,27 +85,29 @@ public S handle(List<Class<? extends Throwable>> failures) {
8485 }
8586
8687 /**
87- * Specifies that a failure has occurred if the {@code failurePredicate} matches the failure.
88+ * Specifies that a failure has occurred if the {@code failurePredicate} matches the failure. Any exception thrown
89+ * from the {@code failurePredicate} is treated as a {@code false} result.
8890 *
8991 * @throws NullPointerException if {@code failurePredicate} is null
9092 */
91- public S handleIf (Predicate <? extends Throwable > failurePredicate ) {
93+ public S handleIf (CheckedPredicate <? extends Throwable > failurePredicate ) {
9294 Assert .notNull (failurePredicate , "failurePredicate" );
9395 config .failuresChecked = true ;
9496 config .failureConditions .add (failurePredicateFor (failurePredicate ));
9597 return (S ) this ;
9698 }
9799
98100 /**
99- * Specifies that a failure has occurred if the {@code resultPredicate} matches the execution result.
101+ * Specifies that a failure has occurred if the {@code resultPredicate} matches the execution result. Any exception
102+ * thrown from the {@code resultPredicate} is treated as a {@code false} result.
100103 *
101104 * @throws NullPointerException if {@code resultPredicate} is null
102105 */
103106 @ SuppressWarnings ("unchecked" )
104- public S handleIf (BiPredicate <R , ? extends Throwable > resultPredicate ) {
107+ public S handleIf (CheckedBiPredicate <R , ? extends Throwable > resultPredicate ) {
105108 Assert .notNull (resultPredicate , "resultPredicate" );
106109 config .failuresChecked = true ;
107- config .failureConditions .add ((BiPredicate <R , Throwable >) resultPredicate );
110+ config .failureConditions .add ((CheckedBiPredicate <R , Throwable >) resultPredicate );
108111 return (S ) this ;
109112 }
110113
@@ -120,11 +123,12 @@ public S handleResult(R result) {
120123 /**
121124 * Specifies that a failure has occurred if the {@code resultPredicate} matches the execution result. This method is
122125 * only considered when a result is returned from an execution, not when an exception is thrown. To handle results or
123- * exceptions with the same condition, use {@link #handleIf(BiPredicate)}.
126+ * exceptions with the same condition, use {@link #handleIf(CheckedBiPredicate)}. Any exception thrown from the {@code
127+ * resultPredicate} is treated as a {@code false} result.
124128 *
125129 * @throws NullPointerException if {@code resultPredicate} is null
126130 */
127- public S handleResultIf (Predicate <R > resultPredicate ) {
131+ public S handleResultIf (CheckedPredicate <R > resultPredicate ) {
128132 Assert .notNull (resultPredicate , "resultPredicate" );
129133 config .failureConditions .add (resultPredicateFor (resultPredicate ));
130134 return (S ) this ;
@@ -133,16 +137,17 @@ public S handleResultIf(Predicate<R> resultPredicate) {
133137 /**
134138 * Returns a predicate that evaluates whether the {@code result} equals an execution result.
135139 */
136- static <R > BiPredicate <R , Throwable > resultPredicateFor (R result ) {
140+ static <R > CheckedBiPredicate <R , Throwable > resultPredicateFor (R result ) {
137141 return (t , u ) -> result == null ? t == null && u == null : Objects .equals (result , t );
138142 }
139143
140144 /**
141145 * Returns a predicate that evaluates the {@code failurePredicate} against a failure.
142146 */
143147 @ SuppressWarnings ("unchecked" )
144- static <R > BiPredicate <R , Throwable > failurePredicateFor (Predicate <? extends Throwable > failurePredicate ) {
145- return (t , u ) -> u != null && ((Predicate <Throwable >) failurePredicate ).test (u );
148+ static <R > CheckedBiPredicate <R , Throwable > failurePredicateFor (
149+ CheckedPredicate <? extends Throwable > failurePredicate ) {
150+ return (t , u ) -> u != null && ((CheckedPredicate <Throwable >) failurePredicate ).test (u );
146151 }
147152
148153 /**
@@ -151,7 +156,7 @@ static <R> BiPredicate<R, Throwable> failurePredicateFor(Predicate<? extends Thr
151156 * Short-circuits to false without invoking {@code resultPredicate}, when result is not present (i.e.
152157 * BiPredicate.test(null, Throwable)).
153158 */
154- static <R > BiPredicate <R , Throwable > resultPredicateFor (Predicate <R > resultPredicate ) {
159+ static <R > CheckedBiPredicate <R , Throwable > resultPredicateFor (CheckedPredicate <R > resultPredicate ) {
155160 return (t , u ) -> {
156161 if (u == null ) {
157162 return resultPredicate .test (t );
@@ -167,7 +172,7 @@ static <R> BiPredicate<R, Throwable> resultPredicateFor(Predicate<R> resultPredi
167172 /**
168173 * Returns a predicate that returns whether any of the {@code failures} are assignable from an execution failure.
169174 */
170- static <R > BiPredicate <R , Throwable > failurePredicateFor (List <Class <? extends Throwable >> failures ) {
175+ static <R > CheckedBiPredicate <R , Throwable > failurePredicateFor (List <Class <? extends Throwable >> failures ) {
171176 return (t , u ) -> {
172177 if (u == null )
173178 return false ;
0 commit comments