2 * Copyright (c) 2018 Inocybe Technologies 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.sal.core.compat;
10 import com.google.common.util.concurrent.CheckedFuture;
11 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import java.util.Optional;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.Executor;
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.TimeoutException;
17 import org.opendaylight.mdsal.dom.api.DOMRpcException;
18 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
19 import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
22 * Base for a DOMRpcResult future adapter.
24 * @author Thomas Pantelis
26 @SuppressWarnings("checkstyle:ClassTypeParameterName")
27 public abstract class AbstractDOMRpcResultFutureAdapter<T extends DOMRpcResult, TE extends DOMRpcException,
28 F extends DOMRpcResult, FE extends DOMRpcException> implements CheckedFuture<T, TE> {
29 private final CheckedFuture<F, FE> delegate;
30 private final ExceptionMapper<TE> exMapper;
31 private volatile Optional<T> result;
33 AbstractDOMRpcResultFutureAdapter(CheckedFuture<F, FE> delegate, ExceptionMapper<TE> exMapper) {
34 this.delegate = delegate;
35 this.exMapper = exMapper;
38 protected abstract T transform(F fromResult);
40 public CheckedFuture<F, FE> delegate() {
45 public void addListener(Runnable listener, Executor executor) {
46 delegate.addListener(listener, executor);
50 public boolean cancel(boolean mayInterruptIfRunning) {
51 return delegate.cancel(mayInterruptIfRunning);
55 public boolean isCancelled() {
56 return delegate.isCancelled();
60 public boolean isDone() {
61 return delegate.isDone();
65 public T get() throws InterruptedException, ExecutionException {
67 return result.orElse(null);
71 return transformIfNecessary(delegate.get());
72 } catch (ExecutionException e) {
73 throw new ExecutionException(e.getMessage(), exMapper.apply(e));
78 public T get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException,
81 return result.orElse(null);
85 return transformIfNecessary(delegate.get(timeout, unit));
86 } catch (ExecutionException e) {
87 throw new ExecutionException(e.getMessage(), exMapper.apply(e));
92 @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
93 public T checkedGet() throws TE {
96 } catch (InterruptedException | ExecutionException e) {
97 throw exMapper.apply(e);
102 @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
103 public T checkedGet(final long timeout, final TimeUnit unit) throws TimeoutException, TE {
105 return get(timeout, unit);
106 } catch (InterruptedException | ExecutionException e) {
107 throw exMapper.apply(e);
111 private synchronized T transformIfNecessary(F delegateResult) {
112 if (result == null) {
113 if (delegateResult == null) {
114 result = Optional.empty();
116 result = Optional.of(transform(delegateResult));
120 return result.orElse(null);