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