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>> multiple(long initTime, long maxTime, TimeUnit timeUnit, int multiple) {
        Assert.isTrue(multiple > 0, "The multiple must be great than 0");
        Assert.isTrue(maxTime >= initTime, "The max time must be great than or equals init time");
        return ctx -> {
            int count = Math.max(ctx.getExecutedCount(), 1) - 1;
            long sleepTime = initTime << (count * (multiple - 1));
            ThreadUtils.sleep(Math.min(sleepTime, maxTime), timeUnit);
        };
    }

    public static <V> Action1<TaskContext<V>> multiple(long initTime, long maxTime, TimeUnit timeUnit) {
        return multiple(initTime, maxTime, timeUnit, 2);
    }

    public static <V> Action1<TaskContext<V>> multiple(long initTime, TimeUnit timeUnit) {
        return multiple(initTime, Long.MAX_VALUE, timeUnit, 2);
    }

    public static void main(String[] args) {
        for (int i = 1; i < 5; i++) {
            System.out.println(3 << ((i - 1) * (3 - 1)));
        }
    }

}

@@ -313,20 +313,20 @@
 %3E%3E fixed
-Time
+Wait
 (long sl
@@ -470,32 +470,36 @@
 ext%3CV%3E%3E multiple
+Wait
 (long initTime, 
@@ -1011,32 +1011,36 @@
 ext%3CV%3E%3E multiple
+Wait
 (long initTime, 
@@ -1089,32 +1089,36 @@
  return multiple
+Wait
 (initTime, maxTi
@@ -1196,16 +1196,20 @@
 multiple
+Wait
 (long in
@@ -1260,16 +1260,20 @@
 multiple
+Wait
 (initTim

test wait strategy