Fix eclipse/checkstyle warnings
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / concurrent / MappingCheckedFuture.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.yangtools.util.concurrent;
10
11 import com.google.common.base.Function;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.AbstractCheckedFuture;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.concurrent.CancellationException;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.TimeoutException;
19 import javax.annotation.Nonnull;
20
21 /**
22  * An implementation of CheckedFuture that provides similar behavior for the <code>get</code> methods
23  * that the <code>checkedGet</code> methods provide.
24  *
25  * <p>For {@link CancellationException} and {@link InterruptedException}, the specified exception mapper
26  * is invoked to translate them to the checked exception type.
27  *
28  * <p>For {@link ExecutionException}, the mapper is invoked to translate the cause to the checked exception
29  * and a new ExecutionException is thrown with the translated cause.
30  *
31  * @author Thomas Pantelis
32  *
33  * @param <V> The result type returned by this Future's get method
34  * @param <X> The checked exception type
35  */
36 public final class MappingCheckedFuture<V, X extends Exception> extends AbstractCheckedFuture<V, X> {
37
38     private final Function<Exception, X> mapper;
39
40     private MappingCheckedFuture(final ListenableFuture<V> delegate, final Function<Exception, X> mapper) {
41         super(delegate);
42         this.mapper = Preconditions.checkNotNull(mapper);
43     }
44
45     /**
46      * Creates a new <code>MappingCheckedFuture</code> that wraps the given {@link ListenableFuture}
47      * delegate.
48      *
49      * @param delegate the {@link ListenableFuture} to wrap
50      * @param mapper the mapping {@link Function} used to translate exceptions from the delegate
51      * @return a new <code>MappingCheckedFuture</code>
52      */
53     public static <V, X extends Exception> MappingCheckedFuture<V, X> create(
54             final ListenableFuture<V> delegate, final Function<Exception, X> mapper) {
55         return new MappingCheckedFuture<>(delegate, mapper);
56     }
57
58     @Override
59     @SuppressWarnings("checkstyle:parameterName")
60     protected X mapException(@Nonnull final Exception e) {
61         return mapper.apply(e);
62     }
63
64     private ExecutionException wrapInExecutionException(final String message, final Exception ex) {
65         return new ExecutionException(message, mapException(ex));
66     }
67
68     @Override
69     public V get() throws InterruptedException, ExecutionException {
70         try {
71             return super.get();
72         } catch (final InterruptedException e) {
73             Thread.currentThread().interrupt();
74             throw wrapInExecutionException("Operation was interrupted", e);
75         } catch (final CancellationException e) {
76             throw wrapInExecutionException("Operation was cancelled", e);
77         } catch (final ExecutionException e) {
78             throw wrapInExecutionException(e.getMessage(), e);
79         }
80     }
81
82     @Override
83     public V get(final long timeout, @Nonnull final TimeUnit unit)
84             throws InterruptedException, ExecutionException, TimeoutException {
85         try {
86             return super.get(timeout, unit);
87         } catch (final InterruptedException e) {
88             Thread.currentThread().interrupt();
89             throw wrapInExecutionException("Operation was interrupted", e);
90         } catch (final CancellationException e) {
91             throw wrapInExecutionException("Operation was cancelled", e);
92         } catch (final ExecutionException e) {
93             throw wrapInExecutionException(e.getMessage(), e);
94         }
95     }
96 }