2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.mdsal.dom.api;
10 import static java.util.Objects.requireNonNull;
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;
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.
24 * Alternatively, you can use {@link #of(Consumer, Consumer)} utility method, which provides the equivalent
25 * dispatch with a nicer interface:
27 * DOMOperationCallback callback = DOMOperationCallback.of((success) -> {
28 * // ... code to handle success ...
29 * }, (failure) -> {
30 * // ... code to handle failure ...
35 * Finally, you can create a bridging {@link DOMOperationCallback} through either
36 * {@link #completingFuture(SettableFuture)} or {@link #completingFuture(CompletableFuture)}.
38 * @author Robert Varga
43 public interface DOMOperationCallback extends Consumer<CheckedValue<DOMOperationResult, DOMOperationException>> {
45 * Create a DOMOperationCallback composed of two separate consumers, one for success and one for failure.
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
52 static DOMOperationCallback of(final Consumer<DOMOperationResult> onSuccess,
53 final Consumer<DOMOperationException> onFailure) {
54 requireNonNull(onSuccess);
55 requireNonNull(onFailure);
57 if (result.isPresent()) {
58 onSuccess.accept(result.get());
60 onFailure.accept(result.getException());
66 * Create a {@link DOMOperationCallback} which completes the specified future.
68 * @param future {@link CompletableFuture} to complete
69 * @return A {@link DOMOperationCallback}
70 * @throws NullPointerException if any of the arguments is null
72 static DOMOperationCallback completingFuture(final CompletableFuture<DOMOperationResult> future) {
73 requireNonNull(future);
74 return of(future::complete, future::completeExceptionally);
78 * Create a {@link DOMOperationCallback} which completes the specified future.
80 * @param future {@link SettableFuture} to complete
81 * @return A {@link DOMOperationCallback}
82 * @throws NullPointerException if any of the arguments is null
84 static DOMOperationCallback completingFuture(final SettableFuture<DOMOperationResult> future) {
85 requireNonNull(future);
86 return of(future::set, future::setException);