89a669f43baa672cfbe46641c93b04adce11c933
[mdsal.git] / common / mdsal-common-api / src / main / java / org / opendaylight / mdsal / common / api / 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 package org.opendaylight.mdsal.common.api;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.AbstractCheckedFuture;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.concurrent.CancellationException;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
18 import java.util.function.Function;
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  * @deprecated Use {@code org.opendaylight.controller.md.sal.common.api.MappingCheckedFuture} instead.
36  */
37 @Deprecated
38 public final class MappingCheckedFuture<V, X extends Exception> extends AbstractCheckedFuture<V, X> {
39
40     private final Function<Exception, X> mapper;
41
42     private MappingCheckedFuture(final ListenableFuture<V> delegate, final Function<Exception, X> mapper) {
43         super(delegate);
44         this.mapper = requireNonNull(mapper);
45     }
46
47     /**
48      * Creates a new <code>MappingCheckedFuture</code> that wraps the given {@link ListenableFuture}
49      * delegate.
50      *
51      * @param delegate the {@link ListenableFuture} to wrap
52      * @param mapper the mapping {@link Function} used to translate exceptions from the delegate
53      * @return a new <code>MappingCheckedFuture</code>
54      */
55     public static <V, X extends Exception> MappingCheckedFuture<V, X> create(
56             final ListenableFuture<V> delegate, final Function<Exception, X> mapper) {
57         return new MappingCheckedFuture<>(delegate, mapper);
58     }
59
60     @Override
61     @SuppressWarnings("checkstyle:parameterName")
62     protected X mapException(@Nonnull final Exception e) {
63         return mapper.apply(e);
64     }
65
66     private ExecutionException wrapInExecutionException(final String message, final Exception ex) {
67         return new ExecutionException(message, mapException(ex));
68     }
69
70     @Override
71     public V get() throws InterruptedException, ExecutionException {
72         try {
73             return super.get();
74         } catch (final InterruptedException e) {
75             Thread.currentThread().interrupt();
76             throw wrapInExecutionException("Operation was interrupted", e);
77         } catch (final CancellationException e) {
78             throw wrapInExecutionException("Operation was cancelled", e);
79         } catch (final ExecutionException e) {
80             throw wrapInExecutionException(e.getMessage(), e);
81         }
82     }
83
84     @Override
85     public V get(final long timeout, @Nonnull final TimeUnit unit)
86             throws InterruptedException, ExecutionException, TimeoutException {
87         try {
88             return super.get(timeout, unit);
89         } catch (final InterruptedException e) {
90             Thread.currentThread().interrupt();
91             throw wrapInExecutionException("Operation was interrupted", e);
92         } catch (final CancellationException e) {
93             throw wrapInExecutionException("Operation was cancelled", e);
94         } catch (final ExecutionException e) {
95             throw wrapInExecutionException(e.getMessage(), e);
96         }
97     }
98 }