Merge "Bug 447 - Yang documentation generator improvements"
[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.Objects;
17 import com.google.common.base.Objects.ToStringHelper;
18 import com.google.common.base.Preconditions;
19
20 public abstract class AbstractImmutableNormalizedNode<K extends InstanceIdentifier.PathArgument,V>
21         implements NormalizedNode<K, V>, Immutable {
22
23     private final K nodeIdentifier;
24     private final V value;
25
26     protected AbstractImmutableNormalizedNode(final K nodeIdentifier, final V value) {
27         this.nodeIdentifier = Preconditions.checkNotNull(nodeIdentifier, "nodeIdentifier");
28         this.value = Preconditions.checkNotNull(value, "value");
29     }
30
31     @Override
32     public final QName getNodeType() {
33         return getIdentifier().getNodeType();
34     }
35
36     @Override
37     public final K getIdentifier() {
38         return nodeIdentifier;
39     }
40
41     @Override
42     public final CompositeNode getParent() {
43         throw new UnsupportedOperationException("Deprecated");
44     }
45
46     @Override
47     public final QName getKey() {
48         return getNodeType();
49     }
50
51     @Override
52     public final V getValue() {
53         return value;
54     }
55
56     @Override
57     public final V setValue(final V value) {
58         throw new UnsupportedOperationException("Immutable");
59     }
60
61     @Override
62     public final String toString() {
63         return addToStringAttributes(Objects.toStringHelper(this)).toString();
64     }
65
66     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
67         return toStringHelper.add("nodeIdentifier", nodeIdentifier).add("value", getValue());
68     }
69
70     protected abstract boolean valueEquals(AbstractImmutableNormalizedNode<?, ?> other);
71     protected abstract int valueHashCode();
72
73     @Override
74     public final boolean equals(final Object obj) {
75         if (this == obj) {
76             return true;
77         }
78         if (obj == null) {
79             return false;
80         }
81         if (this.getClass() != obj.getClass()) {
82             return false;
83         }
84
85         final AbstractImmutableNormalizedNode<?, ?> other = (AbstractImmutableNormalizedNode<?, ?>)obj;
86         if (!nodeIdentifier.equals(other.nodeIdentifier)) {
87             return false;
88         }
89
90         return valueEquals(other);
91     }
92
93     @Override
94     public final int hashCode() {
95         int result = nodeIdentifier.hashCode();
96         result = 31 * result + valueHashCode();
97         return result;
98     }
99 }