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;
021import org.checkerframework.checker.nullness.qual.Nullable;
022
023/**
024 * A {@link ListenableFuture} which forwards all its method calls to another future. Subclasses
025 * should override one or more methods to modify the behavior of the backing future as desired per
026 * the <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
027 *
028 * <p>Most subclasses can just use {@link SimpleForwardingListenableFuture}.
029 *
030 * @author Shardul Deo
031 * @since 4.0
032 */
033@CanIgnoreReturnValue // TODO(cpovirk): Consider being more strict.
034@GwtCompatible
035@ElementTypesAreNonnullByDefault
036public abstract class ForwardingListenableFuture<V extends @Nullable Object>
037    extends ForwardingFuture<V> implements ListenableFuture<V> {
038
039  /** Constructor for use by subclasses. */
040  protected ForwardingListenableFuture() {}
041
042  @Override
043  protected abstract ListenableFuture<? extends V> delegate();
044
045  @Override
046  public void addListener(Runnable listener, Executor exec) {
047    delegate().addListener(listener, exec);
048  }
049
050  // TODO(cpovirk): Use standard Javadoc form for SimpleForwarding* class and constructor
051  /**
052   * A simplified version of {@link ForwardingListenableFuture} where subclasses can pass in an
053   * already constructed {@link ListenableFuture} as the delegate.
054   *
055   * @since 9.0
056   */
057  public abstract static class SimpleForwardingListenableFuture<V extends @Nullable Object>
058      extends ForwardingListenableFuture<V> {
059    private final ListenableFuture<V> delegate;
060
061    protected SimpleForwardingListenableFuture(ListenableFuture<V> delegate) {
062      this.delegate = Preconditions.checkNotNull(delegate);
063    }
064
065    @Override
066    protected final ListenableFuture<V> delegate() {
067      return delegate;
068    }
069  }
070}