001/*
002 * Copyright (C) 2009 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
017package com.google.common.util.concurrent;
018
019import com.google.common.base.Preconditions;
020
021import java.util.concurrent.Executor;
022
023/**
024 * A {@link ListenableFuture} which forwards all its method calls to another
025 * future. Subclasses should override one or more methods to modify the behavior
026 * of the backing future as desired per the <a
027 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
028 *
029 * <p>Most subclasses can just use {@link SimpleForwardingListenableFuture}.
030 *
031 * @param <V> The result type returned by this Future's {@code get} method
032 * 
033 * @author Shardul Deo
034 * @since 4.0
035 */
036public abstract class ForwardingListenableFuture<V> extends ForwardingFuture<V>
037    implements ListenableFuture<V> {
038
039  /** Constructor for use by subclasses. */
040  protected ForwardingListenableFuture() {}
041
042  @Override
043  protected abstract ListenableFuture<V> delegate();
044
045  @Override
046  public void addListener(Runnable listener, Executor exec) {
047    delegate().addListener(listener, exec);
048  }
049
050  /*
051   * TODO(cpovirk): Use standard Javadoc form for SimpleForwarding* class and
052   * constructor
053   */
054  /**
055   * A simplified version of {@link ForwardingListenableFuture} where subclasses
056   * can pass in an already constructed {@link ListenableFuture} 
057   * as the delegate.
058   * 
059   * @since 9.0
060   */
061  public abstract static class SimpleForwardingListenableFuture<V>
062      extends ForwardingListenableFuture<V> {
063    private final ListenableFuture<V> delegate;
064
065    protected SimpleForwardingListenableFuture(ListenableFuture<V> delegate) {
066      this.delegate = Preconditions.checkNotNull(delegate);
067    }
068
069    @Override
070    protected final ListenableFuture<V> delegate() {
071      return delegate;
072    }
073  }
074}