Rework NormalizedNode type hierarchy
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / nodes / AbstractImmutableNormalizedNode.java
index 708f9225c2f29c640db725b4550b2d5386d2a6cf..bda43c15d6fefd63cb754e7e5574625820d34f34 100644 (file)
@@ -7,93 +7,45 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.nodes;
 
+import com.google.common.base.MoreObjects.ToStringHelper;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.concepts.AbstractIdentifiable;
 import org.opendaylight.yangtools.concepts.Immutable;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.CompositeNode;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 
-import com.google.common.base.Objects;
-import com.google.common.base.Objects.ToStringHelper;
-import com.google.common.base.Preconditions;
-
-public abstract class AbstractImmutableNormalizedNode<K extends InstanceIdentifier.PathArgument,V>
-        implements NormalizedNode<K, V>, Immutable {
-
-    private final K nodeIdentifier;
-    private final V value;
-
-    protected AbstractImmutableNormalizedNode(final K nodeIdentifier, final V value) {
-        this.nodeIdentifier = Preconditions.checkNotNull(nodeIdentifier, "nodeIdentifier");
-        this.value = Preconditions.checkNotNull(value, "value");
-    }
-
-    @Override
-    public final QName getNodeType() {
-        return getIdentifier().getNodeType();
-    }
-
-    @Override
-    public final K getIdentifier() {
-        return nodeIdentifier;
-    }
-
-    @Override
-    public final CompositeNode getParent() {
-        throw new UnsupportedOperationException("Deprecated");
-    }
-
-    @Override
-    public final QName getKey() {
-        return getNodeType();
+public abstract class AbstractImmutableNormalizedNode<K extends PathArgument, N extends NormalizedNode>
+        extends AbstractIdentifiable<PathArgument, K> implements NormalizedNode, Immutable {
+    protected AbstractImmutableNormalizedNode(final K nodeIdentifier) {
+        super(nodeIdentifier);
     }
 
-    @Override
-    public final V getValue() {
-        return value;
-    }
-
-    @Override
-    public final V setValue(final V value) {
-        throw new UnsupportedOperationException("Immutable");
-    }
-
-    @Override
-    public final String toString() {
-        return addToStringAttributes(Objects.toStringHelper(this)).toString();
-    }
-
-    protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
-        return toStringHelper.add("nodeIdentifier", nodeIdentifier).add("value", getValue());
-    }
-
-    protected abstract boolean valueEquals(AbstractImmutableNormalizedNode<?, ?> other);
-    protected abstract int valueHashCode();
-
     @Override
     public final boolean equals(final Object obj) {
         if (this == obj) {
             return true;
         }
-        if (obj == null) {
-            return false;
-        }
-        if (this.getClass() != obj.getClass()) {
+        final Class<N> clazz = implementedType();
+        if (!clazz.isInstance(obj)) {
             return false;
         }
-
-        final AbstractImmutableNormalizedNode<?, ?> other = (AbstractImmutableNormalizedNode<?, ?>)obj;
-        if (!nodeIdentifier.equals(other.nodeIdentifier)) {
-            return false;
-        }
-
-        return valueEquals(other);
+        final N other = clazz.cast(obj);
+        return getIdentifier().equals(other.getIdentifier()) && valueEquals(other);
     }
 
     @Override
     public final int hashCode() {
-        int result = nodeIdentifier.hashCode();
-        result = 31 * result + valueHashCode();
-        return result;
+        return 31 * getIdentifier().hashCode() + valueHashCode();
+    }
+
+    @Override
+    protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
+        return super.addToStringAttributes(toStringHelper).add("body", body());
     }
+
+    protected abstract @NonNull Class<N> implementedType();
+
+    protected abstract int valueHashCode();
+
+    protected abstract boolean valueEquals(@NonNull N other);
 }