Class RetryFunction<F,​T>

  • Type Parameters:
    F - The type of the parameter the Function accepts
    T - The type of the result the Function yields upon application
    All Implemented Interfaces:
    java.util.function.Function<F,​T>

    public class RetryFunction<F,​T>
    extends java.lang.Object
    implements java.util.function.Function<F,​T>
    A Function which wraps the apply method of another Function and attempts it up to a fixed number of times. This class can be used when a task is known to be prone to occasional failure and other workarounds are not known.
    See Also:
    for a Supplier implementation, for a Runnable implementation, for some factory methods
    • Constructor Summary

      Constructors 
      Constructor Description
      RetryFunction​(java.util.function.Function<F,​T> function, int tries)
      An instance that does nothing before retrying and ignores exceptions that occur.
      RetryFunction​(java.util.function.Function<F,​T> function, int tries, ExceptionHandler handler)
      An instance that does nothing before retrying.
      RetryFunction​(java.util.function.Function<F,​T> function, int tries, ExceptionHandler handler, java.lang.Runnable beforeRetry)
      Constructor for RetryFunction.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      T apply​(F parameter)
      Attempt to apply parameter to the wrapped Function tries number of times.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.function.Function

        andThen, compose
    • Field Detail

      • function

        private final java.util.function.Function<F,​T> function
      • tries

        private final int tries
      • beforeRetry

        private final java.lang.Runnable beforeRetry
    • Constructor Detail

      • RetryFunction

        public RetryFunction​(java.util.function.Function<F,​T> function,
                             int tries)
        An instance that does nothing before retrying and ignores exceptions that occur.
        Parameters:
        function - which fetches the result, must not be null
        tries - the numbe rof times to attempt to get a result, must be positive
      • RetryFunction

        public RetryFunction​(java.util.function.Function<F,​T> function,
                             int tries,
                             ExceptionHandler handler)
        An instance that does nothing before retrying.
        Parameters:
        function - which fetches the result, must not be null
        tries - the number of times to attempt to get a result, must be positive
        handler - reacts to exceptions thrown by the supplier, must not be null
      • RetryFunction

        public RetryFunction​(java.util.function.Function<F,​T> function,
                             int tries,
                             ExceptionHandler handler,
                             java.lang.Runnable beforeRetry)

        Constructor for RetryFunction.

        Parameters:
        function - which fetches the result, must not be null
        tries - the number of times to attempt to get a result, must be positive
        handler - reacts to exceptions thrown by the supplier, must not be null
        beforeRetry - an effect that is run before a retry attempt
    • Method Detail

      • apply

        public T apply​(F parameter)
        Attempt to apply parameter to the wrapped Function tries number of times. Any exceptions thrown will be ignored until the number of attempts is reached. If the number of attempts is reached without a successful result, the most recent exception to be thrown will be rethrown.
        Specified by:
        apply in interface java.util.function.Function<F,​T>