Cleanup use of Guava library
[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 static java.util.Objects.requireNonNull;
12
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 java.util.function.Function;
20 import javax.annotation.Nonnull;
21
22 /**
23  * An implementation of CheckedFuture that provides similar behavior for the <code>get</code> methods
24  * that the <code>checkedGet</code> methods provide.
25  *
26  * <p>For {@link CancellationException} and {@link InterruptedException}, the specified exception mapper
27  * is invoked to translate them to the checked exception type.
28  *
29  * <p>For {@link ExecutionException}, the mapper is invoked to translate the cause to the checked exception
30  * and a new ExecutionException is thrown with the translated cause.
31  *
32  * @author Thomas Pantelis
33  *
34  * @param <V> The result type returned by this Future's get method
35  * @param <X> The checked exception type
36  */
37 public final class MappingCheckedFuture<V, X extends Exception> extends AbstractCheckedFuture<V, X> {
38
39     private final Function<Exception, X> mapper;
40
41     private MappingCheckedFuture(final ListenableFuture<V> delegate, final Function<Exception, X> mapper) {
42         super(delegate);
43         this.mapper = requireNonNull(mapper);
44     }
45
46     /**
47      * Creates a new <code>MappingCheckedFuture</code> that wraps the given {@link ListenableFuture}
48      * delegate.
49      *
50      * @param delegate the {@link ListenableFuture} to wrap
51      * @param mapper the mapping {@link Function} used to translate exceptions from the delegate
52      * @return a new <code>MappingCheckedFuture</code>
53      */
54     public static <V, X extends Exception> MappingCheckedFuture<V, X> create(
55             final ListenableFuture<V> delegate, final Function<Exception, X> mapper) {
56         return new MappingCheckedFuture<>(delegate, mapper);
57     }
58
59     @Override
60     @SuppressWarnings("checkstyle:parameterName")
61     protected X mapException(@Nonnull final Exception e) {
62         return mapper.apply(e);
63     }
64
65     private ExecutionException wrapInExecutionException(final String message, final Exception ex) {
66         return new ExecutionException(message, mapException(ex));
67     }
68
69     @Override
70     public V get() throws InterruptedException, ExecutionException {
71         try {
72             return super.get();
73         } catch (final InterruptedException e) {
74             Thread.currentThread().interrupt();
75             throw wrapInExecutionException("Operation was interrupted", e);
76         } catch (final CancellationException e) {
77             throw wrapInExecutionException("Operation was cancelled", e);
78         } catch (final ExecutionException e) {
79             throw wrapInExecutionException(e.getMessage(), e);
80         }
81     }
82
83     @Override
84     public V get(final long timeout, @Nonnull final TimeUnit unit)
85             throws InterruptedException, ExecutionException, TimeoutException {
86         try {
87             return super.get(timeout, unit);
88         } catch (final InterruptedException e) {
89             Thread.currentThread().interrupt();
90             throw wrapInExecutionException("Operation was interrupted", e);
91         } catch (final CancellationException e) {
92             throw wrapInExecutionException("Operation was cancelled", e);
93         } catch (final ExecutionException e) {
94             throw wrapInExecutionException(e.getMessage(), e);
95         }
96     }
97 }