Add YangError.getErrorTag()
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / nodes / AbstractImmutableNormalizedNode.java
index ceb20124c81f709faf28cf6c647fac50be52e464..848982521b507a3ff62d9bf0606fd6bb4ff530dc 100644 (file)
@@ -7,72 +7,48 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.nodes;
 
+import com.google.common.base.MoreObjects.ToStringHelper;
+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.Preconditions;
-
-public abstract class AbstractImmutableNormalizedNode<K extends InstanceIdentifier.PathArgument,V>
+public abstract class AbstractImmutableNormalizedNode<K extends PathArgument, V> extends AbstractIdentifiable<K>
         implements NormalizedNode<K, V>, Immutable {
-
-    protected final K nodeIdentifier;
-    protected V value;
-
-    protected AbstractImmutableNormalizedNode(K nodeIdentifier, V value) {
-        this.nodeIdentifier = Preconditions.checkNotNull(nodeIdentifier, "nodeIdentifier");
-        this.value = Preconditions.checkNotNull(value, "value");
+    protected AbstractImmutableNormalizedNode(final K nodeIdentifier) {
+        super(nodeIdentifier);
     }
 
     @Override
-    public QName getNodeType() {
+    public final QName getNodeType() {
         return getIdentifier().getNodeType();
     }
 
     @Override
-    public K getIdentifier() {
-        return nodeIdentifier;
+    protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
+        return super.addToStringAttributes(toStringHelper).add("value", getValue());
     }
 
-    @Override
-    public CompositeNode getParent() {
-        throw new UnsupportedOperationException("Deprecated");
-    }
+    protected abstract boolean valueEquals(AbstractImmutableNormalizedNode<?, ?> other);
 
-    @Override
-    public QName getKey() {
-        return getNodeType();
-    }
+    protected abstract int valueHashCode();
 
     @Override
-    public V getValue() {
-        return value;
-    }
-
-    @Override
-    public V setValue(V value) {
-        throw new UnsupportedOperationException("Immutable");
-    }
-
-    @Override
-    public boolean equals(Object o) {
-        if (this == o) return true;
-        if (!(o instanceof AbstractImmutableNormalizedNode)) return false;
-
-        AbstractImmutableNormalizedNode that = (AbstractImmutableNormalizedNode) o;
-
-        if (!nodeIdentifier.equals(that.nodeIdentifier)) return false;
-        if (!value.equals(that.value)) return false;
+    public final boolean equals(final Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null || this.getClass() != obj.getClass()) {
+            return false;
+        }
 
-        return true;
+        final AbstractImmutableNormalizedNode<?, ?> other = (AbstractImmutableNormalizedNode<?, ?>)obj;
+        return getIdentifier().equals(other.getIdentifier()) && valueEquals(other);
     }
 
     @Override
-    public int hashCode() {
-        int result = nodeIdentifier.hashCode();
-        result = 31 * result + value.hashCode();
-        return result;
+    public final int hashCode() {
+        return 31 * getIdentifier().hashCode() + valueHashCode();
     }
 }