Migrate mdsal-dom-spi to JDT annotations
[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 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 org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
21 import org.opendaylight.yangtools.concepts.Immutable;
22 import org.opendaylight.yangtools.yang.common.RpcError;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24
25 /**
26  * Utility class implementing {@link DefaultDOMRpcResult}.
27  */
28 @Beta
29 @NonNullByDefault
30 public final class DefaultDOMRpcResult implements DOMRpcResult, Immutable, Serializable {
31     private static final long serialVersionUID = 1L;
32
33     private final @Nullable NormalizedNode<?, ?> result;
34     private final Collection<? extends RpcError> errors;
35
36     public DefaultDOMRpcResult(final NormalizedNode<?, ?> result, final RpcError... errors) {
37         this(result, asCollection(errors));
38     }
39
40     public DefaultDOMRpcResult(final RpcError... errors) {
41         this(null, asCollection(errors));
42     }
43
44     public DefaultDOMRpcResult(final @Nullable NormalizedNode<?, ?> result) {
45         this(result, Collections.emptyList());
46     }
47
48     public DefaultDOMRpcResult(final @Nullable NormalizedNode<?, ?> result,
49             final Collection<? extends RpcError> errors) {
50         this.result = result;
51         this.errors = requireNonNull(errors);
52     }
53
54     public DefaultDOMRpcResult(final Collection<RpcError> errors) {
55         this(null, errors);
56     }
57
58     private static Collection<RpcError> asCollection(final RpcError... errors) {
59         return errors.length == 0 ? Collections.emptyList() : Arrays.asList(errors);
60     }
61
62     @Override
63     public Collection<? extends RpcError> getErrors() {
64         return errors;
65     }
66
67     @Override
68     public @Nullable 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 @Nullable Object obj) {
83         if (this == obj) {
84             return true;
85         }
86         if (!(obj instanceof DefaultDOMRpcResult)) {
87             return false;
88         }
89         final DefaultDOMRpcResult other = (DefaultDOMRpcResult) obj;
90         return errors.equals(other.errors) && Objects.equals(result, other.result);
91     }
92 }