Program: 161647
Project: 161647
Buggy source:
/** 
 * This returns a future which provides the results of all the provided futures.  Thus  preventing the need to iterate over all the futures and manually extract the results.  This  call does NOT block, instead it will return a future which will not complete until all the  provided futures complete.   <p> The order of the result list is NOT deterministic. <p> If called with   {@code true} for {@code ignoreFailedFutures}, even if some of the provided  futures finished in error, they will be ignored and just the successful results will be  provided.  If called with   {@code false} then if any futures complete in error, then the returned future will throw a  {@link ExecutionException} with the error as the cause when {@link Future#get()} is invoked.  In addition if called with {@code false} and any of the provided futures are canceled, then the returned future will also be canceled, resulting in a  {@link CancellationException} being thrown when {@link Future#get()} is invoked.  In the case where there is canceled and failed exceptions in the collection, this will prefer to throw the  failure as an  {@link ExecutionException} rather than obscure it with a {@link CancellationException}.  In other words   {@link CancellationException} will be thrown ONLY if there was canceled tasks, but NO tasks which finished in error.
 * @since 4.0.0
 * @param < T > The result object type returned from the futures
 * @param futures Structure of futures to iterate over and extract results from
 * @param ignoreFailedFutures {@code true} to ignore any failed or canceled futures
 * @return A {@link ListenableFuture} which will provide a list of the results from the provided futures
 */
public static <T>ListenableFuture<List<T>> makeResultListFuture(Iterable<? extends ListenableFuture<? extends T>> futures,final boolean ignoreFailedFutures){
  if (futures == null) {
    return immediateResultFuture(Collections.<T>emptyList());
  }
  ListenableFuture<List<ListenableFuture<? extends T>>> completeFuture=makeCompleteListFuture(futures);
  final SettableListenableFuture<List<T>> result;
  result=new CancelDelegateSettableListenableFuture<>(completeFuture);
  completeFuture.addCallback(new FutureCallback<List<ListenableFuture<? extends T>>>(){
    @Override public void handleResult(    List<ListenableFuture<? extends T>> resultFutures){
      boolean needToCancel=false;
      ArrayList<T> results=new ArrayList<>(resultFutures.size());
      Iterator<ListenableFuture<? extends T>> it=resultFutures.iterator();
      while (it.hasNext()) {
        ListenableFuture<? extends T> f=it.next();
        if (f.isCancelled()) {
          if (!ignoreFailedFutures) {
            needToCancel=true;
          }
          continue;
        }
        try {
          results.add(f.get());
        }
 catch (        ExecutionException e) {
          if (!ignoreFailedFutures) {
            result.setFailure(e.getCause());
            return;
          }
        }
catch (        Exception e) {
          result.setFailure(new Exception(e));
          return;
        }
      }
      if (needToCancel) {
        if (!result.cancel(false)) {
          throw new IllegalStateException();
        }
      }
 else {
        result.setResult(results);
      }
    }
    @Override public void handleFailure(    Throwable t){
      if (t instanceof CancellationException) {
      }
 else {
        result.setFailure(t);
      }
    }
  }
);
  return result;
}

Fixed version (ground truth):
/** 
 * This returns a future which provides the results of all the provided futures.  Thus  preventing the need to iterate over all the futures and manually extract the results.  This  call does NOT block, instead it will return a future which will not complete until all the  provided futures complete.   <p> The order of the result list is NOT deterministic. <p> If called with   {@code true} for {@code ignoreFailedFutures}, even if some of the provided  futures finished in error, they will be ignored and just the successful results will be  provided.  If called with   {@code false} then if any futures complete in error, then the returned future will throw a  {@link ExecutionException} with the error as the cause when {@link Future#get()} is invoked.  In addition if called with {@code false} and any of the provided futures are canceled, then the returned future will also be canceled, resulting in a  {@link CancellationException} being thrown when {@link Future#get()} is invoked.  In the case where there is canceled and failed exceptions in the collection, this will prefer to throw the  failure as an  {@link ExecutionException} rather than obscure it with a {@link CancellationException}.  In other words   {@link CancellationException} will be thrown ONLY if there was canceled tasks, but NO tasks which finished in error.
 * @since 4.0.0
 * @param < T > The result object type returned from the futures
 * @param futures Structure of futures to iterate over and extract results from
 * @param ignoreFailedFutures {@code true} to ignore any failed or canceled futures
 * @return A {@link ListenableFuture} which will provide a list of the results from the provided futures
 */
public static <T>ListenableFuture<List<T>> makeResultListFuture(Iterable<? extends ListenableFuture<? extends T>> futures,final boolean ignoreFailedFutures){
  if (futures == null) {
    return immediateResultFuture(Collections.<T>emptyList());
  }
  ListenableFuture<List<ListenableFuture<? extends T>>> completeFuture=makeCompleteListFuture(futures);
  final SettableListenableFuture<List<T>> result;
  result=new CancelDelegateSettableListenableFuture<>(completeFuture);
  completeFuture.addCallback(new FutureCallback<List<ListenableFuture<? extends T>>>(){
    @Override public void handleResult(    List<ListenableFuture<? extends T>> resultFutures){
      boolean needToCancel=false;
      ArrayList<T> results=new ArrayList<>(resultFutures.size());
      Iterator<ListenableFuture<? extends T>> it=resultFutures.iterator();
      while (it.hasNext()) {
        ListenableFuture<? extends T> f=it.next();
        if (f.isCancelled()) {
          if (!ignoreFailedFutures) {
            needToCancel=true;
          }
          continue;
        }
        try {
          results.add(f.get());
        }
 catch (        ExecutionException e) {
          if (!ignoreFailedFutures) {
            result.setFailure(e.getCause());
            return;
          }
        }
catch (        Exception e) {
          result.setFailure(new Exception(e));
          return;
        }
      }
      if (needToCancel) {
        if (!result.cancel(false)) {
          throw new IllegalStateException();
        }
      }
 else {
        result.setResult(results);
      }
    }
    @Override public void handleFailure(    Throwable t){
      result.setFailure(t);
    }
  }
);
  return result;
}