11d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/*
21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2006 The Guava Authors
31d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
41d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License");
51d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * you may not use this file except in compliance with the License.
61d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * You may obtain a copy of the License at
71d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
81d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * http://www.apache.org/licenses/LICENSE-2.0
91d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Unless required by applicable law or agreed to in writing, software
111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS,
121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * See the License for the specific language governing permissions and
141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * limitations under the License.
151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */
161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpackage com.google.common.util.concurrent;
181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.base.Preconditions.checkArgument;
201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.base.Preconditions.checkNotNull;
211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.base.Preconditions.checkState;
221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.util.concurrent.MoreExecutors.sameThreadExecutor;
231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.util.concurrent.Uninterruptibles.getUninterruptibly;
241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.util.concurrent.Uninterruptibles.putUninterruptibly;
251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.util.concurrent.Uninterruptibles.takeUninterruptibly;
261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static java.lang.Thread.currentThread;
271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static java.util.Arrays.asList;
281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static java.util.concurrent.TimeUnit.NANOSECONDS;
291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.Beta;
311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.base.Function;
321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.base.Preconditions;
331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.collect.ImmutableList;
341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.collect.Lists;
351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.collect.Ordering;
361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.lang.reflect.Constructor;
381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.lang.reflect.InvocationTargetException;
391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.lang.reflect.UndeclaredThrowableException;
401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Arrays;
411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.List;
421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.concurrent.BlockingQueue;
431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.concurrent.CancellationException;
441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.concurrent.CountDownLatch;
451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.concurrent.ExecutionException;
461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.concurrent.Executor;
471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.concurrent.Future;
481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.concurrent.LinkedBlockingQueue;
491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.concurrent.TimeUnit;
501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.concurrent.TimeoutException;
511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.concurrent.atomic.AtomicInteger;
521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport javax.annotation.Nullable;
541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/**
561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Static utility methods pertaining to the {@link Future} interface.
571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @author Kevin Bourrillion
591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @author Nishant Thakkar
601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @author Sven Mawson
611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 1.0
621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */
631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@Beta
641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpublic final class Futures {
651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  private Futures() {}
661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Creates a {@link CheckedFuture} out of a normal {@link ListenableFuture}
691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * and a {@link Function} that maps from {@link Exception} instances into the
701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * appropriate checked type.
711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>The given mapping function will be applied to an
731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link InterruptedException}, a {@link CancellationException}, or an
741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link ExecutionException} with the actual cause of the exception.
751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * See {@link Future#get()} for details on the exceptions thrown.
761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 9.0 (source-compatible since 1.0)
781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <V, X extends Exception> CheckedFuture<V, X> makeChecked(
801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      ListenableFuture<V> future, Function<Exception, X> mapper) {
811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return new MappingCheckedFuture<V, X>(checkNotNull(future), mapper);
821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Creates a {@code ListenableFuture} which has its value set immediately upon
861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * construction. The getters just return the value. This {@code Future} can't
871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * be canceled or timed out and its {@code isDone()} method always returns
881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code true}.
891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <V> ListenableFuture<V> immediateFuture(@Nullable V value) {
911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    SettableFuture<V> future = SettableFuture.create();
921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    future.set(value);
931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return future;
941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns a {@code CheckedFuture} which has its value set immediately upon
981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * construction.
991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
1001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>The returned {@code Future} can't be cancelled, and its {@code isDone()}
1011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * method always returns {@code true}. Calling {@code get()} or {@code
1021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * checkedGet()} will immediately return the provided value.
1031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
1041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <V, X extends Exception> CheckedFuture<V, X>
1051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      immediateCheckedFuture(@Nullable V value) {
1061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    SettableFuture<V> future = SettableFuture.create();
1071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    future.set(value);
1081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return Futures.makeChecked(future, new Function<Exception, X>() {
1091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
1101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      public X apply(Exception e) {
1111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        throw new AssertionError("impossible");
1121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
1131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    });
1141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
1171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns a {@code ListenableFuture} which has an exception set immediately
1181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * upon construction.
1191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
1201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>The returned {@code Future} can't be cancelled, and its {@code isDone()}
1211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * method always returns {@code true}. Calling {@code get()} will immediately
1221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * throw the provided {@code Throwable} wrapped in an {@code
1231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * ExecutionException}.
1241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
1251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws Error if the throwable is an {@link Error}.
1261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
1271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <V> ListenableFuture<V> immediateFailedFuture(
1281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      Throwable throwable) {
1291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(throwable);
1301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    SettableFuture<V> future = SettableFuture.create();
1311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    future.setException(throwable);
1321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return future;
1331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
1361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns a {@code CheckedFuture} which has an exception set immediately upon
1371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * construction.
1381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
1391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>The returned {@code Future} can't be cancelled, and its {@code isDone()}
1401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * method always returns {@code true}. Calling {@code get()} will immediately
1411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * throw the provided {@code Throwable} wrapped in an {@code
1421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * ExecutionException}, and calling {@code checkedGet()} will throw the
1431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * provided exception itself.
1441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
1451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws Error if the throwable is an {@link Error}.
1461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
1471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <V, X extends Exception> CheckedFuture<V, X>
1481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      immediateFailedCheckedFuture(final X exception) {
1491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(exception);
1501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return makeChecked(Futures.<V>immediateFailedFuture(exception),
1511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        new Function<Exception, X>() {
1521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          @Override
1531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          public X apply(Exception e) {
1541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert            return exception;
1551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          }
1561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        });
1571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
1601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Returns a new {@code ListenableFuture} whose result is asynchronously
1611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * derived from the result of the given {@code Future}. More precisely, the
1621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * returned {@code Future} takes its result from a {@code Future} produced by
1631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * applying the given {@code Function} to the result of the original {@code
1641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Future}. Example:
1651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
1661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <pre>   {@code
1671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *   ListenableFuture<RowKey> rowKeyFuture = indexService.lookUp(query);
1681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *   Function<RowKey, ListenableFuture<QueryResult>> queryFunction =
1691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       new Function<RowKey, ListenableFuture<QueryResult>>() {
1701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         public ListenableFuture<QueryResult> apply(RowKey rowKey) {
1711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *           return dataService.read(rowKey);
1721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         }
1731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       };
1741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *   ListenableFuture<QueryResult> queryFuture =
1751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       chain(rowKeyFuture, queryFunction);
1761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * }</pre>
1771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
1781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Note: This overload of {@code chain} is designed for cases in which the
1791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * work of creating the derived future is fast and lightweight, as the method
1801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * does not accept an {@code Executor} in which to perform the the work. For
1811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * heavier derivations, this overload carries some caveats: First, the thread
1821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * that the derivation runs in depends on whether the input {@code Future} is
1831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * done at the time {@code chain} is called. In particular, if called late,
1841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code chain} will run the derivation in the thread that called {@code
1851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * chain}.  Second, derivations may run in an internal thread of the system
1861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * responsible for the input {@code Future}, such as an RPC network thread.
1871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Finally, during the execution of a {@code sameThreadExecutor} {@code
1881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * chain} function, all other registered but unexecuted listeners are
1891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * prevented from running, even if those listeners are to run in other
1901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * executors.
1911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
1921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>The returned {@code Future} attempts to keep its cancellation state in
1931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * sync with that of the input future and that of the future returned by the
1941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * chain function. That is, if the returned {@code Future} is cancelled, it
1951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * will attempt to cancel the other two, and if either of the other two is
1961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * cancelled, the returned {@code Future} will receive a callback in which it
1971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * will attempt to cancel itself.
1981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
1991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param input The future to chain
2001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param function A function to chain the results of the provided future
2011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     to the results of the returned future.  This will be run in the thread
2021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     that notifies input it is complete.
2031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return A future that holds result of the chain.
2041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @deprecated Convert your {@code Function} to a {@code AsyncFunction}, and
2051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     use {@link #transform(ListenableFuture, AsyncFunction)}. This method is
2061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     scheduled to be removed from Guava in Guava release 12.0.
2071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
2081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Deprecated
2091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <I, O> ListenableFuture<O> chain(
2101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      ListenableFuture<I> input,
2111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      Function<? super I, ? extends ListenableFuture<? extends O>> function) {
2121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return chain(input, function, MoreExecutors.sameThreadExecutor());
2131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
2141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
2161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Returns a new {@code ListenableFuture} whose result is asynchronously
2171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * derived from the result of the given {@code Future}. More precisely, the
2181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * returned {@code Future} takes its result from a {@code Future} produced by
2191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * applying the given {@code Function} to the result of the original {@code
2201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Future}. Example:
2211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
2221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <pre>   {@code
2231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *   ListenableFuture<RowKey> rowKeyFuture = indexService.lookUp(query);
2241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *   Function<RowKey, ListenableFuture<QueryResult>> queryFunction =
2251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       new Function<RowKey, ListenableFuture<QueryResult>>() {
2261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         public ListenableFuture<QueryResult> apply(RowKey rowKey) {
2271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *           return dataService.read(rowKey);
2281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         }
2291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       };
2301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *   ListenableFuture<QueryResult> queryFuture =
2311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       chain(rowKeyFuture, queryFunction, executor);
2321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * }</pre>
2331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
2341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>The returned {@code Future} attempts to keep its cancellation state in
2351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * sync with that of the input future and that of the future returned by the
2361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * chain function. That is, if the returned {@code Future} is cancelled, it
2371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * will attempt to cancel the other two, and if either of the other two is
2381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * cancelled, the returned {@code Future} will receive a callback in which it
2391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * will attempt to cancel itself.
2401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
2411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Note: For cases in which the work of creating the derived future is
2421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * fast and lightweight, consider {@linkplain Futures#chain(ListenableFuture,
2431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Function) the other overload} or explicit use of {@code
2441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * sameThreadExecutor}. For heavier derivations, this choice carries some
2451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * caveats: First, the thread that the derivation runs in depends on whether
2461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * the input {@code Future} is done at the time {@code chain} is called. In
2471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * particular, if called late, {@code chain} will run the derivation in the
2481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * thread that called {@code chain}. Second, derivations may run in an
2491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * internal thread of the system responsible for the input {@code Future},
2501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * such as an RPC network thread. Finally, during the execution of a {@code
2511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * sameThreadExecutor} {@code chain} function, all other registered but
2521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * unexecuted listeners are prevented from running, even if those listeners
2531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * are to run in other executors.
2541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
2551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param input The future to chain
2561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param function A function to chain the results of the provided future
2571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     to the results of the returned future.
2581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param executor Executor to run the function in.
2591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return A future that holds result of the chain.
2601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @deprecated Convert your {@code Function} to a {@code AsyncFunction}, and
2611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     use {@link #transform(ListenableFuture, AsyncFunction, Executor)}. This
2621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     method is scheduled to be removed from Guava in Guava release 12.0.
2631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
2641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Deprecated
2651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <I, O> ListenableFuture<O> chain(ListenableFuture<I> input,
2661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      final Function<? super I, ? extends ListenableFuture<? extends O>>
2671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          function,
2681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      Executor executor) {
2691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(function);
2701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    ChainingListenableFuture<I, O> chain =
2711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        new ChainingListenableFuture<I, O>(new AsyncFunction<I, O>() {
2721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          @Override
2731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          /*
2741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert           * All methods of ListenableFuture are covariant, and we don't expose
2751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert           * the object anywhere that would allow it to be downcast.
2761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert           */
2771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          @SuppressWarnings("unchecked")
2781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          public ListenableFuture<O> apply(I input) {
2791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert            return (ListenableFuture) function.apply(input);
2801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          }
2811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }, input);
2821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    input.addListener(chain, executor);
2831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return chain;
2841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
2851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
2871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns a new {@code ListenableFuture} whose result is asynchronously
2881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * derived from the result of the given {@code Future}. More precisely, the
2891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * returned {@code Future} takes its result from a {@code Future} produced by
2901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * applying the given {@code AsyncFunction} to the result of the original
2911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code Future}. Example:
2921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
2931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <pre>   {@code
2941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *   ListenableFuture<RowKey> rowKeyFuture = indexService.lookUp(query);
2951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *   AsyncFunction<RowKey, QueryResult> queryFunction =
2961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       new AsyncFunction<RowKey, QueryResult>() {
2971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         public ListenableFuture<QueryResult> apply(RowKey rowKey) {
2981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *           return dataService.read(rowKey);
2991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         }
3001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       };
3011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *   ListenableFuture<QueryResult> queryFuture =
3021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       transform(rowKeyFuture, queryFunction);
3031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * }</pre>
3041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
3051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Note: This overload of {@code transform} is designed for cases in which
3061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * the work of creating the derived {@code Future} is fast and lightweight,
3071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * as the method does not accept an {@code Executor} in which to perform the
3081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * the work. (The created {@code Future} itself need not complete quickly.)
3091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * For heavier operations, this overload carries some caveats: First, the
3101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * thread that {@code function.apply} runs in depends on whether the input
3111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code Future} is done at the time {@code transform} is called. In
3121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * particular, if called late, {@code transform} will run the operation in
3131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * the thread that called {@code transform}.  Second, {@code function.apply}
3141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * may run in an internal thread of the system responsible for the input
3151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code Future}, such as an RPC network thread.  Finally, during the
3161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * execution of a {@code sameThreadExecutor} {@code function.apply}, all
3171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * other registered but unexecuted listeners are prevented from running, even
3181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * if those listeners are to run in other executors.
3191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
3201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>The returned {@code Future} attempts to keep its cancellation state in
3211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * sync with that of the input future and that of the future returned by the
3221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * function. That is, if the returned {@code Future} is cancelled, it will
3231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * attempt to cancel the other two, and if either of the other two is
3241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * cancelled, the returned {@code Future} will receive a callback in which it
3251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * will attempt to cancel itself.
3261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
3271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param input The future to transform
3281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param function A function to transform the result of the input future
3291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     to the result of the output future
3301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return A future that holds result of the function (if the input succeeded)
3311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     or the original input's failure (if not)
3321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 11.0
3331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
3341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <I, O> ListenableFuture<O> transform(ListenableFuture<I> input,
3351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      AsyncFunction<? super I, ? extends O> function) {
3361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return transform(input, function, MoreExecutors.sameThreadExecutor());
3371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
3381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
3391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
3401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns a new {@code ListenableFuture} whose result is asynchronously
3411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * derived from the result of the given {@code Future}. More precisely, the
3421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * returned {@code Future} takes its result from a {@code Future} produced by
3431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * applying the given {@code AsyncFunction} to the result of the original
3441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code Future}. Example:
3451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
3461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <pre>   {@code
3471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *   ListenableFuture<RowKey> rowKeyFuture = indexService.lookUp(query);
3481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *   AsyncFunction<RowKey, QueryResult> queryFunction =
3491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       new AsyncFunction<RowKey, QueryResult>() {
3501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         public ListenableFuture<QueryResult> apply(RowKey rowKey) {
3511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *           return dataService.read(rowKey);
3521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         }
3531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       };
3541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *   ListenableFuture<QueryResult> queryFuture =
3551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       transform(rowKeyFuture, queryFunction, executor);
3561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * }</pre>
3571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
3581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>The returned {@code Future} attempts to keep its cancellation state in
3591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * sync with that of the input future and that of the future returned by the
3601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * chain function. That is, if the returned {@code Future} is cancelled, it
3611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * will attempt to cancel the other two, and if either of the other two is
3621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * cancelled, the returned {@code Future} will receive a callback in which it
3631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * will attempt to cancel itself.
3641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
3651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Note: For cases in which the work of creating the derived future is
3661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * fast and lightweight, consider {@linkplain
3671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Futures#transform(ListenableFuture, Function) the other overload} or
3681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * explicit use of {@code sameThreadExecutor}. For heavier derivations, this
3691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * choice carries some caveats: First, the thread that {@code function.apply}
3701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * runs in depends on whether the input {@code Future} is done at the time
3711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code transform} is called. In particular, if called late, {@code
3721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * transform} will run the operation in the thread that called {@code
3731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * transform}.  Second, {@code function.apply} may run in an internal thread
3741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * of the system responsible for the input {@code Future}, such as an RPC
3751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * network thread.  Finally, during the execution of a {@code
3761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * sameThreadExecutor} {@code function.apply}, all other registered but
3771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * unexecuted listeners are prevented from running, even if those listeners
3781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * are to run in other executors.
3791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
3801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param input The future to transform
3811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param function A function to transform the result of the input future
3821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     to the result of the output future
3831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param executor Executor to run the function in.
3841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return A future that holds result of the function (if the input succeeded)
3851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     or the original input's failure (if not)
3861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 11.0
3871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
3881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <I, O> ListenableFuture<O> transform(ListenableFuture<I> input,
3891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      AsyncFunction<? super I, ? extends O> function,
3901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      Executor executor) {
3911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    ChainingListenableFuture<I, O> output =
3921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        new ChainingListenableFuture<I, O>(function, input);
3931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    input.addListener(output, executor);
3941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return output;
3951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
3961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
3971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
3981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns a new {@code ListenableFuture} whose result is the product of
3991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * applying the given {@code Function} to the result of the given {@code
4001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Future}. Example:
4011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
4021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <pre>   {@code
4031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *   ListenableFuture<QueryResult> queryFuture = ...;
4041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *   Function<QueryResult, List<Row>> rowsFunction =
4051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       new Function<QueryResult, List<Row>>() {
4061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         public List<Row> apply(QueryResult queryResult) {
4071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *           return queryResult.getRows();
4081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         }
4091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       };
4101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *   ListenableFuture<List<Row>> rowsFuture =
4111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       transform(queryFuture, rowsFunction);
4121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * }</pre>
4131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
4141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Note: This overload of {@code transform} is designed for cases in which
4151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * the transformation is fast and lightweight, as the method does not accept
4161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * an {@code Executor} in which to perform the the work. For heavier
4171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * transformations, this overload carries some caveats: First, the thread
4181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * that the transformation runs in depends on whether the input {@code
4191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Future} is done at the time {@code transform} is called. In particular, if
4201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * called late, {@code transform} will perform the transformation in the
4211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * thread that called {@code transform}. Second, transformations may run in
4221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * an internal thread of the system responsible for the input {@code Future},
4231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * such as an RPC network thread. Finally, during the execution of a {@code
4241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * sameThreadExecutor} transformation, all other registered but unexecuted
4251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * listeners are prevented from running, even if those listeners are to run
4261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * in other executors.
4271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
4281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>The returned {@code Future} attempts to keep its cancellation state in
4291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * sync with that of the input future. That is, if the returned {@code Future}
4301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * is cancelled, it will attempt to cancel the input, and if the input is
4311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * cancelled, the returned {@code Future} will receive a callback in which it
4321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * will attempt to cancel itself.
4331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
4341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>An example use of this method is to convert a serializable object
4351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * returned from an RPC into a POJO.
4361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
4371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param future The future to transform
4381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param function A Function to transform the results of the provided future
4391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     to the results of the returned future.  This will be run in the thread
4401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     that notifies input it is complete.
4411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return A future that holds result of the transformation.
4421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 9.0 (in 1.0 as {@code compose})
4431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
4441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <I, O> ListenableFuture<O> transform(ListenableFuture<I> future,
4451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      final Function<? super I, ? extends O> function) {
4461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return transform(future, function, MoreExecutors.sameThreadExecutor());
4471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
4481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
4491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
4501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns a new {@code ListenableFuture} whose result is the product of
4511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * applying the given {@code Function} to the result of the given {@code
4521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Future}. Example:
4531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
4541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <pre>   {@code
4551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *   ListenableFuture<QueryResult> queryFuture = ...;
4561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *   Function<QueryResult, List<Row>> rowsFunction =
4571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       new Function<QueryResult, List<Row>>() {
4581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         public List<Row> apply(QueryResult queryResult) {
4591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *           return queryResult.getRows();
4601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         }
4611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       };
4621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *   ListenableFuture<List<Row>> rowsFuture =
4631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       transform(queryFuture, rowsFunction, executor);
4641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * }</pre>
4651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
4661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>The returned {@code Future} attempts to keep its cancellation state in
4671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * sync with that of the input future. That is, if the returned {@code Future}
4681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * is cancelled, it will attempt to cancel the input, and if the input is
4691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * cancelled, the returned {@code Future} will receive a callback in which it
4701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * will attempt to cancel itself.
4711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
4721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>An example use of this method is to convert a serializable object
4731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * returned from an RPC into a POJO.
4741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
4751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Note: For cases in which the transformation is fast and lightweight,
4761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * consider {@linkplain Futures#transform(ListenableFuture, Function) the
4771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * other overload} or explicit use of {@link
4781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * MoreExecutors#sameThreadExecutor}. For heavier transformations, this
4791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * choice carries some caveats: First, the thread that the transformation
4801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * runs in depends on whether the input {@code Future} is done at the time
4811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code transform} is called. In particular, if called late, {@code
4821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * transform} will perform the transformation in the thread that called
4831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code transform}.  Second, transformations may run in an internal thread
4841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * of the system responsible for the input {@code Future}, such as an RPC
4851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * network thread.  Finally, during the execution of a {@code
4861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * sameThreadExecutor} transformation, all other registered but unexecuted
4871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * listeners are prevented from running, even if those listeners are to run
4881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * in other executors.
4891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
4901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param future The future to transform
4911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param function A Function to transform the results of the provided future
4921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     to the results of the returned future.
4931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param executor Executor to run the function in.
4941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return A future that holds result of the transformation.
4951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 9.0 (in 2.0 as {@code compose})
4961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
4971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <I, O> ListenableFuture<O> transform(ListenableFuture<I> future,
4981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      final Function<? super I, ? extends O> function, Executor executor) {
4991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(function);
5001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    Function<I, ListenableFuture<O>> wrapperFunction
5011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        = new Function<I, ListenableFuture<O>>() {
5021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert            @Override public ListenableFuture<O> apply(I input) {
5031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert              O output = function.apply(input);
5041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert              return immediateFuture(output);
5051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert            }
5061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        };
5071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return chain(future, wrapperFunction, executor);
5081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
5091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
5101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
5111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Like {@link #transform(ListenableFuture, Function)} except that the
5121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * transformation {@code function} is invoked on each call to
5131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link Future#get() get()} on the returned future.
5141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
5151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>The returned {@code Future} reflects the input's cancellation
5161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * state directly, and any attempt to cancel the returned Future is likewise
5171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * passed through to the input Future.
5181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
5191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Note that calls to {@linkplain Future#get(long, TimeUnit) timed get}
5201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * only apply the timeout to the execution of the underlying {@code Future},
5211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <em>not</em> to the execution of the transformation function.
5221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
5231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>The primary audience of this method is callers of {@code transform}
5241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * who don't have a {@code ListenableFuture} available and
5251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * do not mind repeated, lazy function evaluation.
5261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
5271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param future The future to transform
5281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param function A Function to transform the results of the provided future
5291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     to the results of the returned future.
5301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return A future that returns the result of the transformation.
5311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 10.0
5321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
5331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Beta
5341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <I, O> Future<O> lazyTransform(final Future<I> future,
5351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      final Function<? super I, ? extends O> function) {
5361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(future);
5371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(function);
5381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return new Future<O>() {
5391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
5401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
5411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      public boolean cancel(boolean mayInterruptIfRunning) {
5421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return future.cancel(mayInterruptIfRunning);
5431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
5441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
5451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
5461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      public boolean isCancelled() {
5471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return future.isCancelled();
5481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
5491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
5501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
5511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      public boolean isDone() {
5521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return future.isDone();
5531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
5541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
5551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
5561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      public O get() throws InterruptedException, ExecutionException {
5571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return applyTransformation(future.get());
5581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
5591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
5601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
5611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      public O get(long timeout, TimeUnit unit)
5621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          throws InterruptedException, ExecutionException, TimeoutException {
5631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return applyTransformation(future.get(timeout, unit));
5641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
5651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
5661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      private O applyTransformation(I input) throws ExecutionException {
5671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        try {
5681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          return function.apply(input);
5691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        } catch (Throwable t) {
5701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          throw new ExecutionException(t);
5711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
5721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
5731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    };
5741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
5751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
5761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
5771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * An implementation of {@code ListenableFuture} that also implements
5781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code Runnable} so that it can be used to nest ListenableFutures.
5791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Once the passed-in {@code ListenableFuture} is complete, it calls the
5801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * passed-in {@code Function} to generate the result.
5811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
5821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>If the function throws any checked exceptions, they should be wrapped
5831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * in a {@code UndeclaredThrowableException} so that this class can get
5841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * access to the cause.
5851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
5861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  private static class ChainingListenableFuture<I, O>
5871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      extends AbstractFuture<O> implements Runnable {
5881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
5891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    private AsyncFunction<? super I, ? extends O> function;
5901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    private ListenableFuture<? extends I> inputFuture;
5911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    private volatile ListenableFuture<? extends O> outputFuture;
5921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    private final BlockingQueue<Boolean> mayInterruptIfRunningChannel =
5931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        new LinkedBlockingQueue<Boolean>(1);
5941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    private final CountDownLatch outputCreated = new CountDownLatch(1);
5951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
5961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    private ChainingListenableFuture(
5971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        AsyncFunction<? super I, ? extends O> function,
5981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        ListenableFuture<? extends I> inputFuture) {
5991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      this.function = checkNotNull(function);
6001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      this.inputFuture = checkNotNull(inputFuture);
6011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
6021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
6031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    /**
6041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * Delegate the get() to the input and output futures, in case
6051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * their implementations defer starting computation until their
6061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * own get() is invoked.
6071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     */
6081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
6091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public O get() throws InterruptedException, ExecutionException {
6101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      if (!isDone()) {
6111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // Invoking get on the inputFuture will ensure our own run()
6121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // method below is invoked as a listener when inputFuture sets
6131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // its value.  Therefore when get() returns we should then see
6141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // the outputFuture be created.
6151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        ListenableFuture<? extends I> inputFuture = this.inputFuture;
6161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        if (inputFuture != null) {
6171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          inputFuture.get();
6181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
6191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
6201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // If our listener was scheduled to run on an executor we may
6211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // need to wait for our listener to finish running before the
6221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // outputFuture has been constructed by the function.
6231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        outputCreated.await();
6241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
6251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // Like above with the inputFuture, we have a listener on
6261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // the outputFuture that will set our own value when its
6271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // value is set.  Invoking get will ensure the output can
6281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // complete and invoke our listener, so that we can later
6291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // get the result.
6301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        ListenableFuture<? extends O> outputFuture = this.outputFuture;
6311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        if (outputFuture != null) {
6321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          outputFuture.get();
6331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
6341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
6351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return super.get();
6361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
6371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
6381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    /**
6391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * Delegate the get() to the input and output futures, in case
6401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * their implementations defer starting computation until their
6411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * own get() is invoked.
6421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     */
6431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
6441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public O get(long timeout, TimeUnit unit) throws TimeoutException,
6451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        ExecutionException, InterruptedException {
6461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      if (!isDone()) {
6471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // Use a single time unit so we can decrease remaining timeout
6481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // as we wait for various phases to complete.
6491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        if (unit != NANOSECONDS) {
6501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          timeout = NANOSECONDS.convert(timeout, unit);
6511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          unit = NANOSECONDS;
6521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
6531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
6541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // Invoking get on the inputFuture will ensure our own run()
6551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // method below is invoked as a listener when inputFuture sets
6561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // its value.  Therefore when get() returns we should then see
6571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // the outputFuture be created.
6581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        ListenableFuture<? extends I> inputFuture = this.inputFuture;
6591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        if (inputFuture != null) {
6601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          long start = System.nanoTime();
6611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          inputFuture.get(timeout, unit);
6621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          timeout -= Math.max(0, System.nanoTime() - start);
6631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
6641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
6651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // If our listener was scheduled to run on an executor we may
6661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // need to wait for our listener to finish running before the
6671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // outputFuture has been constructed by the function.
6681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        long start = System.nanoTime();
6691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        if (!outputCreated.await(timeout, unit)) {
6701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          throw new TimeoutException();
6711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
6721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        timeout -= Math.max(0, System.nanoTime() - start);
6731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
6741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // Like above with the inputFuture, we have a listener on
6751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // the outputFuture that will set our own value when its
6761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // value is set.  Invoking get will ensure the output can
6771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // complete and invoke our listener, so that we can later
6781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // get the result.
6791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        ListenableFuture<? extends O> outputFuture = this.outputFuture;
6801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        if (outputFuture != null) {
6811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          outputFuture.get(timeout, unit);
6821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
6831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
6841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return super.get(timeout, unit);
6851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
6861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
6871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
6881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public boolean cancel(boolean mayInterruptIfRunning) {
6891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      /*
6901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert       * Our additional cancellation work needs to occur even if
6911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert       * !mayInterruptIfRunning, so we can't move it into interruptTask().
6921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert       */
6931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      if (super.cancel(mayInterruptIfRunning)) {
6941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // This should never block since only one thread is allowed to cancel
6951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // this Future.
6961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        putUninterruptibly(mayInterruptIfRunningChannel, mayInterruptIfRunning);
6971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        cancel(inputFuture, mayInterruptIfRunning);
6981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        cancel(outputFuture, mayInterruptIfRunning);
6991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return true;
7001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
7011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return false;
7021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
7031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
7041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    private void cancel(@Nullable Future<?> future,
7051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        boolean mayInterruptIfRunning) {
7061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      if (future != null) {
7071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        future.cancel(mayInterruptIfRunning);
7081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
7091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
7101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
7111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
7121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public void run() {
7131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      try {
7141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        I sourceResult;
7151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        try {
7161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          sourceResult = getUninterruptibly(inputFuture);
7171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        } catch (CancellationException e) {
7181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          // Cancel this future and return.
7191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          // At this point, inputFuture is cancelled and outputFuture doesn't
7201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          // exist, so the value of mayInterruptIfRunning is irrelevant.
7211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          cancel(false);
7221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          return;
7231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        } catch (ExecutionException e) {
7241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          // Set the cause of the exception as this future's exception
7251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          setException(e.getCause());
7261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          return;
7271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
7281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
7291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        final ListenableFuture<? extends O> outputFuture = this.outputFuture =
7301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert            function.apply(sourceResult);
7311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        if (isCancelled()) {
7321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          // Handles the case where cancel was called while the function was
7331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          // being applied.
7341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          // There is a gap in cancel(boolean) between calling sync.cancel()
7351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          // and storing the value of mayInterruptIfRunning, so this thread
7361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          // needs to block, waiting for that value.
7371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          outputFuture.cancel(
7381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert              takeUninterruptibly(mayInterruptIfRunningChannel));
7391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          this.outputFuture = null;
7401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          return;
7411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
7421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        outputFuture.addListener(new Runnable() {
7431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert            @Override
7441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert            public void run() {
7451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert              try {
7461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert                // Here it would have been nice to have had an
7471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert                // UninterruptibleListenableFuture, but we don't want to start a
7481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert                // combinatorial explosion of interfaces, so we have to make do.
7491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert                set(getUninterruptibly(outputFuture));
7501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert              } catch (CancellationException e) {
7511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert                // Cancel this future and return.
7521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert                // At this point, inputFuture and outputFuture are done, so the
7531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert                // value of mayInterruptIfRunning is irrelevant.
7541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert                cancel(false);
7551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert                return;
7561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert              } catch (ExecutionException e) {
7571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert                // Set the cause of the exception as this future's exception
7581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert                setException(e.getCause());
7591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert              } finally {
7601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert                // Don't pin inputs beyond completion
7611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert                ChainingListenableFuture.this.outputFuture = null;
7621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert              }
7631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert            }
7641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          }, MoreExecutors.sameThreadExecutor());
7651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      } catch (UndeclaredThrowableException e) {
7661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // Set the cause of the exception as this future's exception
7671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        setException(e.getCause());
7681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      } catch (Exception e) {
7691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // This exception is irrelevant in this thread, but useful for the
7701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // client
7711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        setException(e);
7721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      } catch (Error e) {
7731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // Propagate errors up ASAP - our superclass will rethrow the error
7741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        setException(e);
7751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      } finally {
7761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // Don't pin inputs beyond completion
7771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        function = null;
7781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        inputFuture = null;
7791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // Allow our get routines to examine outputFuture now.
7801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        outputCreated.countDown();
7811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
7821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
7831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
7841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
7851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
7861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Creates a new {@code ListenableFuture} whose value is a list containing the
7871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * values of all its input futures, if all succeed. If any input fails, the
7881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * returned future fails.
7891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
7901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>The list of results is in the same order as the input list.
7911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
7921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Canceling this future does not cancel any of the component futures;
7931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * however, if any of the provided futures fails or is canceled, this one is,
7941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * too.
7951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
7961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param futures futures to combine
7971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return a future that provides a list of the results of the component
7981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         futures
7991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 10.0
8001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
8011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Beta
8021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <V> ListenableFuture<List<V>> allAsList(
8031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      ListenableFuture<? extends V>... futures) {
8041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return new ListFuture<V>(ImmutableList.copyOf(futures), true,
8051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        MoreExecutors.sameThreadExecutor());
8061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
8071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
8091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Creates a new {@code ListenableFuture} whose value is a list containing the
8101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * values of all its input futures, if all succeed. If any input fails, the
8111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * returned future fails.
8121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
8131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>The list of results is in the same order as the input list.
8141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
8151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Canceling this future does not cancel any of the component futures;
8161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * however, if any of the provided futures fails or is canceled, this one is,
8171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * too.
8181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
8191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param futures futures to combine
8201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return a future that provides a list of the results of the component
8211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         futures
8221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 10.0
8231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
8241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Beta
8251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <V> ListenableFuture<List<V>> allAsList(
8261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      Iterable<? extends ListenableFuture<? extends V>> futures) {
8271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return new ListFuture<V>(ImmutableList.copyOf(futures), true,
8281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        MoreExecutors.sameThreadExecutor());
8291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
8301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
8321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Creates a new {@code ListenableFuture} whose value is a list containing the
8331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * values of all its successful input futures. The list of results is in the
8341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * same order as the input list, and if any of the provided futures fails or
8351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * is canceled, its corresponding position will contain {@code null} (which is
8361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * indistinguishable from the future having a successful value of
8371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code null}).
8381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
8391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param futures futures to combine
8401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return a future that provides a list of the results of the component
8411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         futures
8421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 10.0
8431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
8441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Beta
8451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <V> ListenableFuture<List<V>> successfulAsList(
8461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      ListenableFuture<? extends V>... futures) {
8471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return new ListFuture<V>(ImmutableList.copyOf(futures), false,
8481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        MoreExecutors.sameThreadExecutor());
8491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
8501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
8521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Creates a new {@code ListenableFuture} whose value is a list containing the
8531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * values of all its successful input futures. The list of results is in the
8541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * same order as the input list, and if any of the provided futures fails or
8551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * is canceled, its corresponding position will contain {@code null} (which is
8561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * indistinguishable from the future having a successful value of
8571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code null}).
8581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
8591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param futures futures to combine
8601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return a future that provides a list of the results of the component
8611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         futures
8621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 10.0
8631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
8641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Beta
8651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <V> ListenableFuture<List<V>> successfulAsList(
8661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      Iterable<? extends ListenableFuture<? extends V>> futures) {
8671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return new ListFuture<V>(ImmutableList.copyOf(futures), false,
8681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        MoreExecutors.sameThreadExecutor());
8691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
8701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
8721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Registers separate success and failure callbacks to be run when the {@code
8731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Future}'s computation is {@linkplain java.util.concurrent.Future#isDone()
8741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * complete} or, if the computation is already complete, immediately.
8751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
8761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>There is no guaranteed ordering of execution of callbacks, but any
8771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * callback added through this method is guaranteed to be called once the
8781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * computation is complete.
8791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
8801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Example: <pre> {@code
8811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * ListenableFuture<QueryResult> future = ...;
8821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * addCallback(future,
8831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     new FutureCallback<QueryResult> {
8841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       public void onSuccess(QueryResult result) {
8851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         storeInCache(result);
8861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       }
8871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       public void onFailure(Throwable t) {
8881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         reportError(t);
8891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       }
8901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     });}</pre>
8911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
8921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Note: This overload of {@code addCallback} is designed for cases in
8931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * which the callack is fast and lightweight, as the method does not accept
8941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * an {@code Executor} in which to perform the the work. For heavier
8951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * callbacks, this overload carries some caveats: First, the thread that the
8961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * callback runs in depends on whether the input {@code Future} is done at the
8971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * time {@code addCallback} is called and on whether the input {@code Future}
8981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * is ever cancelled. In particular, {@code addCallback} may execute the
8991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * callback in the thread that calls {@code addCallback} or {@code
9001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Future.cancel}. Second, callbacks may run in an internal thread of the
9011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * system responsible for the input {@code Future}, such as an RPC network
9021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * thread. Finally, during the execution of a {@code sameThreadExecutor}
9031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * callback, all other registered but unexecuted listeners are prevented from
9041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * running, even if those listeners are to run in other executors.
9051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
9061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>For a more general interface to attach a completion listener to a
9071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code Future}, see {@link ListenableFuture#addListener addListener}.
9081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
9091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param future The future attach the callback to.
9101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param callback The callback to invoke when {@code future} is completed.
9111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 10.0
9121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
9131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <V> void addCallback(ListenableFuture<V> future,
9141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      FutureCallback<? super V> callback) {
9151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    addCallback(future, callback, MoreExecutors.sameThreadExecutor());
9161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
9171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
9191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Registers separate success and failure callbacks to be run when the {@code
9201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Future}'s computation is {@linkplain java.util.concurrent.Future#isDone()
9211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * complete} or, if the computation is already complete, immediately.
9221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
9231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>The callback is run in {@code executor}.
9241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * There is no guaranteed ordering of execution of callbacks, but any
9251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * callback added through this method is guaranteed to be called once the
9261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * computation is complete.
9271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
9281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Example: <pre> {@code
9291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * ListenableFuture<QueryResult> future = ...;
9301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Executor e = ...
9311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * addCallback(future, e,
9321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     new FutureCallback<QueryResult> {
9331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       public void onSuccess(QueryResult result) {
9341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         storeInCache(result);
9351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       }
9361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       public void onFailure(Throwable t) {
9371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         reportError(t);
9381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *       }
9391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     });}</pre>
9401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
9411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * When the callback is fast and lightweight consider {@linkplain
9421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Futures#addCallback(ListenableFuture, FutureCallback) the other overload}
9431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * or explicit use of {@link MoreExecutors#sameThreadExecutor
9441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * sameThreadExecutor}. For heavier callbacks, this choice carries some
9451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * caveats: First, the thread that the callback runs in depends on whether
9461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * the input {@code Future} is done at the time {@code addCallback} is called
9471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * and on whether the input {@code Future} is ever cancelled. In particular,
9481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code addCallback} may execute the callback in the thread that calls
9491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code addCallback} or {@code Future.cancel}. Second, callbacks may run in
9501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * an internal thread of the system responsible for the input {@code Future},
9511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * such as an RPC network thread. Finally, during the execution of a {@code
9521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * sameThreadExecutor} callback, all other registered but unexecuted
9531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * listeners are prevented from running, even if those listeners are to run
9541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * in other executors.
9551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
9561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>For a more general interface to attach a completion listener to a
9571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code Future}, see {@link ListenableFuture#addListener addListener}.
9581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
9591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param future The future attach the callback to.
9601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param callback The callback to invoke when {@code future} is completed.
9611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param executor The executor to run {@code callback} when the future
9621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *    completes.
9631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 10.0
9641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
9651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <V> void addCallback(final ListenableFuture<V> future,
9661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      final FutureCallback<? super V> callback, Executor executor) {
9671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    Preconditions.checkNotNull(callback);
9681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    Runnable callbackListener = new Runnable() {
9691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
9701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      public void run() {
9711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        try {
9721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          // TODO(user): (Before Guava release), validate that this
9731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          // is the thing for IE.
9741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          V value = getUninterruptibly(future);
9751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          callback.onSuccess(value);
9761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        } catch (ExecutionException e) {
9771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          callback.onFailure(e.getCause());
9781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        } catch (RuntimeException e) {
9791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          callback.onFailure(e);
9801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        } catch (Error e) {
9811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          callback.onFailure(e);
9821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
9831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
9841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    };
9851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    future.addListener(callbackListener, executor);
9861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
9871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
9891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns the result of {@link Future#get()}, converting most exceptions to a
9901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * new instance of the given checked exception type. This reduces boilerplate
9911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * for a common use of {@code Future} in which it is unnecessary to
9921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * programmatically distinguish between exception types or to extract other
9931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * information from the exception instance.
9941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
9951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Exceptions from {@code Future.get} are treated as follows:
9961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <ul>
9971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <li>Any {@link ExecutionException} has its <i>cause</i> wrapped in an
9981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     {@code X} if the cause is a checked exception, an {@link
9991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     UncheckedExecutionException} if the cause is a {@code
10001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     RuntimeException}, or an {@link ExecutionError} if the cause is an
10011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     {@code Error}.
10021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <li>Any {@link InterruptedException} is wrapped in an {@code X} (after
10031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     restoring the interrupt).
10041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <li>Any {@link CancellationException} is propagated untouched, as is any
10051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     other {@link RuntimeException} (though {@code get} implementations are
10061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     discouraged from throwing such exceptions).
10071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * </ul>
10081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
10091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * The overall principle is to continue to treat every checked exception as a
10101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * checked exception, every unchecked exception as an unchecked exception, and
10111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * every error as an error. In addition, the cause of any {@code
10121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * ExecutionException} is wrapped in order to ensure that the new stack trace
10131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * matches that of the current thread.
10141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
10151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Instances of {@code exceptionClass} are created by choosing an arbitrary
10161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * public constructor that accepts zero or more arguments, all of type {@code
10171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * String} or {@code Throwable} (preferring constructors with at least one
10181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code String}) and calling the constructor via reflection. If the
10191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * exception did not already have a cause, one is set by calling {@link
10201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Throwable#initCause(Throwable)} on it. If no such constructor exists, an
10211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code IllegalArgumentException} is thrown.
10221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
10231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws X if {@code get} throws any checked exception except for an {@code
10241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         ExecutionException} whose cause is not itself a checked exception
10251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws UncheckedExecutionException if {@code get} throws an {@code
10261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         ExecutionException} with a {@code RuntimeException} as its cause
10271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws ExecutionError if {@code get} throws an {@code ExecutionException}
10281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         with an {@code Error} as its cause
10291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws CancellationException if {@code get} throws a {@code
10301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         CancellationException}
10311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IllegalArgumentException if {@code exceptionClass} extends {@code
10321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         RuntimeException} or does not have a suitable constructor
10331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 10.0
10341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
10351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Beta
10361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <V, X extends Exception> V get(
10371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      Future<V> future, Class<X> exceptionClass) throws X {
10381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(future);
10391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkArgument(!RuntimeException.class.isAssignableFrom(exceptionClass),
10401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        "Futures.get exception type (%s) must not be a RuntimeException",
10411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        exceptionClass);
10421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    try {
10431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return future.get();
10441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    } catch (InterruptedException e) {
10451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      currentThread().interrupt();
10461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      throw newWithCause(exceptionClass, e);
10471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    } catch (ExecutionException e) {
10481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      wrapAndThrowExceptionOrError(e.getCause(), exceptionClass);
10491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      throw new AssertionError();
10501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
10511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
10521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
10531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
10541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns the result of {@link Future#get(long, TimeUnit)}, converting most
10551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * exceptions to a new instance of the given checked exception type. This
10561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * reduces boilerplate for a common use of {@code Future} in which it is
10571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * unnecessary to programmatically distinguish between exception types or to
10581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * extract other information from the exception instance.
10591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
10601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Exceptions from {@code Future.get} are treated as follows:
10611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <ul>
10621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <li>Any {@link ExecutionException} has its <i>cause</i> wrapped in an
10631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     {@code X} if the cause is a checked exception, an {@link
10641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     UncheckedExecutionException} if the cause is a {@code
10651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     RuntimeException}, or an {@link ExecutionError} if the cause is an
10661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     {@code Error}.
10671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <li>Any {@link InterruptedException} is wrapped in an {@code X} (after
10681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     restoring the interrupt).
10691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <li>Any {@link TimeoutException} is wrapped in an {@code X}.
10701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <li>Any {@link CancellationException} is propagated untouched, as is any
10711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     other {@link RuntimeException} (though {@code get} implementations are
10721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     discouraged from throwing such exceptions).
10731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * </ul>
10741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
10751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * The overall principle is to continue to treat every checked exception as a
10761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * checked exception, every unchecked exception as an unchecked exception, and
10771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * every error as an error. In addition, the cause of any {@code
10781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * ExecutionException} is wrapped in order to ensure that the new stack trace
10791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * matches that of the current thread.
10801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
10811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Instances of {@code exceptionClass} are created by choosing an arbitrary
10821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * public constructor that accepts zero or more arguments, all of type {@code
10831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * String} or {@code Throwable} (preferring constructors with at least one
10841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code String}) and calling the constructor via reflection. If the
10851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * exception did not already have a cause, one is set by calling {@link
10861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Throwable#initCause(Throwable)} on it. If no such constructor exists, an
10871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code IllegalArgumentException} is thrown.
10881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
10891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws X if {@code get} throws any checked exception except for an {@code
10901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         ExecutionException} whose cause is not itself a checked exception
10911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws UncheckedExecutionException if {@code get} throws an {@code
10921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         ExecutionException} with a {@code RuntimeException} as its cause
10931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws ExecutionError if {@code get} throws an {@code ExecutionException}
10941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         with an {@code Error} as its cause
10951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws CancellationException if {@code get} throws a {@code
10961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         CancellationException}
10971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IllegalArgumentException if {@code exceptionClass} extends {@code
10981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         RuntimeException} or does not have a suitable constructor
10991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 10.0
11001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
11011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Beta
11021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <V, X extends Exception> V get(
11031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      Future<V> future, long timeout, TimeUnit unit, Class<X> exceptionClass)
11041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      throws X {
11051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(future);
11061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(unit);
11071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkArgument(!RuntimeException.class.isAssignableFrom(exceptionClass),
11081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        "Futures.get exception type (%s) must not be a RuntimeException",
11091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        exceptionClass);
11101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    try {
11111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return future.get(timeout, unit);
11121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    } catch (InterruptedException e) {
11131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      currentThread().interrupt();
11141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      throw newWithCause(exceptionClass, e);
11151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    } catch (TimeoutException e) {
11161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      throw newWithCause(exceptionClass, e);
11171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    } catch (ExecutionException e) {
11181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      wrapAndThrowExceptionOrError(e.getCause(), exceptionClass);
11191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      throw new AssertionError();
11201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
11211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
11221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
11231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  private static <X extends Exception> void wrapAndThrowExceptionOrError(
11241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      Throwable cause, Class<X> exceptionClass) throws X {
11251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (cause instanceof Error) {
11261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      throw new ExecutionError((Error) cause);
11271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
11281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (cause instanceof RuntimeException) {
11291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      throw new UncheckedExecutionException(cause);
11301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
11311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    throw newWithCause(exceptionClass, cause);
11321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
11331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
11341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
11351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns the result of calling {@link Future#get()} uninterruptibly on a
11361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * task known not to throw a checked exception. This makes {@code Future} more
11371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * suitable for lightweight, fast-running tasks that, barring bugs in the
11381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * code, will not fail. This gives it exception-handling behavior similar to
11391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * that of {@code ForkJoinTask.join}.
11401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
11411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Exceptions from {@code Future.get} are treated as follows:
11421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <ul>
11431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <li>Any {@link ExecutionException} has its <i>cause</i> wrapped in an
11441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     {@link UncheckedExecutionException} (if the cause is an {@code
11451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     Exception}) or {@link ExecutionError} (if the cause is an {@code
11461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     Error}).
11471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <li>Any {@link InterruptedException} causes a retry of the {@code get}
11481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     call. The interrupt is restored before {@code getUnchecked} returns.
11491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <li>Any {@link CancellationException} is propagated untouched. So is any
11501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     other {@link RuntimeException} ({@code get} implementations are
11511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     discouraged from throwing such exceptions).
11521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * </ul>
11531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
11541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * The overall principle is to eliminate all checked exceptions: to loop to
11551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * avoid {@code InterruptedException}, to pass through {@code
11561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * CancellationException}, and to wrap any exception from the underlying
11571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * computation in an {@code UncheckedExecutionException} or {@code
11581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * ExecutionError}.
11591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
11601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>For an uninterruptible {@code get} that preserves other exceptions, see
11611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link Uninterruptibles#getUninterruptibly(Future)}.
11621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
11631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws UncheckedExecutionException if {@code get} throws an {@code
11641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         ExecutionException} with an {@code Exception} as its cause
11651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws ExecutionError if {@code get} throws an {@code ExecutionException}
11661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         with an {@code Error} as its cause
11671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws CancellationException if {@code get} throws a {@code
11681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         CancellationException}
11691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 10.0
11701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
11711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Beta
11721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <V> V getUnchecked(Future<V> future) {
11731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(future);
11741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    try {
11751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return getUninterruptibly(future);
11761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    } catch (ExecutionException e) {
11771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      wrapAndThrowUnchecked(e.getCause());
11781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      throw new AssertionError();
11791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
11801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
11811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
11821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  private static void wrapAndThrowUnchecked(Throwable cause) {
11831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (cause instanceof Error) {
11841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      throw new ExecutionError((Error) cause);
11851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
11861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    /*
11871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * It's a non-Error, non-Exception Throwable. From my survey of such
11881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * classes, I believe that most users intended to extend Exception, so we'll
11891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * treat it like an Exception.
11901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     */
11911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    throw new UncheckedExecutionException(cause);
11921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
11931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
11941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /*
11951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * TODO(user): FutureChecker interface for these to be static methods on? If
11961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * so, refer to it in the (static-method) Futures.get documentation
11971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
11981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
11991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /*
12001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Arguably we don't need a timed getUnchecked because any operation slow
12011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * enough to require a timeout is heavyweight enough to throw a checked
12021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * exception and therefore be inappropriate to use with getUnchecked. Further,
12031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * it's not clear that converting the checked TimeoutException to a
12041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * RuntimeException -- especially to an UncheckedExecutionException, since it
12051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * wasn't thrown by the computation -- makes sense, and if we don't convert
12061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * it, the user still has to write a try-catch block.
12071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
12081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * If you think you would use this method, let us know.
12091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
12101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  private static <X extends Exception> X newWithCause(
12121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      Class<X> exceptionClass, Throwable cause) {
12131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    // getConstructors() guarantees this as long as we don't modify the array.
12141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @SuppressWarnings("unchecked")
12151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    List<Constructor<X>> constructors =
12161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        (List) Arrays.asList(exceptionClass.getConstructors());
12171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    for (Constructor<X> constructor : preferringStrings(constructors)) {
12181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Nullable X instance = newFromConstructor(constructor, cause);
12191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      if (instance != null) {
12201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        if (instance.getCause() == null) {
12211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          instance.initCause(cause);
12221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
12231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return instance;
12241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
12251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
12261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    throw new IllegalArgumentException(
12271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        "No appropriate constructor for exception of type " + exceptionClass
12281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert            + " in response to chained exception", cause);
12291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
12301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  private static <X extends Exception> List<Constructor<X>>
12321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      preferringStrings(List<Constructor<X>> constructors) {
12331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return WITH_STRING_PARAM_FIRST.sortedCopy(constructors);
12341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
12351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  private static final Ordering<Constructor<?>> WITH_STRING_PARAM_FIRST =
12371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      Ordering.natural().onResultOf(new Function<Constructor<?>, Boolean>() {
12381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        @Override public Boolean apply(Constructor<?> input) {
12391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          return asList(input.getParameterTypes()).contains(String.class);
12401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
12411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }).reverse();
12421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Nullable private static <X> X newFromConstructor(
12441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      Constructor<X> constructor, Throwable cause) {
12451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    Class<?>[] paramTypes = constructor.getParameterTypes();
12461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    Object[] params = new Object[paramTypes.length];
12471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    for (int i = 0; i < paramTypes.length; i++) {
12481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      Class<?> paramType = paramTypes[i];
12491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      if (paramType.equals(String.class)) {
12501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        params[i] = cause.toString();
12511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      } else if (paramType.equals(Throwable.class)) {
12521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        params[i] = cause;
12531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      } else {
12541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return null;
12551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
12561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
12571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    try {
12581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return constructor.newInstance(params);
12591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    } catch (IllegalArgumentException e) {
12601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return null;
12611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    } catch (InstantiationException e) {
12621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return null;
12631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    } catch (IllegalAccessException e) {
12641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return null;
12651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    } catch (InvocationTargetException e) {
12661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return null;
12671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
12681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
12691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
12711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Class that implements {@link #allAsList} and {@link #successfulAsList}.
12721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * The idea is to create a (null-filled) List and register a listener with
12731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * each component future to fill out the value in the List when that future
12741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * completes.
12751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
12761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  private static class ListFuture<V> extends AbstractFuture<List<V>> {
12771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    ImmutableList<? extends ListenableFuture<? extends V>> futures;
12781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    final boolean allMustSucceed;
12791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    final AtomicInteger remaining;
12801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    List<V> values;
12811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    /**
12831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * Constructor.
12841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     *
12851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * @param futures all the futures to build the list from
12861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * @param allMustSucceed whether a single failure or cancellation should
12871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     *        propagate to this future
12881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * @param listenerExecutor used to run listeners on all the passed in
12891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     *        futures.
12901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     */
12911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    ListFuture(
12921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        final ImmutableList<? extends ListenableFuture<? extends V>> futures,
12931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        final boolean allMustSucceed, final Executor listenerExecutor) {
12941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      this.futures = futures;
12951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      this.values = Lists.newArrayListWithCapacity(futures.size());
12961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      this.allMustSucceed = allMustSucceed;
12971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      this.remaining = new AtomicInteger(futures.size());
12981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      init(listenerExecutor);
13001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
13011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
13021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    private void init(final Executor listenerExecutor) {
13031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // First, schedule cleanup to execute when the Future is done.
13041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      addListener(new Runnable() {
13051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        @Override
13061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        public void run() {
13071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          // By now the values array has either been set as the Future's value,
13081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          // or (in case of failure) is no longer useful.
13091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          ListFuture.this.values = null;
13101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
13111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          // Let go of the memory held by other futures
13121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          ListFuture.this.futures = null;
13131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
13141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }, MoreExecutors.sameThreadExecutor());
13151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
13161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // Now begin the "real" initialization.
13171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
13181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // Corner case: List is empty.
13191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      if (futures.isEmpty()) {
13201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        set(Lists.newArrayList(values));
13211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return;
13221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
13231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
13241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // Populate the results list with null initially.
13251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      for (int i = 0; i < futures.size(); ++i) {
13261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        values.add(null);
13271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
13281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
13291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // Register a listener on each Future in the list to update
13301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // the state of this future.
13311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // Note that if all the futures on the list are done prior to completing
13321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // this loop, the last call to addListener() will callback to
13331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // setOneValue(), transitively call our cleanup listener, and set
13341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // this.futures to null.
13351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // We store a reference to futures to avoid the NPE.
13361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      ImmutableList<? extends ListenableFuture<? extends V>> localFutures = futures;
13371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      for (int i = 0; i < localFutures.size(); i++) {
13381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        final ListenableFuture<? extends V> listenable = localFutures.get(i);
13391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        final int index = i;
13401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        listenable.addListener(new Runnable() {
13411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          @Override
13421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          public void run() {
13431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert            setOneValue(index, listenable);
13441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          }
13451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }, listenerExecutor);
13461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
13471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
13481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
13491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    /**
13501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * Sets the value at the given index to that of the given future.
13511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     */
13521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    private void setOneValue(int index, Future<? extends V> future) {
13531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      List<V> localValues = values;
13541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      if (isDone() || localValues == null) {
13551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // Some other future failed or has been cancelled, causing this one to
13561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // also be cancelled or have an exception set. This should only happen
13571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // if allMustSucceed is true.
13581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        checkState(allMustSucceed,
13591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert            "Future was done before all dependencies completed");
13601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return;
13611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
13621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
13631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      try {
13641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        checkState(future.isDone(),
13651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert            "Tried to set value from future which is not done");
13661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        localValues.set(index, getUninterruptibly(future));
13671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      } catch (CancellationException e) {
13681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        if (allMustSucceed) {
13691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          // Set ourselves as cancelled. Let the input futures keep running
13701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          // as some of them may be used elsewhere.
13711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          // (Currently we don't override interruptTask, so
13721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          // mayInterruptIfRunning==false isn't technically necessary.)
13731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          cancel(false);
13741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
13751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      } catch (ExecutionException e) {
13761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        if (allMustSucceed) {
13771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          // As soon as the first one fails, throw the exception up.
13781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          // The result of all other inputs is then ignored.
13791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          setException(e.getCause());
13801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
13811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      } catch (RuntimeException e) {
13821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        if (allMustSucceed) {
13831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          setException(e);
13841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
13851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      } catch (Error e) {
13861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // Propagate errors up ASAP - our superclass will rethrow the error
13871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        setException(e);
13881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      } finally {
13891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        int newRemaining = remaining.decrementAndGet();
13901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        checkState(newRemaining >= 0, "Less than 0 remaining futures");
13911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        if (newRemaining == 0) {
13921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          localValues = values;
13931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          if (localValues != null) {
13941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert            set(Lists.newArrayList(localValues));
13951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          } else {
13961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert            checkState(isDone());
13971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          }
13981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
13991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
14001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
14011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
14021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
14031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public List<V> get() throws InterruptedException, ExecutionException {
14041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      callAllGets();
14051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
14061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // This may still block in spite of the calls above, as the listeners may
14071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // be scheduled for execution in other threads.
14081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return super.get();
14091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
14101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
14111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    /**
14121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * Calls the get method of all dependency futures to work around a bug in
14131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * some ListenableFutures where the listeners aren't called until get() is
14141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * called.
14151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     */
14161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    private void callAllGets() throws InterruptedException {
14171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      List<? extends ListenableFuture<? extends V>> oldFutures = futures;
14181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      if (oldFutures != null && !isDone()) {
14191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        for (ListenableFuture<? extends V> future : oldFutures) {
14201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          // We wait for a little while for the future, but if it's not done,
14211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          // we check that no other futures caused a cancellation or failure.
14221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          // This can introduce a delay of up to 10ms in reporting an exception.
14231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          while (!future.isDone()) {
14241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert            try {
14251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert              future.get();
14261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert            } catch (Error e) {
14271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert              throw e;
14281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert            } catch (InterruptedException e) {
14291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert              throw e;
14301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert            } catch (Throwable e) {
14311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert              // ExecutionException / CancellationException / RuntimeException
14321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert              if (allMustSucceed) {
14331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert                return;
14341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert              } else {
14351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert                continue;
14361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert              }
14371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert            }
14381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          }
14391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
14401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
14411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
14421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
14431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
14441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
14451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * A checked future that uses a function to map from exceptions to the
14461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * appropriate checked type.
14471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
14481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  private static class MappingCheckedFuture<V, X extends Exception> extends
14491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      AbstractCheckedFuture<V, X> {
14501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
14511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    final Function<Exception, X> mapper;
14521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
14531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    MappingCheckedFuture(ListenableFuture<V> delegate,
14541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        Function<Exception, X> mapper) {
14551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      super(delegate);
14561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
14571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      this.mapper = checkNotNull(mapper);
14581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
14591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
14601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
14611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    protected X mapException(Exception e) {
14621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return mapper.apply(e);
14631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
14641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
14651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert}
1466