94cc510645a2bd4500ace771f33b407b2e1d92c0
[controller.git] / opendaylight / md-sal / sal-dom-spi / src / main / java / org / opendaylight / controller / md / sal / 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.controller.md.sal.dom.spi;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.io.Serializable;
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.Objects;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
20 import org.opendaylight.yangtools.concepts.Immutable;
21 import org.opendaylight.yangtools.yang.common.RpcError;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23
24 /**
25  * Utility class implementing {@link DefaultDOMRpcResult}.
26  */
27 @Beta
28 public final class DefaultDOMRpcResult implements DOMRpcResult, Immutable, Serializable {
29     private static final long serialVersionUID = 1L;
30
31     // Flagged as "Non-transient non-serializable instance field" - the Collection is Serializable but the RpcError
32     // interface isn't. In lieu of changing the interface, we assume the implementation is Serializable which is
33     // reasonable since the only implementation that is actually used is from the RpcResultBuilder.
34     @SuppressFBWarnings("SE_BAD_FIELD")
35     private final Collection<RpcError> errors;
36
37     // Unfortunately the NormalizedNode interface isn't Serializable but we assume the implementations are.
38     @SuppressFBWarnings("SE_BAD_FIELD")
39     private final NormalizedNode<?, ?> result;
40
41     private static Collection<RpcError> asCollection(final RpcError... errors) {
42         if (errors.length == 0) {
43             return Collections.emptyList();
44         } else {
45             return Arrays.asList(errors);
46         }
47     }
48
49     public DefaultDOMRpcResult(final NormalizedNode<?, ?> result, final RpcError... errors) {
50         this(result, asCollection(errors));
51     }
52
53     public DefaultDOMRpcResult(final RpcError... errors) {
54         this(null, asCollection(errors));
55     }
56
57     public DefaultDOMRpcResult(final NormalizedNode<?, ?> result) {
58         this(result, Collections.<RpcError>emptyList());
59     }
60
61     public DefaultDOMRpcResult(final NormalizedNode<?, ?> result, final @Nonnull Collection<RpcError> errors) {
62         this.result = result;
63         this.errors = Preconditions.checkNotNull(errors);
64     }
65
66     public DefaultDOMRpcResult(final @Nonnull Collection<RpcError> errors) {
67         this(null, errors);
68     }
69
70     @Override
71     public @Nonnull Collection<RpcError> getErrors() {
72         return errors;
73     }
74
75     @Override
76     public NormalizedNode<?, ?> getResult() {
77         return result;
78     }
79
80     @Override
81     public int hashCode() {
82         int ret = errors.hashCode();
83         if (result != null) {
84             ret = 31 * ret + result.hashCode();
85         }
86         return ret;
87     }
88
89     @Override
90     public boolean equals(final Object obj) {
91         if (this == obj) {
92             return true;
93         }
94         if (!(obj instanceof DefaultDOMRpcResult)) {
95             return false;
96         }
97
98         final DefaultDOMRpcResult other = (DefaultDOMRpcResult) obj;
99         if (!errors.equals(other.errors)) {
100             return false;
101         }
102         return Objects.equals(result, other.result);
103     }
104 }