Util { public static <T> CompletionStage<T> asStage(ListenableFuture<T> future) { CompletableFuture<T> completableFuture = new CompletableFuture<>(); Futures.addCallback(future, new FutureCallback<T>() { @Override public void onSuccess(T result) { completableFuture.complete(result); } @Override public void onFailure(Throwable t) { completableFuture.completeExceptionally(t); } }, directExecutor()); return completableFuture; } private  Util(); static CompletionStage<T> asStage(ListenableFuture<T> future); static ListenableFuture<T> asFuture(CompletionStage<T> stage);  }
@Test public void shouldConvertToStage() throws Exception { CompletionStage<String> converted = Util.asStage(future); future.set("i'm done!"); assertThat(converted.toCompletableFuture().get(), equalTo("i'm done!")); }
@Test public void shouldPropagateExceptionsToStage() throws Exception { CompletionStage<String> converted = Util.asStage(future) .exceptionally(Throwable::getMessage); NullPointerException exception = new NullPointerException("expected exception"); future.setException(exception); assertThat(converted.toCompletableFuture().get(), equalTo("expected exception")); }