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.checkerframework.checker.nullness.qual.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@CanIgnoreReturnValue // TODO(cpovirk): Consider being more strict.
038@GwtCompatible
039@ElementTypesAreNonnullByDefault
040public abstract class ForwardingFuture<V extends @Nullable Object> extends ForwardingObject
041    implements Future<V> {
042  /** Constructor for use by subclasses. */
043  protected ForwardingFuture() {}
044
045  @Override
046  protected abstract Future<? extends V> delegate();
047
048  @Override
049  public boolean cancel(boolean mayInterruptIfRunning) {
050    return delegate().cancel(mayInterruptIfRunning);
051  }
052
053  @Override
054  public boolean isCancelled() {
055    return delegate().isCancelled();
056  }
057
058  @Override
059  public boolean isDone() {
060    return delegate().isDone();
061  }
062
063  @Override
064  @ParametricNullness
065  public V get() throws InterruptedException, ExecutionException {
066    return delegate().get();
067  }
068
069  @Override
070  @ParametricNullness
071  public V get(long timeout, TimeUnit unit)
072      throws InterruptedException, ExecutionException, TimeoutException {
073    return delegate().get(timeout, unit);
074  }
075
076  // TODO(cpovirk): Use standard Javadoc form for SimpleForwarding* class and constructor
077  /**
078   * A simplified version of {@link ForwardingFuture} where subclasses can pass in an already
079   * constructed {@link Future} as the delegate.
080   *
081   * @since 9.0
082   */
083  public abstract static class SimpleForwardingFuture<V extends @Nullable Object>
084      extends ForwardingFuture<V> {
085    private final Future<V> delegate;
086
087    protected SimpleForwardingFuture(Future<V> delegate) {
088      this.delegate = Preconditions.checkNotNull(delegate);
089    }
090
091    @Override
092    protected final Future<V> delegate() {
093      return delegate;
094    }
095  }
096}