View Javadoc
1   /*
2    * Copyright (C) 2009 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package com.google.common.util.concurrent;
16  
17  import static com.google.common.base.Preconditions.checkNotNull;
18  
19  import com.google.common.annotations.GwtCompatible;
20  import java.util.concurrent.ExecutionException;
21  import java.util.concurrent.Executor;
22  import java.util.concurrent.TimeUnit;
23  import java.util.concurrent.TimeoutException;
24  
25  /**
26   * {@link FluentFuture} that forwards all calls to a delegate.
27   *
28   * <h3>Extension</h3>
29   *
30   * If you want a class like {@code FluentFuture} but with extra methods, we recommend declaring your
31   * own subclass of {@link ListenableFuture}, complete with a method like {@link #from} to adapt an
32   * existing {@code ListenableFuture}, implemented atop a {@link ForwardingListenableFuture} that
33   * forwards to that future and adds the desired methods.
34   */
35  @GwtCompatible
36  final class ForwardingFluentFuture<V> extends FluentFuture<V> {
37    private final ListenableFuture<V> delegate;
38  
39    ForwardingFluentFuture(ListenableFuture<V> delegate) {
40      this.delegate = checkNotNull(delegate);
41    }
42  
43    @Override
44    public void addListener(Runnable listener, Executor executor) {
45      delegate.addListener(listener, executor);
46    }
47  
48    @Override
49    public boolean cancel(boolean mayInterruptIfRunning) {
50      return delegate.cancel(mayInterruptIfRunning);
51    }
52  
53    @Override
54    public boolean isCancelled() {
55      return delegate.isCancelled();
56    }
57  
58    @Override
59    public boolean isDone() {
60      return delegate.isDone();
61    }
62  
63    @Override
64    public V get() throws InterruptedException, ExecutionException {
65      return delegate.get();
66    }
67  
68    @Override
69    public V get(long timeout, TimeUnit unit)
70        throws InterruptedException, ExecutionException, TimeoutException {
71      return delegate.get(timeout, unit);
72    }
73  }