MoreFutures { public static <V> CompletableFuture<V> unmodifiableFuture(CompletableFuture<V> future) { return unmodifiableFuture(future, false); } private  MoreFutures(); static void propagateCancellation(ListenableFuture<? extends X> source, Future<? extends Y> destination, boolean mayInterruptIfRunning); static void mirror(ListenableFuture<? extends T> source, SettableFuture<? super T> destination, boolean mayInterruptIfRunning); static Throwable unwrapCompletionException(Throwable throwable); static CompletableFuture<V> unmodifiableFuture(CompletableFuture<V> future); static CompletableFuture<V> unmodifiableFuture(CompletableFuture<V> future, boolean propagateCancel); static CompletableFuture<V> failedFuture(Throwable throwable); static V getFutureValue(Future<V> future); static V getFutureValue(Future<V> future, Class<E> exceptionType); static Optional<T> tryGetFutureValue(Future<T> future); static Optional<V> tryGetFutureValue(Future<V> future, int timeout, TimeUnit timeUnit); static Optional<V> tryGetFutureValue(Future<V> future, int timeout, TimeUnit timeUnit, Class<E> exceptionType); static T getDone(Future<T> future); static void checkSuccess(Future<?> future, String errorMessage); static ListenableFuture<V> whenAnyComplete(Iterable<? extends ListenableFuture<? extends V>> futures); static ListenableFuture<V> whenAnyCompleteCancelOthers(Iterable<? extends ListenableFuture<? extends V>> futures); static CompletableFuture<V> firstCompletedFuture(Iterable<? extends CompletionStage<? extends V>> futures); static CompletableFuture<V> firstCompletedFuture(Iterable<? extends CompletionStage<? extends V>> futures, boolean propagateCancel); static CompletableFuture<List<V>> allAsList(List<CompletableFuture<? extends V>> futures); static ListenableFuture<T> addTimeout(ListenableFuture<T> future, Callable<T> onTimeout, Duration timeout, ScheduledExecutorService executorService); static CompletableFuture<T> addTimeout(CompletableFuture<T> future, Callable<T> onTimeout, Duration timeout, ScheduledExecutorService executorService); static CompletableFuture<V> toCompletableFuture(ListenableFuture<V> listenableFuture); static ListenableFuture<V> toListenableFuture(CompletableFuture<V> completableFuture); static void addSuccessCallback(ListenableFuture<T> future, Consumer<T> successCallback); static void addSuccessCallback(ListenableFuture<T> future, Runnable successCallback); static void addExceptionCallback(ListenableFuture<T> future, Consumer<Throwable> exceptionCallback); static void addExceptionCallback(ListenableFuture<T> future, Runnable exceptionCallback);  }
@Test public void testModifyUnmodifiableFuture() { CompletableFuture<String> future = new CompletableFuture<>(); CompletableFuture<String> unmodifiableFuture = unmodifiableFuture(future); assertFailure(() -> unmodifiableFuture.complete("fail"), e -> assertInstanceOf(e, UnsupportedOperationException.class)); assertFalse(future.isDone()); assertFalse(unmodifiableFuture.isDone()); assertFailure(() -> unmodifiableFuture.completeExceptionally(new IOException()), e -> assertInstanceOf(e, UnsupportedOperationException.class)); assertFalse(future.isDone()); assertFalse(unmodifiableFuture.isDone()); assertFailure(() -> unmodifiableFuture.obtrudeValue("fail"), e -> assertInstanceOf(e, UnsupportedOperationException.class)); assertFalse(future.isDone()); assertFalse(unmodifiableFuture.isDone()); assertFailure(() -> unmodifiableFuture.obtrudeException(new IOException()), e -> assertInstanceOf(e, UnsupportedOperationException.class)); assertFalse(future.isDone()); assertFalse(unmodifiableFuture.isDone()); assertFalse(unmodifiableFuture.cancel(false)); assertFalse(future.isDone()); assertFalse(unmodifiableFuture.isDone()); assertFalse(unmodifiableFuture.cancel(true)); assertFalse(future.isDone()); assertFalse(unmodifiableFuture.isDone()); assertFalse(unmodifiableFuture.completeExceptionally(new CancellationException())); assertFalse(future.isDone()); assertFalse(unmodifiableFuture.isDone()); }
@Test public void testModifyCancelableUnmodifiableFuture() { CompletableFuture<String> future = new CompletableFuture<>(); CompletableFuture<String> unmodifiableFuture = unmodifiableFuture(future, true); assertFailure(() -> unmodifiableFuture.complete("fail"), e -> assertInstanceOf(e, UnsupportedOperationException.class)); assertFalse(future.isDone()); assertFalse(unmodifiableFuture.isDone()); assertFailure(() -> unmodifiableFuture.completeExceptionally(new IOException()), e -> assertInstanceOf(e, UnsupportedOperationException.class)); assertFalse(future.isDone()); assertFalse(unmodifiableFuture.isDone()); assertFailure(() -> unmodifiableFuture.obtrudeValue("fail"), e -> assertInstanceOf(e, UnsupportedOperationException.class)); assertFalse(future.isDone()); assertFalse(unmodifiableFuture.isDone()); assertFailure(() -> unmodifiableFuture.obtrudeException(new IOException()), e -> assertInstanceOf(e, UnsupportedOperationException.class)); assertFalse(future.isDone()); assertFalse(unmodifiableFuture.isDone()); }
@Test public void testUnmodifiableFutureCancelPropagation() { CompletableFuture<String> future = new CompletableFuture<>(); CompletableFuture<String> unmodifiableFuture = unmodifiableFuture(future, true); assertTrue(unmodifiableFuture.cancel(false)); assertTrue(future.isDone()); assertTrue(future.isCancelled()); assertTrue(unmodifiableFuture.isDone()); assertTrue(unmodifiableFuture.isCancelled()); future = new CompletableFuture<>(); unmodifiableFuture = unmodifiableFuture(future, true); assertTrue(unmodifiableFuture.cancel(true)); assertTrue(future.isDone()); assertTrue(future.isCancelled()); assertTrue(unmodifiableFuture.isDone()); assertTrue(unmodifiableFuture.isCancelled()); future = new CompletableFuture<>(); unmodifiableFuture = unmodifiableFuture(future, true); assertTrue(unmodifiableFuture.completeExceptionally(new CancellationException())); assertTrue(future.isDone()); assertTrue(future.isCancelled()); assertTrue(unmodifiableFuture.isDone()); assertTrue(unmodifiableFuture.isCancelled()); }
@Test public void testCompleteUnmodifiableFuture() { CompletableFuture<String> future = new CompletableFuture<>(); CompletableFuture<String> unmodifiableFuture = unmodifiableFuture(future); assertTrue(future.complete("done")); assertEquals(future.getNow(null), "done"); assertTrue(unmodifiableFuture.isDone()); assertEquals(unmodifiableFuture.getNow(null), "done"); }
@Test public void testAlreadyCompleteUnmodifiableFuture() { CompletableFuture<String> future = completedFuture("done"); CompletableFuture<String> unmodifiableFuture = unmodifiableFuture(future); assertEquals(future.getNow(null), "done"); assertTrue(unmodifiableFuture.isDone()); assertEquals(unmodifiableFuture.getNow(null), "done"); }