CheckedFuture.java revision bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dc
1bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor/* 2bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * Copyright (C) 2008 Google Inc. 3bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 4bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * Licensed under the Apache License, Version 2.0 (the "License"); 5bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * you may not use this file except in compliance with the License. 6bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * You may obtain a copy of the License at 7bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 8bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * http://www.apache.org/licenses/LICENSE-2.0 9bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 10bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * Unless required by applicable law or agreed to in writing, software 11bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * distributed under the License is distributed on an "AS IS" BASIS, 12bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * See the License for the specific language governing permissions and 14bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * limitations under the License. 15bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor */ 16bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 17bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnorpackage com.google.common.util.concurrent; 18bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 19bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnorimport java.util.concurrent.CancellationException; 20bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnorimport java.util.concurrent.ExecutionException; 21bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnorimport java.util.concurrent.Future; 22bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnorimport java.util.concurrent.TimeUnit; 23bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnorimport java.util.concurrent.TimeoutException; 24bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 25bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor/** 26bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * A {@code CheckedFuture} is an extension of {@link Future} that includes 27bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * versions of the {@code get} methods that can throw a checked exception and 28bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * allows listeners to be attached to the future. This makes it easier to 29bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * create a future that executes logic which can throw an exception. 30bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 31bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * <p>Implementations of this interface must adapt the exceptions thrown by 32bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * {@code Future#get()}: {@link CancellationException}, 33bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * {@link ExecutionException} and {@link InterruptedException} into the type 34bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * specified by the {@code E} type parameter. 35bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 36bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * <p>This interface also extends the ListenableFuture interface to allow 37bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * listeners to be added. This allows the future to be used as a normal 38bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * {@link Future} or as an asynchronous callback mechanism as needed. This 39bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * allows multiple callbacks to be registered for a particular task, and the 40bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * future will guarantee execution of all listeners when the task completes. 41bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 42bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @author Sven Mawson 43bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @since 2009.09.15 <b>tentative</b> 44bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor */ 45bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnorpublic interface CheckedFuture<V, E extends Exception> 46bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor extends ListenableFuture<V> { 47bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 48bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor /** 49bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * Exception checking version of {@link Future#get()} that will translate 50bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * {@link InterruptedException}, {@link CancellationException} and 51bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * {@link ExecutionException} into application-specific exceptions. 52bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 53bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @return the result of executing the future. 54bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @throws E on interruption, cancellation or execution exceptions. 55bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor */ 56bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor public V checkedGet() throws E; 57bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 58bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor /** 59bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * Exception checking version of {@link Future#get(long, TimeUnit)} that will 60bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * translate {@link InterruptedException}, {@link CancellationException} and 61bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * {@link ExecutionException} into application-specific exceptions. On 62bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * timeout this method throws a normal {@link TimeoutException}. 63bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 64bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @return the result of executing the future. 65bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @throws TimeoutException if retrieving the result timed out. 66bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @throws E on interruption, cancellation or execution exceptions. 67bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor */ 68bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor public V checkedGet(long timeout, TimeUnit unit) 69bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor throws TimeoutException, E; 70bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor} 71