Merge "Fixed possible NPE in CodecMapping."
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / nodes / AbstractImmutableNormalizedNode.java
1 /*
2  * Copyright (c) 2013 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.yangtools.yang.data.impl.schema.nodes;
9
10 import org.opendaylight.yangtools.concepts.Immutable;
11 import org.opendaylight.yangtools.yang.common.QName;
12 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
13 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
15
16 import com.google.common.base.Preconditions;
17
18 public abstract class AbstractImmutableNormalizedNode<K extends InstanceIdentifier.PathArgument,V>
19         implements NormalizedNode<K, V>, Immutable {
20
21     protected final K nodeIdentifier;
22     protected V value;
23
24     protected AbstractImmutableNormalizedNode(K nodeIdentifier, V value) {
25         this.nodeIdentifier = Preconditions.checkNotNull(nodeIdentifier, "nodeIdentifier");
26         this.value = Preconditions.checkNotNull(value, "value");
27     }
28
29     @Override
30     public QName getNodeType() {
31         return getIdentifier().getNodeType();
32     }
33
34     @Override
35     public K getIdentifier() {
36         return nodeIdentifier;
37     }
38
39     @Override
40     public CompositeNode getParent() {
41         throw new UnsupportedOperationException("Deprecated");
42     }
43
44     @Override
45     public QName getKey() {
46         return getNodeType();
47     }
48
49     @Override
50     public V getValue() {
51         return value;
52     }
53
54     @Override
55     public V setValue(V value) {
56         throw new UnsupportedOperationException("Immutable");
57     }
58
59     @Override
60     public boolean equals(Object o) {
61         if (this == o) return true;
62         if (!(o instanceof AbstractImmutableNormalizedNode)) return false;
63
64         AbstractImmutableNormalizedNode that = (AbstractImmutableNormalizedNode) o;
65
66         if (!nodeIdentifier.equals(that.nodeIdentifier)) return false;
67         if (!value.equals(that.value)) return false;
68
69         return true;
70     }
71
72     @Override
73     public int hashCode() {
74         int result = nodeIdentifier.hashCode();
75         result = 31 * result + value.hashCode();
76         return result;
77     }
78 }