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 type
C - config type
R - 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 as handle(Class) or handleIf(CheckedBiPredicate). Specifying a condition that only handles results, such as handleResult(Object) or handleResultIf(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.
  • Constructor Details

    • FailurePolicyBuilder

      protected FailurePolicyBuilder(C config)
  • Method Details

    • handle

      public S handle(Class<? extends Throwable> exception)
      Specifies the exception to handle as a failure. Any exception that is assignable from the exception will be handled.
      Throws:
      NullPointerException - if exception is null
    • handle

      @SafeVarargs public final S handle(Class<? extends Throwable>... exceptions)
      Specifies the exceptions to handle as failures. Any exceptions that are assignable from the exceptions will be handled.
      Throws:
      NullPointerException - if exceptions is null
      IllegalArgumentException - if exceptions is empty
    • handle

      public S handle(List<Class<? extends Throwable>> exceptions)
      Specifies the exceptions to handle as failures. Any exceptions that are assignable from the exceptions will be handled.
      Throws:
      NullPointerException - if exceptions is null
      IllegalArgumentException - if exceptions is null or empty
    • handleIf

      public S handleIf(CheckedPredicate<? extends Throwable> failurePredicate)
      Specifies that a failure has occurred if the failurePredicate matches the exception. Any exception thrown from the failurePredicate is treated as a false result.
      Throws:
      NullPointerException - if failurePredicate is null
    • handleIf

      public S handleIf(CheckedBiPredicate<R,? extends Throwable> resultPredicate)
      Specifies that a failure has occurred if the resultPredicate matches the execution result. Any exception thrown from the resultPredicate is treated as a false result.
      Throws:
      NullPointerException - if resultPredicate is null
    • handleResult

      public S handleResult(R result)
      Specifies that a failure has occurred if the result 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 the resultPredicate 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, use handleIf(CheckedBiPredicate). Any exception thrown from the resultPredicate is treated as a false result.
      Throws:
      NullPointerException - if resultPredicate is null
    • resultPredicateFor

      static <R> CheckedBiPredicate<R,Throwable> resultPredicateFor(R result)
      Returns a predicate that evaluates whether the result equals an execution result.
    • failurePredicateFor

      static <R> CheckedBiPredicate<R,Throwable> failurePredicateFor(CheckedPredicate<? extends Throwable> failurePredicate)
      Returns a predicate that evaluates the failurePredicate against a failure.
    • resultPredicateFor

      static <R> CheckedBiPredicate<R,Throwable> resultPredicateFor(CheckedPredicate<R> resultPredicate)
      Returns a predicate that evaluates the resultPredicate 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,Throwable> failurePredicateFor(List<Class<? extends Throwable>> failures)
      Returns a predicate that returns whether any of the failures are assignable from an execution failure.