@Override public boolean completeExceptionally(Throwable ex) { if (ex instanceof CancellationException) { return cancel(false); } throw new UnsupportedOperationException(); }
/** * Returns a future that can not be completed or optionally canceled. */ public static <V> CompletableFuture<V> unmodifiableFuture(CompletableFuture<V> future, boolean propagateCancel) { requireNonNull(future, "future is null"); Function<Boolean, Boolean> onCancelFunction; if (propagateCancel) { onCancelFunction = future::cancel; } else { onCancelFunction = mayInterrupt -> false; } UnmodifiableCompletableFuture<V> unmodifiableFuture = new UnmodifiableCompletableFuture<>(onCancelFunction); future.whenComplete((value, exception) -> { if (exception != null) { unmodifiableFuture.internalCompleteExceptionally(exception); } else { unmodifiableFuture.internalComplete(value); } }); return unmodifiableFuture; }
/** * Returns a future that can not be completed or optionally canceled. */ @Deprecated public static <V> CompletableFuture<V> unmodifiableFuture(CompletableFuture<V> future, boolean propagateCancel) { requireNonNull(future, "future is null"); Function<Boolean, Boolean> onCancelFunction; if (propagateCancel) { onCancelFunction = future::cancel; } else { onCancelFunction = mayInterrupt -> false; } UnmodifiableCompletableFuture<V> unmodifiableFuture = new UnmodifiableCompletableFuture<>(onCancelFunction); future.whenComplete((value, exception) -> { if (exception != null) { unmodifiableFuture.internalCompleteExceptionally(exception); } else { unmodifiableFuture.internalComplete(value); } }); return unmodifiableFuture; }
/** * Returns a future that can not be completed or optionally canceled. */ @Deprecated public static <V> CompletableFuture<V> unmodifiableFuture(CompletableFuture<V> future, boolean propagateCancel) { requireNonNull(future, "future is null"); Function<Boolean, Boolean> onCancelFunction; if (propagateCancel) { onCancelFunction = future::cancel; } else { onCancelFunction = mayInterrupt -> false; } UnmodifiableCompletableFuture<V> unmodifiableFuture = new UnmodifiableCompletableFuture<>(onCancelFunction); future.whenComplete((value, exception) -> { if (exception != null) { unmodifiableFuture.internalCompleteExceptionally(exception); } else { unmodifiableFuture.internalComplete(value); } }); return unmodifiableFuture; }
@Override public void run() { if (settableFuture.isDone()) { return; } // run the timeout task and set the result into the future try { T result = timeoutValue.get(); settableFuture.internalComplete(result); } catch (Throwable t) { settableFuture.internalCompleteExceptionally(t); propagateIfInstanceOf(t, RuntimeException.class); } // cancel the original future, if it still exists Future<T> future = futureReference.get(); if (future != null) { future.cancel(true); } } }
@Override public void run() { if (settableFuture.isDone()) { return; } // run the timeout task and set the result into the future try { T result = timeoutValue.call(); settableFuture.internalComplete(result); } catch (Throwable t) { settableFuture.internalCompleteExceptionally(t); throwIfInstanceOf(t, RuntimeException.class); } // cancel the original future, if it still exists Future<T> future = futureReference.get(); if (future != null) { future.cancel(true); } } }
@Override public void run() { if (settableFuture.isDone()) { return; } // run the timeout task and set the result into the future try { T result = timeoutValue.call(); settableFuture.internalComplete(result); } catch (Throwable t) { settableFuture.internalCompleteExceptionally(t); throwIfInstanceOf(t, RuntimeException.class); } // cancel the original future, if it still exists Future<T> future = futureReference.get(); if (future != null) { future.cancel(true); } } }
@Override public boolean completeExceptionally(Throwable ex) { if (ex instanceof CancellationException) { return cancel(false); } throw new UnsupportedOperationException(); }
@Override public boolean completeExceptionally(Throwable ex) { if (ex instanceof CancellationException) { return cancel(false); } throw new UnsupportedOperationException(); }