Package org.cactoos.scalar
Class Retry<T>
- java.lang.Object
-
- org.cactoos.scalar.Retry<T>
-
- Type Parameters:
T
- Type of output
- All Implemented Interfaces:
Scalar<T>
public final class Retry<T> extends Object implements Scalar<T>
Func that will try a few times before throwing an exception.new RetryScalar<>( () -> { if (new SecureRandom().nextDouble() > 0.3d) { throw new IllegalArgumentException("May happen"); } return 0; }, 5 ).value() // will try to run 5 times before throwing an exception
There is no thread-safety guarantee.
This class implements
Scalar
, which throws a checkedException
. This may not be convenient in many cases. To make it more convenient and get rid of the checked exception you can use theUnchecked
decorator. Or you may useIoChecked
to wrap it in an IOException.- Since:
- 0.9
-
-
Constructor Summary
Constructors Constructor Description Retry(Scalar<? extends T> scalar)
Ctor.Retry(Scalar<? extends T> scalar, int attempts)
Ctor.Retry(Scalar<? extends T> scalar, int attempts, Duration wait)
Ctor.Retry(Scalar<? extends T> scalar, Duration wait)
Ctor.Retry(Scalar<? extends T> scalar, Func<Integer,Boolean> exit)
Ctor.Retry(Scalar<? extends T> scalar, Func<Integer,Boolean> exit, Duration wait)
Ctor.
-
-
-
Constructor Detail
-
Retry
public Retry(Scalar<? extends T> scalar, Duration wait)
Ctor.- Parameters:
scalar
- Scalar originalwait
- TheDuration
to wait between attempts
-
Retry
public Retry(Scalar<? extends T> scalar, int attempts)
Ctor.- Parameters:
scalar
- Scalar originalattempts
- Maximum number of attempts
-
Retry
public Retry(Scalar<? extends T> scalar, int attempts, Duration wait)
Ctor.- Parameters:
scalar
- Scalar originalattempts
- Maximum number of attemptswait
- TheDuration
to wait between attempts
-
Retry
public Retry(Scalar<? extends T> scalar, Func<Integer,Boolean> exit)
Ctor.- Parameters:
scalar
- Func originalexit
- Exit condition, returns TRUE if there is no reason to try
-
-