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