1/*
2 * Copyright (C) 2009 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
17package com.google.common.util.concurrent;
18
19import com.google.common.base.Preconditions;
20
21import java.util.concurrent.Executor;
22
23/**
24 * A {@link ListenableFuture} which forwards all its method calls to another
25 * future. Subclasses should override one or more methods to modify the behavior
26 * of the backing future as desired per the <a
27 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
28 *
29 * <p>Most subclasses can just use {@link SimpleForwardingListenableFuture}.
30 *
31 * @param <V> The result type returned by this Future's {@code get} method
32 *
33 * @author Shardul Deo
34 * @since 4.0
35 */
36public abstract class ForwardingListenableFuture<V> extends ForwardingFuture<V>
37    implements ListenableFuture<V> {
38
39  /** Constructor for use by subclasses. */
40  protected ForwardingListenableFuture() {}
41
42  @Override
43  protected abstract ListenableFuture<V> delegate();
44
45  @Override
46  public void addListener(Runnable listener, Executor exec) {
47    delegate().addListener(listener, exec);
48  }
49
50  /*
51   * TODO(cpovirk): Use standard Javadoc form for SimpleForwarding* class and
52   * constructor
53   */
54  /**
55   * A simplified version of {@link ForwardingListenableFuture} where subclasses
56   * can pass in an already constructed {@link ListenableFuture}
57   * as the delegate.
58   *
59   * @since 9.0
60   */
61  public abstract static class SimpleForwardingListenableFuture<V>
62      extends ForwardingListenableFuture<V> {
63    private final ListenableFuture<V> delegate;
64
65    protected SimpleForwardingListenableFuture(ListenableFuture<V> delegate) {
66      this.delegate = Preconditions.checkNotNull(delegate);
67    }
68
69    @Override
70    protected final ListenableFuture<V> delegate() {
71      return delegate;
72    }
73  }
74}
75