package com.firefly.utils.retry;

import com.firefly.utils.Assert;
import com.firefly.utils.concurrent.ThreadUtils;
import com.firefly.utils.function.Action1;

import java.util.concurrent.TimeUnit;

/**
 * @author Pengtao Qiu
 */
abstract public class WaitStrategies {
    public static <V> Action1<TaskContext<V>> fixedTime(long sleepTime, TimeUnit timeUnit) {
        return ctx -> ThreadUtils.sleep(sleepTime, timeUnit);
    }

    public static <V> Action1<TaskContext<V>> exponentialIncrease(long initTime, long maxTime, TimeUnit timeUnit,
                                                                  int multiply) {
        Assert.isTrue(multiply > 0, "The multiply must be great than 0");
        Assert.isTrue(maxTime >= initTime, "The max time must be great than or equals init time");
        return ctx -> {
           long timeout = initTime << ctx.getExecutedCount() * multiply;
        };
    }

}

@@ -474,26 +474,15 @@
 V%3E%3E 
-exponentialIncreas
+multipl
 e(lo
@@ -530,74 +530,8 @@
 nit,
-%0A                                                                 
  int
@@ -538,17 +538,17 @@
  multipl
-y
+e
 ) %7B%0A    
@@ -572,17 +572,17 @@
 (multipl
-y
+e
  %3E 0, %22T
@@ -591,17 +591,17 @@
  multipl
-y
+e
  must be
@@ -755,68 +755,688 @@
     
+ 
-long timeout = initTime %3C%3C ctx.getExecutedCount() * multiply
+int count = Math.max(ctx.getExecutedCount(), 1) - 1;%0A            long sleepTime = initTime %3C%3C (count * (multiple - 1));%0A            ThreadUtils.sleep(Math.min(sleepTime, maxTime), timeUnit);%0A        %7D;%0A    %7D%0A%0A    public static %3CV%3E Action1%3CTaskContext%3CV%3E%3E multiple(long initTime, long maxTime, TimeUnit timeUnit) %7B%0A        return multiple(initTime, maxTime, timeUnit, 2);%0A    %7D%0A%0A    public static %3CV%3E Action1%3CTaskContext%3CV%3E%3E multiple(long initTime, TimeUnit timeUnit) %7B%0A        return multiple(initTime, Long.MAX_VALUE, timeUnit, 2);%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        for (int i = 1; i %3C 5; i++) %7B%0A            System.out.println(3 %3C%3C ((i - 1) * (3 - 1)))
 ;%0A  
@@ -1434,27 +1434,26 @@
  1)));%0A        %7D
-;
 %0A    %7D%0A%0A%7D%0A

test wait strategy