Add DOMOperationService and related interfaces
[mdsal.git] / dom / mdsal-dom-api / src / main / java / org / opendaylight / mdsal / dom / api / DOMOperationCallback.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.dom.api;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.util.concurrent.SettableFuture;
14 import java.util.concurrent.CompletableFuture;
15 import java.util.function.Consumer;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.opendaylight.yangtools.concepts.CheckedValue;
18
19 /**
20  * A callback to be executed when an operation invocation completes. This interface is modeled as a {@link Consumer}
21  * of {@link CheckedValue}, which needs to be unwrapped.
22  *
23  * <p>
24  * Alternatively, you can use {@link #of(Consumer, Consumer)} utility method, which provides the equivalent
25  * dispatch with a nicer interface:
26  * <code>
27  *     DOMOperationCallback callback = DOMOperationCallback.of((success) -&gt; {
28  *         // ... code to handle success ...
29  *     }, (failure) -&gt; {
30  *         // ... code to handle failure ...
31  *     });
32  * </code>
33  *
34  * <p>
35  * Finally, you can create a bridging {@link DOMOperationCallback} through either
36  * {@link #completingFuture(SettableFuture)} or {@link #completingFuture(CompletableFuture)}.
37  *
38  * @author Robert Varga
39  */
40 @Beta
41 @NonNullByDefault
42 @FunctionalInterface
43 public interface DOMOperationCallback extends Consumer<CheckedValue<DOMOperationResult, DOMOperationException>> {
44     /**
45      * Create a DOMOperationCallback composed of two separate consumers, one for success and one for failure.
46      *
47      * @param onSuccess Callback to invoke on success
48      * @param onFailure Callback to invoke on failure
49      * @return A {@link DOMOperationCallback} which delegates to provided methods
50      * @throws NullPointerException if any of the arguments is null
51      */
52     static DOMOperationCallback of(final Consumer<DOMOperationResult> onSuccess,
53             final Consumer<DOMOperationException> onFailure) {
54         requireNonNull(onSuccess);
55         requireNonNull(onFailure);
56         return result -> {
57             if (result.isPresent()) {
58                 onSuccess.accept(result.get());
59             } else {
60                 onFailure.accept(result.getException());
61             }
62         };
63     }
64
65     /**
66      * Create a {@link DOMOperationCallback} which completes the specified future.
67      *
68      * @param future {@link CompletableFuture} to complete
69      * @return A {@link DOMOperationCallback}
70      * @throws NullPointerException if any of the arguments is null
71      */
72     static DOMOperationCallback completingFuture(final CompletableFuture<DOMOperationResult> future) {
73         requireNonNull(future);
74         return of(future::complete, future::completeExceptionally);
75     }
76
77     /**
78      * Create a {@link DOMOperationCallback} which completes the specified future.
79      *
80      * @param future {@link SettableFuture} to complete
81      * @return A {@link DOMOperationCallback}
82      * @throws NullPointerException if any of the arguments is null
83      */
84     static DOMOperationCallback completingFuture(final SettableFuture<DOMOperationResult> future) {
85         requireNonNull(future);
86         return of(future::set, future::setException);
87     }
88 }