Class 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 checked Exception. This may not be convenient in many cases. To make it more convenient and get rid of the checked exception you can use the Unchecked decorator. Or you may use IoChecked to wrap it in an IOException.

    Since:
    0.9
    • Constructor Detail

      • Retry

        public Retry​(Scalar<? extends T> scalar)
        Ctor.
        Parameters:
        scalar - Scalar original
      • Retry

        public Retry​(Scalar<? extends T> scalar,
                     Duration wait)
        Ctor.
        Parameters:
        scalar - Scalar original
        wait - The Duration to wait between attempts
      • Retry

        public Retry​(Scalar<? extends T> scalar,
                     int attempts)
        Ctor.
        Parameters:
        scalar - Scalar original
        attempts - Maximum number of attempts
      • Retry

        public Retry​(Scalar<? extends T> scalar,
                     int attempts,
                     Duration wait)
        Ctor.
        Parameters:
        scalar - Scalar original
        attempts - Maximum number of attempts
        wait - The Duration to wait between attempts
      • Retry

        public Retry​(Scalar<? extends T> scalar,
                     Func<Integer,​Boolean> exit)
        Ctor.
        Parameters:
        scalar - Func original
        exit - Exit condition, returns TRUE if there is no reason to try
      • Retry

        public Retry​(Scalar<? extends T> scalar,
                     Func<Integer,​Boolean> exit,
                     Duration wait)
        Ctor.
        Parameters:
        scalar - Func original
        exit - Exit condition, returns TRUE if there is no reason to try
        wait - The Duration to wait between attempts
    • Method Detail

      • value

        public T value()
                throws Exception
        Description copied from interface: Scalar
        Convert it to the value.
        Specified by:
        value in interface Scalar<T>
        Returns:
        The value
        Throws:
        Exception - If fails