001 /* 002 * Copyright (C) 2008 Google Inc. 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 017 package com.google.common.util.concurrent; 018 019 import com.google.common.annotations.Beta; 020 021 import java.util.concurrent.CancellationException; 022 import java.util.concurrent.ExecutionException; 023 import java.util.concurrent.Future; 024 import java.util.concurrent.TimeUnit; 025 import java.util.concurrent.TimeoutException; 026 027 /** 028 * A {@code CheckedFuture} is an extension of {@link Future} that includes 029 * versions of the {@code get} methods that can throw a checked exception and 030 * allows listeners to be attached to the future. This makes it easier to 031 * create a future that executes logic which can throw an exception. 032 * 033 * <p>Implementations of this interface must adapt the exceptions thrown by 034 * {@code Future#get()}: {@link CancellationException}, 035 * {@link ExecutionException} and {@link InterruptedException} into the type 036 * specified by the {@code E} type parameter. 037 * 038 * <p>This interface also extends the ListenableFuture interface to allow 039 * listeners to be added. This allows the future to be used as a normal 040 * {@link Future} or as an asynchronous callback mechanism as needed. This 041 * allows multiple callbacks to be registered for a particular task, and the 042 * future will guarantee execution of all listeners when the task completes. 043 * 044 * @author Sven Mawson 045 * @since 1 046 */ 047 @Beta 048 public interface CheckedFuture<V, E extends Exception> 049 extends ListenableFuture<V> { 050 051 /** 052 * Exception checking version of {@link Future#get()} that will translate 053 * {@link InterruptedException}, {@link CancellationException} and 054 * {@link ExecutionException} into application-specific exceptions. 055 * 056 * @return the result of executing the future. 057 * @throws E on interruption, cancellation or execution exceptions. 058 */ 059 V checkedGet() throws E; 060 061 /** 062 * Exception checking version of {@link Future#get(long, TimeUnit)} that will 063 * translate {@link InterruptedException}, {@link CancellationException} and 064 * {@link ExecutionException} into application-specific exceptions. On 065 * timeout this method throws a normal {@link TimeoutException}. 066 * 067 * @return the result of executing the future. 068 * @throws TimeoutException if retrieving the result timed out. 069 * @throws E on interruption, cancellation or execution exceptions. 070 */ 071 V checkedGet(long timeout, TimeUnit unit) throws TimeoutException, E; 072 }