package org.sis.util.concurrent;

import org.springframework.util.concurrent.ListenableFuture;

import java.util.concurrent.CompletableFuture;

public class Futures {

  public static <T> CompletableFuture<T> completableFuture(ListenableFuture<T> future) {
    CompletableFuture<T> completableFuture = new CompletableFuture<>();
    future.addCallback(completableFuture::complete, completableFuture::completeExceptionally);
    return completableFuture;
  }
}

@@ -27,16 +27,79 @@
 rrent;%0A%0A
+import com.google.common.util.concurrent.ThreadFactoryBuilder;%0A
 import o
@@ -168,352 +168,1493 @@
 ava.
-util.concurrent.CompletableFuture;%0A%0Apublic class Futures %7B%0A%0A  public static %3CT%3E CompletableFuture%3CT%3E completableFuture(ListenableFuture%3CT%3E future) %7B%0A    CompletableFuture%3CT%3E completableFuture = new CompletableFuture%3C%3E();%0A    future.addCallback(completableFuture::complete, completableFuture::completeExceptionally);%0A    return completableFuture
+time.Duration;%0Aimport java.util.concurrent.*;%0A%0Aimport static java.util.function.Function.identity;%0A%0A/**%0A * @since 1.0%0A */%0Apublic final class Futures %7B%0A%0A  /**%0A   * Fixed size scheduler thread pool for handling asynchronous timeouts.%0A   * %3Cp%3E%0A   * For more details read %3Ca href=%22http://www.nurkiewicz.com/2014/12/asynchronous-timeouts-with.html%22%3Ethis%3C/a%3E blog.%0A   */%0A  private static final ScheduledExecutorService TIMEOUT_SCHEDULER = Executors.newScheduledThreadPool(%0A      1, new ThreadFactoryBuilder()%0A          .setDaemon(true)%0A          .setNameFormat(%22failAfter-%25d%22)%0A          .build());%0A%0A  public static %3CT%3E CompletableFuture%3CT%3E completableFuture(ListenableFuture%3CT%3E future) %7B%0A    CompletableFuture%3CT%3E completableFuture = new CompletableFuture%3C%3E();%0A    future.addCallback(completableFuture::complete, completableFuture::completeExceptionally);%0A    return completableFuture;%0A  %7D%0A%0A  public static %3CT%3E CompletableFuture%3CT%3E failAfter(Duration duration) %7B%0A    final CompletableFuture%3CT%3E promise = new CompletableFuture%3C%3E();%0A    TIMEOUT_SCHEDULER.schedule(() -%3E %7B%0A      final TimeoutException ex = new TimeoutException(%22Timeout after %22 + duration);%0A      return promise.completeExceptionally(ex);%0A    %7D, duration.toMillis(), TimeUnit.MILLISECONDS);%0A    return promise;%0A  %7D%0A%0A  public static %3CT%3E CompletableFuture%3CT%3E within(CompletableFuture%3CT%3E future, Duration duration) %7B%0A    final CompletableFuture%3CT%3E timeout = failAfter(duration);%0A    return future.applyToEither(timeout, identity())
 ;%0A  

Add async timeout for CompletableFutures