Integrate MRI projects for Neon
[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<? extends 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,
62             final @Nonnull Collection<? extends RpcError> errors) {
63         this.result = result;
64         this.errors = Preconditions.checkNotNull(errors);
65     }
66
67     public DefaultDOMRpcResult(final @Nonnull Collection<RpcError> errors) {
68         this(null, errors);
69     }
70
71     @Override
72     public @Nonnull Collection<? extends RpcError> getErrors() {
73         return errors;
74     }
75
76     @Override
77     public NormalizedNode<?, ?> getResult() {
78         return result;
79     }
80
81     @Override
82     public int hashCode() {
83         int ret = errors.hashCode();
84         if (result != null) {
85             ret = 31 * ret + result.hashCode();
86         }
87         return ret;
88     }
89
90     @Override
91     public boolean equals(final Object obj) {
92         if (this == obj) {
93             return true;
94         }
95         if (!(obj instanceof DefaultDOMRpcResult)) {
96             return false;
97         }
98
99         final DefaultDOMRpcResult other = (DefaultDOMRpcResult) obj;
100         if (!errors.equals(other.errors)) {
101             return false;
102         }
103         return Objects.equals(result, other.result);
104     }
105 }