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