Merge "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 com.google.common.base.Objects;
11 import com.google.common.base.Objects.ToStringHelper;
12 import com.google.common.base.Preconditions;
13
14 import org.opendaylight.yangtools.concepts.Immutable;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18
19 public abstract class AbstractImmutableNormalizedNode<K extends InstanceIdentifier.PathArgument,V> implements NormalizedNode<K, V>, Immutable {
20     private final K nodeIdentifier;
21
22     protected AbstractImmutableNormalizedNode(final K nodeIdentifier) {
23         this.nodeIdentifier = Preconditions.checkNotNull(nodeIdentifier, "nodeIdentifier");
24     }
25
26     @Override
27     public final QName getNodeType() {
28         return getIdentifier().getNodeType();
29     }
30
31     @Override
32     public final K getIdentifier() {
33         return nodeIdentifier;
34     }
35
36     @Override
37     public final String toString() {
38         return addToStringAttributes(Objects.toStringHelper(this)).toString();
39     }
40
41     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
42         return toStringHelper.add("nodeIdentifier", nodeIdentifier).add("value", getValue());
43     }
44
45     protected abstract boolean valueEquals(AbstractImmutableNormalizedNode<?, ?> other);
46     protected abstract int valueHashCode();
47
48     @Override
49     public final boolean equals(final Object obj) {
50         if (this == obj) {
51             return true;
52         }
53         if (obj == null) {
54             return false;
55         }
56         if (this.getClass() != obj.getClass()) {
57             return false;
58         }
59
60         final AbstractImmutableNormalizedNode<?, ?> other = (AbstractImmutableNormalizedNode<?, ?>)obj;
61         if (!nodeIdentifier.equals(other.nodeIdentifier)) {
62             return false;
63         }
64
65         return valueEquals(other);
66     }
67
68     @Override
69     public final int hashCode() {
70         int result = nodeIdentifier.hashCode();
71         result = 31 * result + valueHashCode();
72         return result;
73     }
74 }