ac0e7c944a50a087cba173037250344277d8a86c
[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 org.opendaylight.mdsal.dom.api.DOMRpcResult;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.Preconditions;
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 javax.annotation.Nonnull;
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     private final Collection<RpcError> errors;
31     private final NormalizedNode<?, ?> result;
32
33     private static Collection<RpcError> asCollection(final RpcError... errors) {
34         if (errors.length == 0) {
35             return Collections.emptyList();
36         } else {
37             return Arrays.asList(errors);
38         }
39     }
40
41     public DefaultDOMRpcResult(final NormalizedNode<?, ?> result, final RpcError... errors) {
42         this(result, asCollection(errors));
43     }
44
45     public DefaultDOMRpcResult(final RpcError... errors) {
46         this(null, asCollection(errors));
47     }
48
49     public DefaultDOMRpcResult(final NormalizedNode<?, ?> result) {
50         this(result, Collections.<RpcError>emptyList());
51     }
52
53     public DefaultDOMRpcResult(final NormalizedNode<?, ?> result, final @Nonnull Collection<RpcError> errors) {
54         this.result = result;
55         this.errors = Preconditions.checkNotNull(errors);
56     }
57
58     public DefaultDOMRpcResult(final @Nonnull Collection<RpcError> errors) {
59         this(null, errors);
60     }
61
62     @Override
63     public @Nonnull Collection<RpcError> getErrors() {
64         return errors;
65     }
66
67     @Override
68     public NormalizedNode<?, ?> getResult() {
69         return result;
70     }
71
72     @Override
73     public int hashCode() {
74         int ret = errors.hashCode();
75         if (result != null) {
76             ret = 31 * ret + result.hashCode();
77         }
78         return ret;
79     }
80
81     @Override
82     public boolean equals(final Object obj) {
83         if (this == obj) {
84             return true;
85         }
86         if (!(obj instanceof DefaultDOMRpcResult)) {
87             return false;
88         }
89
90         final DefaultDOMRpcResult other = (DefaultDOMRpcResult) obj;
91         if (!errors.equals(other.errors)) {
92             return false;
93         }
94         return Objects.equals(result, other.result);
95     }
96 }