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 February 2018.
039 */
040@Beta
041@Deprecated
042@GwtIncompatible
043public abstract class AbstractCheckedFuture<V, X extends Exception>
044    extends ForwardingListenableFuture.SimpleForwardingListenableFuture<V>
045    implements CheckedFuture<V, X> {
046  /**
047   * Constructs an {@code AbstractCheckedFuture} that wraps a delegate.
048   */
049  protected AbstractCheckedFuture(ListenableFuture<V> delegate) {
050    super(delegate);
051  }
052
053  /**
054   * Translates from an {@link InterruptedException}, {@link CancellationException} or {@link
055   * ExecutionException} thrown by {@code get} to an exception of type {@code X} to be thrown by
056   * {@code checkedGet}. Subclasses must implement this method.
057   *
058   * <p>If {@code e} is an {@code InterruptedException}, the calling {@code checkedGet} method has
059   * already restored the interrupt after catching the exception. If an implementation of {@link
060   * #mapException(Exception)} wishes to swallow the interrupt, it can do so by calling {@link
061   * Thread#interrupted()}.
062   *
063   * <p>Subclasses may choose to throw, rather than return, a subclass of {@code RuntimeException}
064   * to allow creating a CheckedFuture that throws both checked and unchecked exceptions.
065   */
066  // We might like @ForOverride here, but some subclasses invoke this from their get() methods.
067  protected abstract X mapException(Exception e);
068
069  /**
070   * {@inheritDoc}
071   *
072   * <p>This implementation calls {@link #get()} and maps that method's standard exceptions to
073   * instances of type {@code X} using {@link #mapException}.
074   *
075   * <p>In addition, if {@code get} throws an {@link InterruptedException}, this implementation will
076   * set the current thread's interrupt status before calling {@code mapException}.
077   *
078   * @throws X if {@link #get()} throws an {@link InterruptedException}, {@link
079   *     CancellationException}, or {@link ExecutionException}
080   */
081  @CanIgnoreReturnValue
082  @Override
083  public V checkedGet() throws X {
084    try {
085      return get();
086    } catch (InterruptedException e) {
087      Thread.currentThread().interrupt();
088      throw mapException(e);
089    } catch (CancellationException e) {
090      throw mapException(e);
091    } catch (ExecutionException e) {
092      throw mapException(e);
093    }
094  }
095
096  /**
097   * {@inheritDoc}
098   *
099   * <p>This implementation calls {@link #get(long, TimeUnit)} and maps that method's standard
100   * exceptions (excluding {@link TimeoutException}, which is propagated) to instances of type
101   * {@code X} using {@link #mapException}.
102   *
103   * <p>In addition, if {@code get} throws an {@link InterruptedException}, this implementation will
104   * set the current thread's interrupt status before calling {@code mapException}.
105   *
106   * @throws X if {@link #get()} throws an {@link InterruptedException}, {@link
107   *     CancellationException}, or {@link ExecutionException}
108   */
109  @CanIgnoreReturnValue
110  @Override
111  public V checkedGet(long timeout, TimeUnit unit) throws TimeoutException, X {
112    try {
113      return get(timeout, unit);
114    } catch (InterruptedException e) {
115      Thread.currentThread().interrupt();
116      throw mapException(e);
117    } catch (CancellationException e) {
118      throw mapException(e);
119    } catch (ExecutionException e) {
120      throw mapException(e);
121    }
122  }
123}