001/*
002 * Copyright (C) 2009 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.util.concurrent;
016
017import com.google.common.annotations.GwtCompatible;
018import com.google.common.base.Preconditions;
019import com.google.errorprone.annotations.CanIgnoreReturnValue;
020import java.util.concurrent.Executor;
021
022/**
023 * A {@link ListenableFuture} which forwards all its method calls to another future. Subclasses
024 * should override one or more methods to modify the behavior of the backing future as desired per
025 * the <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
026 *
027 * <p>Most subclasses can just use {@link SimpleForwardingListenableFuture}.
028 *
029 * @author Shardul Deo
030 * @since 4.0
031 */
032@CanIgnoreReturnValue // TODO(cpovirk): Consider being more strict.
033@GwtCompatible
034public abstract class ForwardingListenableFuture<V> extends ForwardingFuture<V>
035    implements ListenableFuture<V> {
036
037  /** Constructor for use by subclasses. */
038  protected ForwardingListenableFuture() {}
039
040  @Override
041  protected abstract ListenableFuture<? extends V> delegate();
042
043  @Override
044  public void addListener(Runnable listener, Executor exec) {
045    delegate().addListener(listener, exec);
046  }
047
048  // TODO(cpovirk): Use standard Javadoc form for SimpleForwarding* class and constructor
049  /**
050   * A simplified version of {@link ForwardingListenableFuture} where subclasses can pass in an
051   * already constructed {@link ListenableFuture} as the delegate.
052   *
053   * @since 9.0
054   */
055  public abstract static class SimpleForwardingListenableFuture<V>
056      extends ForwardingListenableFuture<V> {
057    private final ListenableFuture<V> delegate;
058
059    protected SimpleForwardingListenableFuture(ListenableFuture<V> delegate) {
060      this.delegate = Preconditions.checkNotNull(delegate);
061    }
062
063    @Override
064    protected final ListenableFuture<V> delegate() {
065      return delegate;
066    }
067  }
068}