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