/** * Mirrors all results of the source Future to the destination Future. * <p> * This also propagates cancellations from the destination Future back to the source Future. */ public static <T> void mirror(ListenableFuture<? extends T> source, SettableFuture<? super T> destination, boolean mayInterruptIfRunning) { FutureCallback<T> callback = new FutureCallback<T>() { @Override public void onSuccess(@Nullable T result) { destination.set(result); } @Override public void onFailure(Throwable t) { destination.setException(t); } }; Futures.addCallback(source, callback, directExecutor()); propagateCancellation(destination, source, mayInterruptIfRunning); }
/** * Mirrors all results of the source Future to the destination Future. * <p> * This also propagates cancellations from the destination Future back to the source Future. */ public static <T> void mirror(ListenableFuture<? extends T> source, SettableFuture<? super T> destination, boolean mayInterruptIfRunning) { FutureCallback<T> callback = new FutureCallback<T>() { @Override public void onSuccess(@Nullable T result) { destination.set(result); } @Override public void onFailure(Throwable t) { destination.setException(t); } }; Futures.addCallback(source, callback, directExecutor()); propagateCancellation(destination, source, mayInterruptIfRunning); }
/** * Converts a CompletableFuture to a ListenableFuture. Cancellation of the * ListenableFuture will be propagated to the CompletableFuture. */ public static <V> ListenableFuture<V> toListenableFuture(CompletableFuture<V> completableFuture) { requireNonNull(completableFuture, "completableFuture is null"); SettableFuture<V> future = SettableFuture.create(); propagateCancellation(future, completableFuture, true); completableFuture.whenComplete((value, exception) -> { if (exception != null) { future.setException(exception); } else { future.set(value); } }); return future; }
/** * Converts a CompletableFuture to a ListenableFuture. Cancellation of the * ListenableFuture will be propagated to the CompletableFuture. */ public static <V> ListenableFuture<V> toListenableFuture(CompletableFuture<V> completableFuture) { requireNonNull(completableFuture, "completableFuture is null"); SettableFuture<V> future = SettableFuture.create(); propagateCancellation(future, completableFuture, true); completableFuture.whenComplete((value, exception) -> { if (exception != null) { future.setException(exception); } else { future.set(value); } }); return future; }
@Test public void propagateCancellationWithInterrupt() throws Exception { ExtendedSettableFuture<Object> fromFuture = ExtendedSettableFuture.create(); ExtendedSettableFuture<Object> toFuture = ExtendedSettableFuture.create(); propagateCancellation(fromFuture, toFuture, true); fromFuture.cancel(true); assertTrue(toFuture.isCancelled()); assertTrue(toFuture.checkWasInterrupted()); // Test interrupt override fromFuture = ExtendedSettableFuture.create(); toFuture = ExtendedSettableFuture.create(); propagateCancellation(fromFuture, toFuture, true); fromFuture.cancel(false); assertTrue(toFuture.isCancelled()); assertTrue(toFuture.checkWasInterrupted()); }
@Test public void propagateCancellationWithoutInterrupt() throws Exception { // Test interrupt override ExtendedSettableFuture<Object> fromFuture = ExtendedSettableFuture.create(); ExtendedSettableFuture<Object> toFuture = ExtendedSettableFuture.create(); propagateCancellation(fromFuture, toFuture, false); fromFuture.cancel(true); assertTrue(toFuture.isCancelled()); assertFalse(toFuture.checkWasInterrupted()); fromFuture = ExtendedSettableFuture.create(); toFuture = ExtendedSettableFuture.create(); propagateCancellation(fromFuture, toFuture, false); fromFuture.cancel(false); assertTrue(toFuture.isCancelled()); assertFalse(toFuture.checkWasInterrupted()); }
@Test public void propagateCancellationWithoutInterrupt() throws Exception { // Test interrupt override ExtendedSettableFuture<Object> fromFuture = ExtendedSettableFuture.create(); ExtendedSettableFuture<Object> toFuture = ExtendedSettableFuture.create(); propagateCancellation(fromFuture, toFuture, false); fromFuture.cancel(true); assertTrue(toFuture.isCancelled()); assertFalse(toFuture.checkWasInterrupted()); fromFuture = ExtendedSettableFuture.create(); toFuture = ExtendedSettableFuture.create(); propagateCancellation(fromFuture, toFuture, false); fromFuture.cancel(false); assertTrue(toFuture.isCancelled()); assertFalse(toFuture.checkWasInterrupted()); }
@Test public void propagateCancellationWithInterrupt() throws Exception { ExtendedSettableFuture<Object> fromFuture = ExtendedSettableFuture.create(); ExtendedSettableFuture<Object> toFuture = ExtendedSettableFuture.create(); propagateCancellation(fromFuture, toFuture, true); fromFuture.cancel(true); assertTrue(toFuture.isCancelled()); assertTrue(toFuture.checkWasInterrupted()); // Test interrupt override fromFuture = ExtendedSettableFuture.create(); toFuture = ExtendedSettableFuture.create(); propagateCancellation(fromFuture, toFuture, true); fromFuture.cancel(false); assertTrue(toFuture.isCancelled()); assertTrue(toFuture.checkWasInterrupted()); }