2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.common.api;
10 import static java.util.Objects.requireNonNull;
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;
21 * An implementation of CheckedFuture that provides similar behavior for the <code>get</code> methods
22 * that the <code>checkedGet</code> methods provide.
24 * <p>For {@link CancellationException} and {@link InterruptedException}, the specified exception mapper
25 * is invoked to translate them to the checked exception type.
27 * <p>For {@link ExecutionException}, the mapper is invoked to translate the cause to the checked exception
28 * and a new ExecutionException is thrown with the translated cause.
30 * @author Thomas Pantelis
32 * @param <V> The result type returned by this Future's get method
33 * @param <X> The checked exception type
36 public final class MappingCheckedFuture<V, X extends Exception> extends AbstractCheckedFuture<V, X> {
37 private final Function<Exception, X> mapper;
39 private MappingCheckedFuture(final ListenableFuture<V> delegate, final Function<Exception, X> mapper) {
41 this.mapper = requireNonNull(mapper);
45 * Creates a new <code>MappingCheckedFuture</code> that wraps the given {@link ListenableFuture}
48 * @param delegate the {@link ListenableFuture} to wrap
49 * @param mapper the mapping {@link Function} used to translate exceptions from the delegate
50 * @return a new <code>MappingCheckedFuture</code>
52 public static <V, X extends Exception> MappingCheckedFuture<V, X> create(
53 final ListenableFuture<V> delegate, final Function<Exception, X> mapper) {
54 return new MappingCheckedFuture<>(delegate, mapper);
58 @SuppressWarnings("checkstyle:parameterName")
59 protected X mapException(final Exception e) {
60 return mapper.apply(e);
63 private ExecutionException wrapInExecutionException(final String message, final Exception ex) {
64 return new ExecutionException(message, mapException(ex));
68 public V get() throws InterruptedException, ExecutionException {
71 } catch (final InterruptedException e) {
72 Thread.currentThread().interrupt();
73 throw wrapInExecutionException("Operation was interrupted", e);
74 } catch (final CancellationException e) {
75 throw wrapInExecutionException("Operation was cancelled", e);
76 } catch (final ExecutionException e) {
77 throw wrapInExecutionException(e.getMessage(), e);
82 public V get(final long timeout, final TimeUnit unit)
83 throws InterruptedException, ExecutionException, TimeoutException {
85 return super.get(timeout, unit);
86 } catch (final InterruptedException e) {
87 Thread.currentThread().interrupt();
88 throw wrapInExecutionException("Operation was interrupted", e);
89 } catch (final CancellationException e) {
90 throw wrapInExecutionException("Operation was cancelled", e);
91 } catch (final ExecutionException e) {
92 throw wrapInExecutionException(e.getMessage(), e);