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 * @author Anthony Zana
034 * @since 9.0
035 * @deprecated {@link CheckedFuture} cannot properly support the chained operations that are the
036 *     primary goal of {@link ListenableFuture}. {@code CheckedFuture} also encourages users to
037 *     rethrow exceptions from one thread in another thread, producing misleading stack traces.
038 *     Additionally, it has a surprising policy about which exceptions to map and which to leave
039 *     untouched. Guava users who want a {@code CheckedFuture} can fork the classes for their own
040 *     use, possibly specializing them to the particular exception type they use. We recommend that
041 *     most people use {@code ListenableFuture} and perform any exception wrapping themselves. This
042 *     class is scheduled for removal from Guava in February 2018.
043 */
044@Beta
045@Deprecated
046@GwtIncompatible
047public abstract class ForwardingCheckedFuture<V, X extends Exception>
048    extends ForwardingListenableFuture<V> implements CheckedFuture<V, X> {
049
050  @CanIgnoreReturnValue
051  @Override
052  public V checkedGet() throws X {
053    return delegate().checkedGet();
054  }
055
056  @CanIgnoreReturnValue
057  @Override
058  public V checkedGet(long timeout, TimeUnit unit) throws TimeoutException, X {
059    return delegate().checkedGet(timeout, unit);
060  }
061
062  @Override
063  protected abstract CheckedFuture<V, X> delegate();
064
065  // TODO(cpovirk): Use Standard Javadoc form for SimpleForwarding*
066  /**
067   * A simplified version of {@link ForwardingCheckedFuture} where subclasses can pass in an already
068   * constructed {@link CheckedFuture} as the delegate.
069   *
070   * @since 9.0
071   * @deprecated {@link CheckedFuture} cannot properly support the chained operations that are the
072   *     primary goal of {@link ListenableFuture}. {@code CheckedFuture} also encourages users to
073   *     rethrow exceptions from one thread in another thread, producing misleading stack traces.
074   *     Additionally, it has a surprising policy about which exceptions to map and which to leave
075   *     untouched. Guava users who want a {@code CheckedFuture} can fork the classes for their own
076   *     use, possibly specializing them to the particular exception type they use. We recommend
077   *     that most people use {@code ListenableFuture} and perform any exception wrapping
078   *     themselves. This class is scheduled for removal from Guava in February 2018.
079   */
080  @Beta
081  @Deprecated
082  public abstract static class SimpleForwardingCheckedFuture<V, X extends Exception>
083      extends ForwardingCheckedFuture<V, X> {
084    private final CheckedFuture<V, X> delegate;
085
086    protected SimpleForwardingCheckedFuture(CheckedFuture<V, X> delegate) {
087      this.delegate = Preconditions.checkNotNull(delegate);
088    }
089
090    @Override
091    protected final CheckedFuture<V, X> delegate() {
092      return delegate;
093    }
094  }
095}