/** * Times the given future out after the timeout. * * @param future to time out * @param timeout after which the given future is timed out * @param timeUnit time unit of the timeout * @param <T> type of the given future * @return The timeout enriched future */ public static <T> CompletableFuture<T> orTimeout(CompletableFuture<T> future, long timeout, TimeUnit timeUnit) { if (!future.isDone()) { final ScheduledFuture<?> timeoutFuture = Delayer.delay(new Timeout(future), timeout, timeUnit); future.whenComplete((T value, Throwable throwable) -> { if (!timeoutFuture.isDone()) { timeoutFuture.cancel(false); } }); } return future; }
/** * Times the given future out after the timeout. * * @param future to time out * @param timeout after which the given future is timed out * @param timeUnit time unit of the timeout * @param <T> type of the given future * @return The timeout enriched future */ public static <T> CompletableFuture<T> orTimeout(CompletableFuture<T> future, long timeout, TimeUnit timeUnit) { if (!future.isDone()) { final ScheduledFuture<?> timeoutFuture = Delayer.delay(new Timeout(future), timeout, timeUnit); future.whenComplete((T value, Throwable throwable) -> { if (!timeoutFuture.isDone()) { timeoutFuture.cancel(false); } }); } return future; }
/** * Times the given future out after the timeout. * * @param future to time out * @param timeout after which the given future is timed out * @param timeUnit time unit of the timeout * @param <T> type of the given future * @return The timeout enriched future */ public static <T> CompletableFuture<T> orTimeout(CompletableFuture<T> future, long timeout, TimeUnit timeUnit) { if (!future.isDone()) { final ScheduledFuture<?> timeoutFuture = Delayer.delay(new Timeout(future), timeout, timeUnit); future.whenComplete((T value, Throwable throwable) -> { if (!timeoutFuture.isDone()) { timeoutFuture.cancel(false); } }); } return future; }