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