public <R> Try<R,Throwable> retry(final Function<? super T, ? extends R> fn, final int retries, final long delay, final TimeUnit timeUnit) {
        Try<T,Throwable> narrow = this.mapFailure(x->x);
        final Function<T, Try<R,Throwable>> retry = t -> {
            final long[] sleep = { timeUnit.toMillis(delay) };
            Throwable exception = null;
            for (int count = retries; count >=0; count--) {
                try {
                    return Try.success(fn.apply(t));
                } catch (final Throwable e) {
                    exception = e;
                    ExceptionSoftener.softenRunnable(() -> Thread.sleep(sleep[0]))
                        .run();
                    sleep[0] = sleep[0] * 2;
                }
            }
            return Try.failure(exception);


        };
        return narrow.flatMap(retry);
    }
public < R > Try < R , Throwable > retry ( final Function < ? super T , ? extends R > fn , final int retries , final long delay , final TimeUnit timeUnit ) { Try < T , Throwable > narrow = this . mapFailure ( x -> x ) ; final Function < T , Try < R , Throwable > > retry = t -> { final long [ ] sleep = { timeUnit . toMillis ( delay ) } ; Throwable exception = null ; for ( int count = retries ; count >= 0 ; count -- ) { try { return Try . success ( fn . apply ( t ) ) ; } catch ( final Throwable e ) { exception = e ; ExceptionSoftener . softenRunnable ( ( ) -> Thread . sleep ( sleep [ 0 ] ) ) . run ( ) ; sleep [ 0 ] = sleep [ 0 ] * 2 ; } } return Try . failure ( exception ) ; } ; return narrow . flatMap ( retry ) ; }
Retry a transformation if it fails. Retries up to <b>retries</b>
times, with an doubling backoff period starting @ <b>delay</b> TimeUnits delay before
retry.


@param fn
Function to retry if fails
@param retries
Number of retries
@param delay
Delay in TimeUnits
@param timeUnit
TimeUnit to use for delay