1 /*
2  * Copyright (C) 2011 The Guava Authors
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 com.google.common.annotations.Beta;
20 import com.google.common.base.Preconditions;
21 
22 import java.util.concurrent.TimeUnit;
23 import java.util.concurrent.TimeoutException;
24 
25 /**
26  * A future which forwards all its method calls to another future. Subclasses
27  * should override one or more methods to modify the behavior of the backing
28  * future as desired per the <a href=
29  * "http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
30  *
31  * <p>Most subclasses can simply extend {@link SimpleForwardingCheckedFuture}.
32  *
33  * @param <V> The result type returned by this Future's {@code get} method
34  * @param <X> The type of the Exception thrown by the Future's
35  *            {@code checkedGet} method
36  *
37  * @author Anthony Zana
38  * @since 9.0
39  */
40 @Beta
41 public abstract class ForwardingCheckedFuture<V, X extends Exception>
42     extends ForwardingListenableFuture<V> implements CheckedFuture<V, X> {
43 
44   @Override
checkedGet()45   public V checkedGet() throws X {
46     return delegate().checkedGet();
47   }
48 
49   @Override
checkedGet(long timeout, TimeUnit unit)50   public V checkedGet(long timeout, TimeUnit unit) throws TimeoutException, X {
51     return delegate().checkedGet(timeout, unit);
52   }
53 
54   @Override
delegate()55   protected abstract CheckedFuture<V, X> delegate();
56 
57   // TODO(cpovirk): Use Standard Javadoc form for SimpleForwarding*
58   /**
59    * A simplified version of {@link ForwardingCheckedFuture} where subclasses
60    * can pass in an already constructed {@link CheckedFuture} as the delegate.
61    *
62    * @since 9.0
63    */
64   @Beta
65   public abstract static class SimpleForwardingCheckedFuture<
66       V, X extends Exception> extends ForwardingCheckedFuture<V, X> {
67     private final CheckedFuture<V, X> delegate;
68 
SimpleForwardingCheckedFuture(CheckedFuture<V, X> delegate)69     protected SimpleForwardingCheckedFuture(CheckedFuture<V, X> delegate) {
70       this.delegate = Preconditions.checkNotNull(delegate);
71     }
72 
73     @Override
delegate()74     protected final CheckedFuture<V, X> delegate() {
75       return delegate;
76     }
77   }
78 }
79