Merge "Startup archetype: remove 'Impl' from config subsystem Module name."
[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 java.io.Serializable;
13 import java.util.Arrays;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.Objects;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
19 import org.opendaylight.yangtools.concepts.Immutable;
20 import org.opendaylight.yangtools.yang.common.RpcError;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22
23 /**
24  * Utility class implementing {@link DefaultDOMRpcResult}.
25  */
26 @Beta
27 public final class DefaultDOMRpcResult implements DOMRpcResult, Immutable, Serializable {
28     private static final long serialVersionUID = 1L;
29     private final Collection<RpcError> errors;
30     private final NormalizedNode<?, ?> result;
31
32     private static Collection<RpcError> asCollection(final RpcError... errors) {
33         if (errors.length == 0) {
34             return Collections.emptyList();
35         } else {
36             return Arrays.asList(errors);
37         }
38     }
39
40     public DefaultDOMRpcResult(final NormalizedNode<?, ?> result, final RpcError... errors) {
41         this(result, asCollection(errors));
42     }
43
44     public DefaultDOMRpcResult(final RpcError... errors) {
45         this(null, asCollection(errors));
46     }
47
48     public DefaultDOMRpcResult(final NormalizedNode<?, ?> result) {
49         this(result, Collections.<RpcError>emptyList());
50     }
51
52     public DefaultDOMRpcResult(final NormalizedNode<?, ?> result, final @Nonnull Collection<RpcError> errors) {
53         this.result = result;
54         this.errors = Preconditions.checkNotNull(errors);
55     }
56
57     public DefaultDOMRpcResult(final @Nonnull Collection<RpcError> errors) {
58         this(null, errors);
59     }
60
61     @Override
62     public @Nonnull Collection<RpcError> getErrors() {
63         return errors;
64     }
65
66     @Override
67     public NormalizedNode<?, ?> getResult() {
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 Object obj) {
82         if (this == obj) {
83             return true;
84         }
85         if (!(obj instanceof DefaultDOMRpcResult)) {
86             return false;
87         }
88
89         final DefaultDOMRpcResult other = (DefaultDOMRpcResult) obj;
90         if (!errors.equals(other.errors)) {
91             return false;
92         }
93         return Objects.equals(result, other.result);
94     }
95 }