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