376682a26d8076018520b62081d24fafb9796a72
[controller.git] / opendaylight / md-sal / sal-dom-compat / src / main / java / org / opendaylight / controller / sal / core / compat / AbstractDOMRpcResultFutureAdapter.java
1 /*
2  * Copyright (c) 2018 Inocybe Technologies 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.controller.sal.core.compat;
9
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;
20
21 /**
22  * Base for a DOMRpcResult future adapter.
23  *
24  * @author Thomas Pantelis
25  */
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;
32
33     AbstractDOMRpcResultFutureAdapter(CheckedFuture<F, FE> delegate, ExceptionMapper<TE> exMapper) {
34         this.delegate = delegate;
35         this.exMapper = exMapper;
36     }
37
38     protected abstract T transform(F fromResult);
39
40     public CheckedFuture<F, FE> delegate() {
41         return delegate;
42     }
43
44     @Override
45     public void addListener(Runnable listener, Executor executor) {
46         delegate.addListener(listener, executor);
47     }
48
49     @Override
50     public boolean cancel(boolean mayInterruptIfRunning) {
51         return delegate.cancel(mayInterruptIfRunning);
52     }
53
54     @Override
55     public boolean isCancelled() {
56         return delegate.isCancelled();
57     }
58
59     @Override
60     public boolean isDone() {
61         return delegate.isDone();
62     }
63
64     @Override
65     public T get() throws InterruptedException, ExecutionException {
66         if (result != null) {
67             return result.orElse(null);
68         }
69
70         try {
71             return transformIfNecessary(delegate.get());
72         } catch (ExecutionException e) {
73             throw new ExecutionException(e.getMessage(), exMapper.apply(e));
74         }
75     }
76
77     @Override
78     public T get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException,
79             TimeoutException {
80         if (result != null) {
81             return result.orElse(null);
82         }
83
84         try {
85             return transformIfNecessary(delegate.get(timeout, unit));
86         } catch (ExecutionException e) {
87             throw new ExecutionException(e.getMessage(), exMapper.apply(e));
88         }
89     }
90
91     @Override
92     @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
93     public T checkedGet() throws TE {
94         try {
95             return get();
96         } catch (InterruptedException | ExecutionException e) {
97             throw exMapper.apply(e);
98         }
99     }
100
101     @Override
102     @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
103     public T checkedGet(final long timeout, final TimeUnit unit) throws TimeoutException, TE {
104         try {
105             return get(timeout, unit);
106         } catch (InterruptedException | ExecutionException e) {
107             throw exMapper.apply(e);
108         }
109     }
110
111     private synchronized T transformIfNecessary(F delegateResult) {
112         if (result == null) {
113             if (delegateResult == null) {
114                 result = Optional.empty();
115             } else {
116                 result = Optional.of(transform(delegateResult));
117             }
118         }
119
120         return result.orElse(null);
121     }
122 }