Turn DOMRpcResult's result back to NormalizedNode
[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.Serializable;
15 import java.util.Arrays;
16 import java.util.Collection;
17 import java.util.Collections;
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 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26
27 /**
28  * Utility class implementing {@link DefaultDOMRpcResult}.
29  */
30 @Beta
31 @NonNullByDefault
32 public final class DefaultDOMRpcResult implements DOMRpcResult, Immutable, Serializable {
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     private final Collection<? extends RpcError> errors;
38
39     public DefaultDOMRpcResult(final ContainerNode result, final RpcError... errors) {
40         this(result, asCollection(errors));
41     }
42
43     public DefaultDOMRpcResult(final RpcError... errors) {
44         this(null, asCollection(errors));
45     }
46
47     public DefaultDOMRpcResult(final @Nullable ContainerNode result) {
48         this(result, Collections.emptyList());
49     }
50
51     public DefaultDOMRpcResult(final @Nullable ContainerNode result,
52             final Collection<? extends RpcError> errors) {
53         this.result = result;
54         this.errors = requireNonNull(errors);
55     }
56
57     public DefaultDOMRpcResult(final Collection<RpcError> errors) {
58         this(null, errors);
59     }
60
61     private static Collection<RpcError> asCollection(final RpcError... errors) {
62         return errors.length == 0 ? Collections.emptyList() : Arrays.asList(errors);
63     }
64
65     @Override
66     public Collection<? extends RpcError> getErrors() {
67         return errors;
68     }
69
70     @Override
71     public @Nullable NormalizedNode<?, ?> getResult() {
72         return result;
73     }
74
75     @Override
76     public int hashCode() {
77         int ret = errors.hashCode();
78         if (result != null) {
79             ret = 31 * ret + result.hashCode();
80         }
81         return ret;
82     }
83
84     @Override
85     public boolean equals(final @Nullable Object obj) {
86         if (this == obj) {
87             return true;
88         }
89         if (!(obj instanceof DefaultDOMRpcResult)) {
90             return false;
91         }
92         final DefaultDOMRpcResult other = (DefaultDOMRpcResult) obj;
93         return errors.equals(other.errors) && Objects.equals(result, other.result);
94     }
95 }