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.common.collect.ForwardingObject;
020import com.google.errorprone.annotations.CanIgnoreReturnValue;
021import java.util.concurrent.ExecutionException;
022import java.util.concurrent.Future;
023import java.util.concurrent.TimeUnit;
024import java.util.concurrent.TimeoutException;
025import org.jspecify.annotations.Nullable;
026
027/**
028 * A {@link Future} which forwards all its method calls to another future. Subclasses should
029 * override one or more methods to modify the behavior of the backing future as desired per the <a
030 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
031 *
032 * <p>Most subclasses can just use {@link SimpleForwardingFuture}.
033 *
034 * @author Sven Mawson
035 * @since 1.0
036 */
037@GwtCompatible
038public abstract class ForwardingFuture<V extends @Nullable Object> extends ForwardingObject
039    implements Future<V> {
040  /** Constructor for use by subclasses. */
041  protected ForwardingFuture() {}
042
043  @Override
044  protected abstract Future<? extends V> delegate();
045
046  @Override
047  @CanIgnoreReturnValue
048  public boolean cancel(boolean mayInterruptIfRunning) {
049    return delegate().cancel(mayInterruptIfRunning);
050  }
051
052  @Override
053  public boolean isCancelled() {
054    return delegate().isCancelled();
055  }
056
057  @Override
058  public boolean isDone() {
059    return delegate().isDone();
060  }
061
062  @Override
063  @CanIgnoreReturnValue
064  @ParametricNullness
065  public V get() throws InterruptedException, ExecutionException {
066    return delegate().get();
067  }
068
069  @Override
070  @CanIgnoreReturnValue
071  @ParametricNullness
072  public V get(long timeout, TimeUnit unit)
073      throws InterruptedException, ExecutionException, TimeoutException {
074    return delegate().get(timeout, unit);
075  }
076
077  // TODO(cpovirk): Use standard Javadoc form for SimpleForwarding* class and constructor
078  /**
079   * A simplified version of {@link ForwardingFuture} where subclasses can pass in an already
080   * constructed {@link Future} as the delegate.
081   *
082   * @since 9.0
083   */
084  public abstract static class SimpleForwardingFuture<V extends @Nullable Object>
085      extends ForwardingFuture<V> {
086    private final Future<V> delegate;
087
088    protected SimpleForwardingFuture(Future<V> delegate) {
089      this.delegate = Preconditions.checkNotNull(delegate);
090    }
091
092    @Override
093    protected final Future<V> delegate() {
094      return delegate;
095    }
096  }
097}