|
| 1 | +package com.shortthirdman.primekit.essentials.common.util; |
| 2 | + |
| 3 | +import org.jetbrains.annotations.NotNull; |
| 4 | + |
| 5 | +import java.util.concurrent.CompletableFuture; |
| 6 | +import java.util.concurrent.Future; |
| 7 | +import java.util.concurrent.ScheduledFuture; |
| 8 | +import java.util.concurrent.ScheduledThreadPoolExecutor; |
| 9 | +import java.util.concurrent.ThreadFactory; |
| 10 | +import java.util.concurrent.TimeUnit; |
| 11 | +import java.util.concurrent.TimeoutException; |
| 12 | +import java.util.function.BiConsumer; |
| 13 | + |
| 14 | +/** |
| 15 | + * A utility class to add JDK 9+ timeout functionality to CompletableFuture in JDK. |
| 16 | + * Inspired by JDK 9's CompletableFuture implementation. |
| 17 | + * |
| 18 | + * @author ShortThirdMan |
| 19 | + */ |
| 20 | +public final class CompletableFutureUtils { |
| 21 | + |
| 22 | + private CompletableFutureUtils() {} |
| 23 | + |
| 24 | + /** |
| 25 | + * If not already completed, causes this CompletableFuture to be |
| 26 | + * completed exceptionally with a {@link TimeoutException} after the |
| 27 | + * given timeout. |
| 28 | + * |
| 29 | + * @param future the CompletableFuture to apply the timeout to |
| 30 | + * @param timeout how long to wait before completing exceptionally |
| 31 | + * @param unit the time unit of the timeout argument |
| 32 | + * @return the original CompletableFuture |
| 33 | + */ |
| 34 | + public static <T> CompletableFuture<T> orTimeout(CompletableFuture<T> future, long timeout, TimeUnit unit) { |
| 35 | + if (unit == null) { |
| 36 | + throw new NullPointerException("Time unit cannot be null"); |
| 37 | + } |
| 38 | + if (future == null) { |
| 39 | + throw new NullPointerException("CompletableFuture cannot be null"); |
| 40 | + } |
| 41 | + // If the future is already done, just return it. |
| 42 | + if (future.isDone()) { |
| 43 | + return future; |
| 44 | + } |
| 45 | + // Schedule a task to complete the future exceptionally after the timeout. |
| 46 | + // The Canceller will cancel this scheduled task if the future completes normally. |
| 47 | + return future.whenComplete(new Canceller(Delayer.delay(new Timeout(future), timeout, unit))); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Inner class to handle the actual timeout logic |
| 52 | + */ |
| 53 | + static final class Timeout implements Runnable { |
| 54 | + final CompletableFuture<?> future; |
| 55 | + Timeout(CompletableFuture<?> future) { |
| 56 | + this.future = future; |
| 57 | + } |
| 58 | + public void run() { |
| 59 | + if (future != null && !future.isDone()) { |
| 60 | + future.completeExceptionally(new TimeoutException()); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Inner class to handle cancellation of the timeout task |
| 67 | + */ |
| 68 | + static final class Canceller implements BiConsumer<Object, Throwable> { |
| 69 | + final Future<?> future; |
| 70 | + Canceller(Future<?> future) { |
| 71 | + this.future = future; |
| 72 | + } |
| 73 | + public void accept(Object ignore, Throwable ex) { |
| 74 | + // If the original future completed (ex is null) and the timeout task |
| 75 | + // hasn't run yet, cancel the timeout task. |
| 76 | + if (ex == null && future != null && !future.isDone()) { |
| 77 | + future.cancel(false); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Singleton delayed scheduler |
| 84 | + */ |
| 85 | + static final class Delayer { |
| 86 | + static ScheduledFuture<?> delay(Runnable command, long delay, TimeUnit unit) { |
| 87 | + return delayer.schedule(command, delay, unit); |
| 88 | + } |
| 89 | + private static final ScheduledThreadPoolExecutor delayer; |
| 90 | + static { |
| 91 | + delayer = new ScheduledThreadPoolExecutor(1, new DaemonThreadFactory()); |
| 92 | + delayer.setRemoveOnCancelPolicy(true); |
| 93 | + } |
| 94 | + static final class DaemonThreadFactory implements ThreadFactory { |
| 95 | + public Thread newThread(@NotNull Runnable r) { |
| 96 | + Thread t = new Thread(r); |
| 97 | + t.setDaemon(true); |
| 98 | + t.setName("CompletableFutureUtilsDelayScheduler"); |
| 99 | + return t; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments