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