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