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