001/*
002 * Copyright (C) 2011 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.Beta;
018import com.google.common.annotations.GwtIncompatible;
019import com.google.common.base.Preconditions;
020import com.google.errorprone.annotations.CanIgnoreReturnValue;
021import java.util.concurrent.TimeUnit;
022import java.util.concurrent.TimeoutException;
023
024/**
025 * A future which forwards all its method calls to another future. Subclasses should override one or
026 * more methods to modify the behavior of the backing future as desired per the <a href=
027 * "http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
028 *
029 * <p>Most subclasses can simply extend {@link SimpleForwardingCheckedFuture}.
030 *
031 * @param <V> The result type returned by this Future's {@code get} method
032 * @param <X> The type of the Exception thrown by the Future's {@code checkedGet} method
033 *
034 * @author Anthony Zana
035 * @since 9.0
036 */
037@Beta
038@GwtIncompatible
039public abstract class ForwardingCheckedFuture<V, X extends Exception>
040    extends ForwardingListenableFuture<V> implements CheckedFuture<V, X> {
041
042  @CanIgnoreReturnValue
043  @Override
044  public V checkedGet() throws X {
045    return delegate().checkedGet();
046  }
047
048  @CanIgnoreReturnValue
049  @Override
050  public V checkedGet(long timeout, TimeUnit unit) throws TimeoutException, X {
051    return delegate().checkedGet(timeout, unit);
052  }
053
054  @Override
055  protected abstract CheckedFuture<V, X> delegate();
056
057  // TODO(cpovirk): Use Standard Javadoc form for SimpleForwarding*
058  /**
059   * A simplified version of {@link ForwardingCheckedFuture} where subclasses can pass in an already
060   * constructed {@link CheckedFuture} as the delegate.
061   *
062   * @since 9.0
063   */
064  @Beta
065  public abstract static class SimpleForwardingCheckedFuture<V, X extends Exception>
066      extends ForwardingCheckedFuture<V, X> {
067    private final CheckedFuture<V, X> delegate;
068
069    protected SimpleForwardingCheckedFuture(CheckedFuture<V, X> delegate) {
070      this.delegate = Preconditions.checkNotNull(delegate);
071    }
072
073    @Override
074    protected final CheckedFuture<V, X> delegate() {
075      return delegate;
076    }
077  }
078}