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 */
055@Beta
056@CanIgnoreReturnValue
057@GwtCompatible
058public interface CheckedFuture<V, X extends Exception> extends ListenableFuture<V> {
059
060  /**
061   * Exception checking version of {@link Future#get()} that will translate {@link
062   * InterruptedException}, {@link CancellationException} and {@link ExecutionException} into
063   * application-specific exceptions.
064   *
065   * @return the result of executing the future.
066   * @throws X on interruption, cancellation or execution exceptions.
067   */
068  V checkedGet() throws X;
069
070  /**
071   * Exception checking version of {@link Future#get(long, TimeUnit)} that will translate {@link
072   * InterruptedException}, {@link CancellationException} and {@link ExecutionException} into
073   * application-specific exceptions.  On timeout this method throws a normal {@link
074   * TimeoutException}.
075   *
076   * @return the result of executing the future.
077   * @throws TimeoutException if retrieving the result timed out.
078   * @throws X on interruption, cancellation or execution exceptions.
079   */
080  V checkedGet(long timeout, TimeUnit unit) throws TimeoutException, X;
081}