Refactor AbstractIdentifiable
[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.MoreObjects.ToStringHelper;
11 import org.opendaylight.yangtools.concepts.AbstractSimpleIdentifiable;
12 import org.opendaylight.yangtools.concepts.Immutable;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16
17 public abstract class AbstractImmutableNormalizedNode<K extends PathArgument, V> extends AbstractSimpleIdentifiable<K>
18         implements NormalizedNode<K, V>, Immutable {
19     protected AbstractImmutableNormalizedNode(final K nodeIdentifier) {
20         super(nodeIdentifier);
21     }
22
23     @Override
24     public final QName getNodeType() {
25         return getIdentifier().getNodeType();
26     }
27
28     @Override
29     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
30         return super.addToStringAttributes(toStringHelper).add("value", getValue());
31     }
32
33     protected abstract boolean valueEquals(AbstractImmutableNormalizedNode<?, ?> other);
34
35     protected abstract int valueHashCode();
36
37     @Override
38     public final boolean equals(final Object obj) {
39         if (this == obj) {
40             return true;
41         }
42         if (obj == null || this.getClass() != obj.getClass()) {
43             return false;
44         }
45
46         final AbstractImmutableNormalizedNode<?, ?> other = (AbstractImmutableNormalizedNode<?, ?>)obj;
47         return getIdentifier().equals(other.getIdentifier()) && valueEquals(other);
48     }
49
50     @Override
51     public final int hashCode() {
52         return 31 * getIdentifier().hashCode() + valueHashCode();
53     }
54 }