Rework NormalizedNode type hierarchy
[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.eclipse.jdt.annotation.NonNull;
12 import org.opendaylight.yangtools.concepts.AbstractIdentifiable;
13 import org.opendaylight.yangtools.concepts.Immutable;
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, N extends NormalizedNode>
18         extends AbstractIdentifiable<PathArgument, K> implements NormalizedNode, Immutable {
19     protected AbstractImmutableNormalizedNode(final K nodeIdentifier) {
20         super(nodeIdentifier);
21     }
22
23     @Override
24     public final boolean equals(final Object obj) {
25         if (this == obj) {
26             return true;
27         }
28         final Class<N> clazz = implementedType();
29         if (!clazz.isInstance(obj)) {
30             return false;
31         }
32         final N other = clazz.cast(obj);
33         return getIdentifier().equals(other.getIdentifier()) && valueEquals(other);
34     }
35
36     @Override
37     public final int hashCode() {
38         return 31 * getIdentifier().hashCode() + valueHashCode();
39     }
40
41     @Override
42     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
43         return super.addToStringAttributes(toStringHelper).add("body", body());
44     }
45
46     protected abstract @NonNull Class<N> implementedType();
47
48     protected abstract int valueHashCode();
49
50     protected abstract boolean valueEquals(@NonNull N other);
51 }