EMMA Coverage Report (generated Wed Oct 26 13:47:17 EDT 2011)
[all classes][org.jclouds.concurrent]

COVERAGE SUMMARY FOR SOURCE FILE [ExceptionParsingListenableFuture.java]

nameclass, %method, %block, %line, %
ExceptionParsingListenableFuture.java100% (1/1)44%  (4/9)56%  (43/77)58%  (11/19)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ExceptionParsingListenableFuture100% (1/1)44%  (4/9)56%  (43/77)58%  (11/19)
addListener (Runnable, Executor): void 0%   (0/1)0%   (0/6)0%   (0/2)
cancel (boolean): boolean 0%   (0/1)0%   (0/5)0%   (0/1)
create (ListenableFuture, Function): ExceptionParsingListenableFuture 0%   (0/1)0%   (0/6)0%   (0/1)
isCancelled (): boolean 0%   (0/1)0%   (0/4)0%   (0/1)
isDone (): boolean 0%   (0/1)0%   (0/4)0%   (0/1)
get (): Object 100% (1/1)56%  (5/9)67%  (2/3)
attemptConvert (Exception): Object 100% (1/1)74%  (14/19)67%  (2/3)
ExceptionParsingListenableFuture (ListenableFuture, Function): void 100% (1/1)100% (13/13)100% (4/4)
get (long, TimeUnit): Object 100% (1/1)100% (11/11)100% (3/3)

1/**
2 * Licensed to jclouds, Inc. (jclouds) under one or more
3 * contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  jclouds licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.jclouds.concurrent;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22 
23import java.util.concurrent.ExecutionException;
24import java.util.concurrent.Executor;
25import java.util.concurrent.TimeUnit;
26import java.util.concurrent.TimeoutException;
27 
28import com.google.common.base.Function;
29import com.google.common.util.concurrent.ListenableFuture;
30 
31/**
32 * Transforms the exceptions in a future upon get
33 * 
34 * Temporarily here until the following is resolved: <a
35 * href="http://code.google.com/p/guava-libraries/issues/detail?id=310"> guava issue 310</a>
36 * 
37 * @author Adrian Cole
38 */
39public class ExceptionParsingListenableFuture<T> implements ListenableFuture<T> {
40 
41   private final ListenableFuture<T> future;
42   private final Function<Exception, T> function;
43 
44   public static <T> ExceptionParsingListenableFuture<T> create(ListenableFuture<T> future,
45            Function<Exception, T> function) {
46      return new ExceptionParsingListenableFuture<T>(future, function);
47   }
48 
49   public ExceptionParsingListenableFuture(ListenableFuture<T> future, Function<Exception, T> function) {
50      this.future = checkNotNull(future);
51      this.function = checkNotNull(function);
52   }
53 
54   public boolean cancel(boolean mayInterruptIfRunning) {
55      return future.cancel(mayInterruptIfRunning);
56   }
57 
58   public T get() throws InterruptedException, ExecutionException {
59      try {
60         return future.get();
61      } catch (Exception e) {
62         return attemptConvert(e);
63      }
64   }
65 
66   private T attemptConvert(Exception e) {
67      if (e instanceof ExecutionException && e.getCause() instanceof Exception)
68         return function.apply((Exception) e.getCause());
69      return function.apply(e);
70   }
71 
72   public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
73      try {
74         return future.get(timeout, unit);
75      } catch (Exception e) {
76         return attemptConvert(e);
77      }
78   }
79 
80   public boolean isCancelled() {
81      return future.isCancelled();
82   }
83 
84   public boolean isDone() {
85      return future.isDone();
86   }
87 
88   @Override
89   public void addListener(Runnable listener, Executor exec) {
90      future.addListener(listener, exec);
91   }
92}

[all classes][org.jclouds.concurrent]
EMMA 2.0.5312 (C) Vladimir Roubtsov