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.TimeUnit;
024 import java.util.concurrent.TimeoutException;
025
026 /**
027 * A delegating wrapper around a {@link ListenableFuture} that adds support for
028 * the {@link #checkedGet()} and {@link #checkedGet(long, TimeUnit)} methods.
029 *
030 * @author Sven Mawson
031 * @since 1.0
032 */
033 @Beta
034 public abstract class AbstractCheckedFuture<V, X extends Exception>
035 extends ForwardingListenableFuture.SimpleForwardingListenableFuture<V>
036 implements CheckedFuture<V, X> {
037 /**
038 * Constructs an {@code AbstractCheckedFuture} that wraps a delegate.
039 */
040 protected AbstractCheckedFuture(ListenableFuture<V> delegate) {
041 super(delegate);
042 }
043
044 /**
045 * Translates from an {@link InterruptedException},
046 * {@link CancellationException} or {@link ExecutionException} thrown by
047 * {@code get} to an exception of type {@code X} to be thrown by
048 * {@code checkedGet}. Subclasses must implement this method.
049 *
050 * <p>If {@code e} is an {@code InterruptedException}, the calling
051 * {@code checkedGet} method has already restored the interrupt after catching
052 * the exception. If an implementation of {@link #mapException(Exception)}
053 * wishes to swallow the interrupt, it can do so by calling
054 * {@link Thread#interrupted()}.
055 *
056 * <p>Subclasses may choose to throw, rather than return, a subclass of
057 * {@code RuntimeException} to allow creating a CheckedFuture that throws
058 * both checked and unchecked exceptions.
059 */
060 protected abstract X mapException(Exception e);
061
062 /**
063 * {@inheritDoc}
064 *
065 * <p>This implementation calls {@link #get()} and maps that method's standard
066 * exceptions to instances of type {@code X} using {@link #mapException}.
067 *
068 * <p>In addition, if {@code get} throws an {@link InterruptedException}, this
069 * implementation will set the current thread's interrupt status before
070 * calling {@code mapException}.
071 *
072 * @throws X if {@link #get()} throws an {@link InterruptedException},
073 * {@link CancellationException}, or {@link ExecutionException}
074 */
075 public V checkedGet() throws X {
076 try {
077 return get();
078 } catch (InterruptedException e) {
079 Thread.currentThread().interrupt();
080 throw mapException(e);
081 } catch (CancellationException e) {
082 throw mapException(e);
083 } catch (ExecutionException e) {
084 throw mapException(e);
085 }
086 }
087
088 /**
089 * {@inheritDoc}
090 *
091 * <p>This implementation calls {@link #get(long, TimeUnit)} and maps that
092 * method's standard exceptions (excluding {@link TimeoutException}, which is
093 * propagated) to instances of type {@code X} using {@link #mapException}.
094 *
095 * <p>In addition, if {@code get} throws an {@link InterruptedException}, this
096 * implementation will set the current thread's interrupt status before
097 * calling {@code mapException}.
098 *
099 * @throws X if {@link #get()} throws an {@link InterruptedException},
100 * {@link CancellationException}, or {@link ExecutionException}
101 * @throws TimeoutException {@inheritDoc}
102 */
103 public V checkedGet(long timeout, TimeUnit unit) throws TimeoutException, X {
104 try {
105 return get(timeout, unit);
106 } catch (InterruptedException e) {
107 Thread.currentThread().interrupt();
108 throw mapException(e);
109 } catch (CancellationException e) {
110 throw mapException(e);
111 } catch (ExecutionException e) {
112 throw mapException(e);
113 }
114 }
115 }