Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2008 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.google.common.util.concurrent;
 
 
A delegating wrapper around a ListenableFuture that adds support for the checkedGet() and checkedGet(long,java.util.concurrent.TimeUnit) methods.

Author(s):
Sven Mawson
Since:
1.0
 
 public abstract class AbstractCheckedFuture<V, X extends Exception>
     implements CheckedFuture<V, X> {
  
Constructs an AbstractCheckedFuture that wraps a delegate.
 
   protected AbstractCheckedFuture(ListenableFuture<V> delegate) {
     super(delegate);
   }

  
Translates from an java.lang.InterruptedException, java.util.concurrent.CancellationException or java.util.concurrent.ExecutionException thrown by get to an exception of type X to be thrown by checkedGet. Subclasses must implement this method.

If e is an InterruptedException, the calling checkedGet method has already restored the interrupt after catching the exception. If an implementation of mapException(java.lang.Exception) wishes to swallow the interrupt, it can do so by calling java.lang.Thread.interrupted().

Subclasses may choose to throw, rather than return, a subclass of RuntimeException to allow creating a CheckedFuture that throws both checked and unchecked exceptions.

 
   protected abstract X mapException(Exception e);

  

This implementation calls ForwardingFuture.get() and maps that method's standard exceptions to instances of type X using mapException(java.lang.Exception).

In addition, if get throws an java.lang.InterruptedException, this implementation will set the current thread's interrupt status before calling mapException.

 
   @Override
   public V checkedGet() throws X {
     try {
       return get();
     } catch (InterruptedException e) {
       Thread.currentThread().interrupt();
       throw mapException(e);
     } catch (CancellationException e) {
       throw mapException(e);
     } catch (ExecutionException e) {
       throw mapException(e);
     }
   }

  

This implementation calls ForwardingFuture.get(long,java.util.concurrent.TimeUnit) and maps that method's standard exceptions (excluding java.util.concurrent.TimeoutException, which is propagated) to instances of type X using mapException(java.lang.Exception).

In addition, if get throws an java.lang.InterruptedException, this implementation will set the current thread's interrupt status before calling mapException.

  public V checkedGet(long timeoutTimeUnit unitthrows TimeoutException, X {
    try {
      return get(timeoutunit);
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
      throw mapException(e);
    } catch (CancellationException e) {
      throw mapException(e);
    } catch (ExecutionException e) {
      throw mapException(e);
    }
  }
New to GrepCode? Check out our FAQ X