001/*
002 * Copyright (C) 2008 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.util.concurrent;
016
017import com.google.common.annotations.Beta;
018import com.google.common.annotations.GwtCompatible;
019import com.google.errorprone.annotations.CanIgnoreReturnValue;
020import java.util.concurrent.CancellationException;
021import java.util.concurrent.ExecutionException;
022import java.util.concurrent.Future;
023import java.util.concurrent.TimeUnit;
024import java.util.concurrent.TimeoutException;
025
026/**
027 * A {@code CheckedFuture} is a {@link ListenableFuture} that includes versions of the {@code get}
028 * methods that can throw a checked exception. This makes it easier to create a future that executes
029 * logic which can throw an exception.
030 *
031 * <p><b>Warning:</b> We recommend against using {@code CheckedFuture} in new projects. {@code
032 * CheckedFuture} is difficult to build libraries atop. {@code CheckedFuture} ports of methods like
033 * {@link Futures#transformAsync} have historically had bugs, and some of these bugs are necessary,
034 * unavoidable consequences of the {@code CheckedFuture} API. Additionally, {@code CheckedFuture}
035 * encourages users to take exceptions from one thread and rethrow them in another, producing
036 * confusing stack traces.
037 *
038 * <p>A common implementation is {@link Futures#immediateCheckedFuture}.
039 *
040 * <p>Implementations of this interface must adapt the exceptions thrown by {@code Future#get()}:
041 * {@link CancellationException}, {@link ExecutionException} and {@link InterruptedException} into
042 * the type specified by the {@code X} type parameter.
043 *
044 * <p>This interface also extends the ListenableFuture interface to allow listeners to be added.
045 * This allows the future to be used as a normal {@link Future} or as an asynchronous callback
046 * mechanism as needed. This allows multiple callbacks to be registered for a particular task, and
047 * the future will guarantee execution of all listeners when the task completes.
048 *
049 * <p>For a simpler alternative to CheckedFuture, consider accessing Future values with {@link
050 * Futures#getChecked(Future, Class) Futures.getChecked()}.
051 *
052 * @author Sven Mawson
053 * @since 1.0
054 * @deprecated {@link CheckedFuture} cannot properly support the chained operations that are the
055 *     primary goal of {@link ListenableFuture}. {@code CheckedFuture} also encourages users to
056 *     rethrow exceptions from one thread in another thread, producing misleading stack traces.
057 *     Additionally, it has a surprising policy about which exceptions to map and which to leave
058 *     untouched. Guava users who want a {@code CheckedFuture} can fork the classes for their own
059 *     use, possibly specializing them to the particular exception type they use. We recommend that
060 *     most people use {@code ListenableFuture} and perform any exception wrapping themselves. This
061 *     class is scheduled for removal from Guava in January 2019.
062 */
063// TODO(b/72241575): Remove by 2019-01
064@Beta
065@CanIgnoreReturnValue
066@Deprecated
067@GwtCompatible
068public interface CheckedFuture<V, X extends Exception> extends ListenableFuture<V> {
069
070  /**
071   * Exception checking version of {@link Future#get()} that will translate {@link
072   * InterruptedException}, {@link CancellationException} and {@link ExecutionException} into
073   * application-specific exceptions.
074   *
075   * @return the result of executing the future.
076   * @throws X on interruption, cancellation or execution exceptions.
077   */
078  V checkedGet() throws X;
079
080  /**
081   * Exception checking version of {@link Future#get(long, TimeUnit)} that will translate {@link
082   * InterruptedException}, {@link CancellationException} and {@link ExecutionException} into
083   * application-specific exceptions. On timeout this method throws a normal {@link
084   * TimeoutException}.
085   *
086   * @return the result of executing the future.
087   * @throws TimeoutException if retrieving the result timed out.
088   * @throws X on interruption, cancellation or execution exceptions.
089   */
090  V checkedGet(long timeout, TimeUnit unit) throws TimeoutException, X;
091}