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