Rename DOMDataTreeChangeService
[mdsal.git] / dom / mdsal-dom-spi / src / main / java / org / opendaylight / mdsal / dom / spi / DefaultDOMRpcResult.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.spi;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.io.Serial;
15 import java.io.Serializable;
16 import java.util.Collection;
17 import java.util.List;
18 import java.util.Objects;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
22 import org.opendaylight.yangtools.concepts.Immutable;
23 import org.opendaylight.yangtools.yang.common.RpcError;
24 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
25
26 /**
27  * Utility class implementing {@link DefaultDOMRpcResult}.
28  */
29 @Beta
30 @NonNullByDefault
31 public final class DefaultDOMRpcResult implements DOMRpcResult, Immutable, Serializable {
32     @Serial
33     private static final long serialVersionUID = 1L;
34
35     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Interfaces do not specify Serializable")
36     private final @Nullable ContainerNode result;
37     // FIXME: a plain Collection is bad for equality
38     private final Collection<? extends RpcError> errors;
39
40     public DefaultDOMRpcResult(final ContainerNode value, final RpcError... errors) {
41         this(value, List.of(errors));
42     }
43
44     public DefaultDOMRpcResult(final RpcError... errors) {
45         this(null, List.of(errors));
46     }
47
48     public DefaultDOMRpcResult(final @Nullable ContainerNode result) {
49         this(result, List.of());
50     }
51
52     public DefaultDOMRpcResult(final @Nullable ContainerNode value, final Collection<? extends RpcError> errors) {
53         result = value;
54         this.errors = requireNonNull(errors);
55     }
56
57     public DefaultDOMRpcResult(final Collection<RpcError> errors) {
58         this(null, errors);
59     }
60
61     @Override
62     public Collection<? extends RpcError> errors() {
63         return errors;
64     }
65
66     @Override
67     public @Nullable ContainerNode value() {
68         return result;
69     }
70
71     @Override
72     public int hashCode() {
73         int ret = errors.hashCode();
74         if (result != null) {
75             ret = 31 * ret + result.hashCode();
76         }
77         return ret;
78     }
79
80     @Override
81     public boolean equals(final @Nullable Object obj) {
82         return this == obj || obj instanceof DefaultDOMRpcResult other && errors.equals(other.errors)
83             && Objects.equals(result, other.result);
84     }
85 }