BUG-1276: fixed generated union constructor
[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
25     protected AbstractImmutableNormalizedNode(final K nodeIdentifier) {
26         this.nodeIdentifier = Preconditions.checkNotNull(nodeIdentifier, "nodeIdentifier");
27     }
28
29     @Override
30     public final QName getNodeType() {
31         return getIdentifier().getNodeType();
32     }
33
34     @Override
35     public final K getIdentifier() {
36         return nodeIdentifier;
37     }
38
39     @Override
40     public final CompositeNode getParent() {
41         throw new UnsupportedOperationException("Deprecated");
42     }
43
44     @Override
45     public final QName getKey() {
46         return getNodeType();
47     }
48
49     @Override
50     public final V setValue(final V value) {
51         throw new UnsupportedOperationException("Immutable");
52     }
53
54     @Override
55     public final String toString() {
56         return addToStringAttributes(Objects.toStringHelper(this)).toString();
57     }
58
59     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
60         return toStringHelper.add("nodeIdentifier", nodeIdentifier).add("value", getValue());
61     }
62
63     protected abstract boolean valueEquals(AbstractImmutableNormalizedNode<?, ?> other);
64     protected abstract int valueHashCode();
65
66     @Override
67     public final boolean equals(final Object obj) {
68         if (this == obj) {
69             return true;
70         }
71         if (obj == null) {
72             return false;
73         }
74         if (this.getClass() != obj.getClass()) {
75             return false;
76         }
77
78         final AbstractImmutableNormalizedNode<?, ?> other = (AbstractImmutableNormalizedNode<?, ?>)obj;
79         if (!nodeIdentifier.equals(other.nodeIdentifier)) {
80             return false;
81         }
82
83         return valueEquals(other);
84     }
85
86     @Override
87     public final int hashCode() {
88         int result = nodeIdentifier.hashCode();
89         result = 31 * result + valueHashCode();
90         return result;
91     }
92 }