96ff9e5f22676d49143a753712d95e2ae9080807
[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.Preconditions;
18
19 public abstract class AbstractImmutableNormalizedNode<K extends InstanceIdentifier.PathArgument,V>
20         implements NormalizedNode<K, V>, Immutable {
21
22     protected final K nodeIdentifier;
23     protected final V value;
24
25     protected AbstractImmutableNormalizedNode(K nodeIdentifier, V value) {
26         this.nodeIdentifier = Preconditions.checkNotNull(nodeIdentifier, "nodeIdentifier");
27         this.value = Preconditions.checkNotNull(value, "value");
28     }
29
30     @Override
31     public final QName getNodeType() {
32         return getIdentifier().getNodeType();
33     }
34
35     @Override
36     public final K getIdentifier() {
37         return nodeIdentifier;
38     }
39
40     @Override
41     public final CompositeNode getParent() {
42         throw new UnsupportedOperationException("Deprecated");
43     }
44
45     @Override
46     public final QName getKey() {
47         return getNodeType();
48     }
49
50     @Override
51     public final V getValue() {
52         return value;
53     }
54
55     @Override
56     public final V setValue(V value) {
57         throw new UnsupportedOperationException("Immutable");
58     }
59
60     @Override
61     public final String toString() {
62         return Objects.toStringHelper(this)
63                 .add("nodeIdentifier", nodeIdentifier)
64                 .add("value", value)
65                 .toString();
66     }
67
68     @Override
69     public boolean equals(Object o) {
70         if (this == o) return true;
71         if (!(o instanceof AbstractImmutableNormalizedNode)) return false;
72
73         AbstractImmutableNormalizedNode<?, ?> that = (AbstractImmutableNormalizedNode<?, ?>) o;
74
75         if (!nodeIdentifier.equals(that.nodeIdentifier)) return false;
76         if (!value.equals(that.value)) return false;
77
78         return true;
79     }
80
81     @Override
82     public int hashCode() {
83         int result = nodeIdentifier.hashCode();
84         result = 31 * result + value.hashCode();
85         return result;
86     }
87 }