makeFailurePropagatingCompleteFuture(Iterable<? extends ListenableFuture<?>> futures, final T result) { FailureFutureCollection<Object> ffc = new FailureFutureCollection<>(futures); if (ffc.isDone()) { List<ListenableFuture<?>> failedFutures = ffc.get(); if (failedFutures.isEmpty()) { return immediateResultFuture(result); ffc.addCallback(new FutureCallback<List<ListenableFuture<?>>>() { @Override public void handleResult(List<ListenableFuture<?>> failedFutures) {
@Override protected void handleFutureDone(ListenableFuture<? extends T> f, int index) { if (f.isCancelled()) { // detect canceled conditions before an exception would have otherwise thrown addResult(f, index); return; } try { f.get(); } catch (InterruptedException e) { // should not be possible since this should only be called once the future is already done Thread.currentThread().interrupt(); } catch (ExecutionException e) { addResult(f, index); // failed so add it } catch (CancellationException e) { // should not be possible due check at start on what should be an already done future throw e; } } }
makeFailurePropagatingCompleteFuture(Iterable<? extends ListenableFuture<?>> futures, final T result) { FailureFutureCollection<Object> ffc = new FailureFutureCollection<>(futures); if (ffc.isDone()) { List<ListenableFuture<?>> failedFutures = ffc.get(); if (failedFutures.isEmpty()) { return immediateResultFuture(result); ffc.addCallback(new FutureCallback<List<ListenableFuture<?>>>() { @Override public void handleResult(List<ListenableFuture<?>> failedFutures) {
/** * This call is similar to {@link #makeCompleteFuture(Iterable)} in that it will immediately * provide a future that will not be satisfied till all provided futures complete. * <p> * This future provides a list of the futures that failed by either throwing an exception or * were canceled. The order of the resulting list is NOT deterministic. If order is needed * please see {@link #makeCompleteListFuture(Iterable)} and check for results. * <p> * If {@link ListenableFuture#cancel(boolean)} is invoked on the returned future, all provided * futures will attempt to be canceled in the same way. * * @since 1.2.0 * @param <T> The result object type returned from the futures * @param futures Structure of futures to iterate over * @return ListenableFuture which will be done once all futures provided are done */ public static <T> ListenableFuture<List<ListenableFuture<? extends T>>> makeFailureListFuture(Iterable<? extends ListenableFuture<? extends T>> futures) { return new FailureFutureCollection<>(futures); }
@Override protected void handleFutureDone(ListenableFuture<? extends T> f, int index) { if (f.isCancelled()) { // detect canceled conditions before an exception would have otherwise thrown addResult(f, index); return; } try { f.get(); } catch (InterruptedException e) { // should not be possible since this should only be called once the future is already done Thread.currentThread().interrupt(); } catch (ExecutionException e) { addResult(f, index); // failed so add it } catch (CancellationException e) { // should not be possible due check at start on what should be an already done future throw e; } } }
/** * This call is similar to {@link #makeCompleteFuture(Iterable)} in that it will immediately * provide a future that will not be satisfied till all provided futures complete. * <p> * This future provides a list of the futures that failed by either throwing an exception or * were canceled. The order of the resulting list is NOT deterministic. If order is needed * please see {@link #makeCompleteListFuture(Iterable)} and check for results. * <p> * If {@link ListenableFuture#cancel(boolean)} is invoked on the returned future, all provided * futures will attempt to be canceled in the same way. * * @since 1.2.0 * @param <T> The result object type returned from the futures * @param futures Structure of futures to iterate over * @return ListenableFuture which will be done once all futures provided are done */ public static <T> ListenableFuture<List<ListenableFuture<? extends T>>> makeFailureListFuture(Iterable<? extends ListenableFuture<? extends T>> futures) { return new FailureFutureCollection<>(futures); }