001/*
002 * Copyright (C) 2008 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.errorprone.annotations.CanIgnoreReturnValue;
020import java.util.concurrent.CancellationException;
021import java.util.concurrent.ExecutionException;
022import java.util.concurrent.TimeUnit;
023import java.util.concurrent.TimeoutException;
024
025/**
026 * A delegating wrapper around a {@link ListenableFuture} that adds support for the {@link
027 * #checkedGet()} and {@link #checkedGet(long, TimeUnit)} methods.
028 *
029 * @author Sven Mawson
030 * @since 1.0
031 * @deprecated {@link CheckedFuture} cannot properly support the chained operations that are the
032 *     primary goal of {@link ListenableFuture}. {@code CheckedFuture} also encourages users to
033 *     rethrow exceptions from one thread in another thread, producing misleading stack traces.
034 *     Additionally, it has a surprising policy about which exceptions to map and which to leave
035 *     untouched. Guava users who want a {@code CheckedFuture} can fork the classes for their own
036 *     use, possibly specializing them to the particular exception type they use. We recommend that
037 *     most people use {@code ListenableFuture} and perform any exception wrapping themselves. This
038 *     class is scheduled for removal from Guava in January 2019.
039 */
040// TODO(b/72241575): Remove by 2019-01
041@Beta
042@Deprecated
043@GwtIncompatible
044public abstract class AbstractCheckedFuture<V, X extends Exception>
045    extends ForwardingListenableFuture.SimpleForwardingListenableFuture<V>
046    implements CheckedFuture<V, X> {
047  /** Constructs an {@code AbstractCheckedFuture} that wraps a delegate. */
048  protected AbstractCheckedFuture(ListenableFuture<V> delegate) {
049    super(delegate);
050  }
051
052  /**
053   * Translates from an {@link InterruptedException}, {@link CancellationException} or {@link
054   * ExecutionException} thrown by {@code get} to an exception of type {@code X} to be thrown by
055   * {@code checkedGet}. Subclasses must implement this method.
056   *
057   * <p>If {@code e} is an {@code InterruptedException}, the calling {@code checkedGet} method has
058   * already restored the interrupt after catching the exception. If an implementation of {@link
059   * #mapException(Exception)} wishes to swallow the interrupt, it can do so by calling {@link
060   * Thread#interrupted()}.
061   *
062   * <p>Subclasses may choose to throw, rather than return, a subclass of {@code RuntimeException}
063   * to allow creating a CheckedFuture that throws both checked and unchecked exceptions.
064   */
065  // We might like @ForOverride here, but some subclasses invoke this from their get() methods.
066  protected abstract X mapException(Exception e);
067
068  /**
069   * {@inheritDoc}
070   *
071   * <p>This implementation calls {@link #get()} and maps that method's standard exceptions to
072   * instances of type {@code X} using {@link #mapException}.
073   *
074   * <p>In addition, if {@code get} throws an {@link InterruptedException}, this implementation will
075   * set the current thread's interrupt status before calling {@code mapException}.
076   *
077   * @throws X if {@link #get()} throws an {@link InterruptedException}, {@link
078   *     CancellationException}, or {@link ExecutionException}
079   */
080  @CanIgnoreReturnValue
081  @Override
082  public V checkedGet() throws X {
083    try {
084      return get();
085    } catch (InterruptedException e) {
086      Thread.currentThread().interrupt();
087      throw mapException(e);
088    } catch (CancellationException | ExecutionException e) {
089      throw mapException(e);
090    }
091  }
092
093  /**
094   * {@inheritDoc}
095   *
096   * <p>This implementation calls {@link #get(long, TimeUnit)} and maps that method's standard
097   * exceptions (excluding {@link TimeoutException}, which is propagated) to instances of type
098   * {@code X} using {@link #mapException}.
099   *
100   * <p>In addition, if {@code get} throws an {@link InterruptedException}, this implementation will
101   * set the current thread's interrupt status before calling {@code mapException}.
102   *
103   * @throws X if {@link #get()} throws an {@link InterruptedException}, {@link
104   *     CancellationException}, or {@link ExecutionException}
105   */
106  @CanIgnoreReturnValue
107  @Override
108  public V checkedGet(long timeout, TimeUnit unit) throws TimeoutException, X {
109    try {
110      return get(timeout, unit);
111    } catch (InterruptedException e) {
112      Thread.currentThread().interrupt();
113      throw mapException(e);
114    } catch (CancellationException | ExecutionException e) {
115      throw mapException(e);
116    }
117  }
118}