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