Package dev.failsafe
Class FailurePolicyBuilder<S,C extends FailurePolicyConfig<R>,R>
- java.lang.Object
-
- dev.failsafe.PolicyBuilder<S,C,R>
-
- dev.failsafe.FailurePolicyBuilder<S,C,R>
-
- Type Parameters:
S
- self typeC
- config typeR
- result type
- All Implemented Interfaces:
PolicyListeners<S,R>
- Direct Known Subclasses:
DelayablePolicyBuilder
,FallbackBuilder
public abstract class FailurePolicyBuilder<S,C extends FailurePolicyConfig<R>,R> extends PolicyBuilder<S,C,R>
A Policy that allows configurable conditions to determine whether an execution is a failure.- By default, any exception is considered a failure and will be handled by the policy. You can override this by
specifying your own
handle
conditions. The default exception handling condition will only be overridden by another condition that handles failure exceptions such ashandle(Class)
orhandleIf(CheckedBiPredicate)
. Specifying a condition that only handles results, such ashandleResult(Object)
orhandleResultIf(CheckedPredicate)
will not replace the default exception handling condition. - If multiple
handle
conditions are specified, any condition that matches an execution result or exception will trigger policy handling.
-
-
Field Summary
-
Fields inherited from class dev.failsafe.PolicyBuilder
config
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
FailurePolicyBuilder(C config)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) static <R> CheckedBiPredicate<R,java.lang.Throwable>
failurePredicateFor(CheckedPredicate<? extends java.lang.Throwable> failurePredicate)
Returns a predicate that evaluates thefailurePredicate
against a failure.(package private) static <R> CheckedBiPredicate<R,java.lang.Throwable>
failurePredicateFor(java.util.List<java.lang.Class<? extends java.lang.Throwable>> failures)
Returns a predicate that returns whether any of thefailures
are assignable from an execution failure.S
handle(java.lang.Class<? extends java.lang.Throwable> exception)
Specifies the exception to handle as a failure.S
handle(java.lang.Class<? extends java.lang.Throwable>... exceptions)
Specifies the exceptions to handle as failures.S
handle(java.util.List<java.lang.Class<? extends java.lang.Throwable>> exceptions)
Specifies the exceptions to handle as failures.S
handleIf(CheckedBiPredicate<R,? extends java.lang.Throwable> resultPredicate)
Specifies that a failure has occurred if theresultPredicate
matches the execution result.S
handleIf(CheckedPredicate<? extends java.lang.Throwable> failurePredicate)
Specifies that a failure has occurred if thefailurePredicate
matches the exception.S
handleResult(R result)
Specifies that a failure has occurred if theresult
matches the execution result.S
handleResultIf(CheckedPredicate<R> resultPredicate)
Specifies that a failure has occurred if theresultPredicate
matches the execution result.(package private) static <R> CheckedBiPredicate<R,java.lang.Throwable>
resultPredicateFor(CheckedPredicate<R> resultPredicate)
Returns a predicate that evaluates theresultPredicate
against a result, when present.(package private) static <R> CheckedBiPredicate<R,java.lang.Throwable>
resultPredicateFor(R result)
Returns a predicate that evaluates whether theresult
equals an execution result.-
Methods inherited from class dev.failsafe.PolicyBuilder
onFailure, onSuccess
-
-
-
-
Constructor Detail
-
FailurePolicyBuilder
protected FailurePolicyBuilder(C config)
-
-
Method Detail
-
handle
public S handle(java.lang.Class<? extends java.lang.Throwable> exception)
Specifies the exception to handle as a failure. Any exception that is assignable from theexception
will be handled.- Throws:
java.lang.NullPointerException
- ifexception
is null
-
handle
@SafeVarargs public final S handle(java.lang.Class<? extends java.lang.Throwable>... exceptions)
Specifies the exceptions to handle as failures. Any exceptions that are assignable from theexceptions
will be handled.- Throws:
java.lang.NullPointerException
- ifexceptions
is nulljava.lang.IllegalArgumentException
- if exceptions is empty
-
handle
public S handle(java.util.List<java.lang.Class<? extends java.lang.Throwable>> exceptions)
Specifies the exceptions to handle as failures. Any exceptions that are assignable from theexceptions
will be handled.- Throws:
java.lang.NullPointerException
- ifexceptions
is nulljava.lang.IllegalArgumentException
- if exceptions is null or empty
-
handleIf
public S handleIf(CheckedPredicate<? extends java.lang.Throwable> failurePredicate)
Specifies that a failure has occurred if thefailurePredicate
matches the exception. Any exception thrown from thefailurePredicate
is treated as afalse
result.- Throws:
java.lang.NullPointerException
- iffailurePredicate
is null
-
handleIf
public S handleIf(CheckedBiPredicate<R,? extends java.lang.Throwable> resultPredicate)
Specifies that a failure has occurred if theresultPredicate
matches the execution result. Any exception thrown from theresultPredicate
is treated as afalse
result.- Throws:
java.lang.NullPointerException
- ifresultPredicate
is null
-
handleResult
public S handleResult(R result)
Specifies that a failure has occurred if theresult
matches the execution result. This method is only considered when a result is returned from an execution, not when an exception is thrown.
-
handleResultIf
public S handleResultIf(CheckedPredicate<R> resultPredicate)
Specifies that a failure has occurred if theresultPredicate
matches the execution result. This method is only considered when a result is returned from an execution, not when an exception is thrown. To handle results or exceptions with the same condition, usehandleIf(CheckedBiPredicate)
. Any exception thrown from theresultPredicate
is treated as afalse
result.- Throws:
java.lang.NullPointerException
- ifresultPredicate
is null
-
resultPredicateFor
static <R> CheckedBiPredicate<R,java.lang.Throwable> resultPredicateFor(R result)
Returns a predicate that evaluates whether theresult
equals an execution result.
-
failurePredicateFor
static <R> CheckedBiPredicate<R,java.lang.Throwable> failurePredicateFor(CheckedPredicate<? extends java.lang.Throwable> failurePredicate)
Returns a predicate that evaluates thefailurePredicate
against a failure.
-
resultPredicateFor
static <R> CheckedBiPredicate<R,java.lang.Throwable> resultPredicateFor(CheckedPredicate<R> resultPredicate)
Returns a predicate that evaluates theresultPredicate
against a result, when present.Short-circuits to false without invoking
resultPredicate
, when result is not present (i.e. BiPredicate.test(null, Throwable)).
-
failurePredicateFor
static <R> CheckedBiPredicate<R,java.lang.Throwable> failurePredicateFor(java.util.List<java.lang.Class<? extends java.lang.Throwable>> failures)
Returns a predicate that returns whether any of thefailures
are assignable from an execution failure.
-
-