BUG-648: Fixup hashCode/equals
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / nodes / AbstractImmutableNormalizedNode.java
index 96ff9e5f22676d49143a753712d95e2ae9080807..708f9225c2f29c640db725b4550b2d5386d2a6cf 100644 (file)
@@ -14,15 +14,16 @@ import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
 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 {
 
-    protected final K nodeIdentifier;
-    protected final V value;
+    private final K nodeIdentifier;
+    private final V value;
 
-    protected AbstractImmutableNormalizedNode(K nodeIdentifier, V value) {
+    protected AbstractImmutableNormalizedNode(final K nodeIdentifier, final V value) {
         this.nodeIdentifier = Preconditions.checkNotNull(nodeIdentifier, "nodeIdentifier");
         this.value = Preconditions.checkNotNull(value, "value");
     }
@@ -53,35 +54,46 @@ public abstract class AbstractImmutableNormalizedNode<K extends InstanceIdentifi
     }
 
     @Override
-    public final V setValue(V value) {
+    public final V setValue(final V value) {
         throw new UnsupportedOperationException("Immutable");
     }
 
     @Override
     public final String toString() {
-        return Objects.toStringHelper(this)
-                .add("nodeIdentifier", nodeIdentifier)
-                .add("value", value)
-                .toString();
+        return addToStringAttributes(Objects.toStringHelper(this)).toString();
     }
 
-    @Override
-    public boolean equals(Object o) {
-        if (this == o) return true;
-        if (!(o instanceof AbstractImmutableNormalizedNode)) return false;
-
-        AbstractImmutableNormalizedNode<?, ?> that = (AbstractImmutableNormalizedNode<?, ?>) o;
+    protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
+        return toStringHelper.add("nodeIdentifier", nodeIdentifier).add("value", getValue());
+    }
 
-        if (!nodeIdentifier.equals(that.nodeIdentifier)) return false;
-        if (!value.equals(that.value)) return false;
+    protected abstract boolean valueEquals(AbstractImmutableNormalizedNode<?, ?> other);
+    protected abstract int valueHashCode();
 
-        return true;
+    @Override
+    public final boolean equals(final Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null) {
+            return false;
+        }
+        if (this.getClass() != obj.getClass()) {
+            return false;
+        }
+
+        final AbstractImmutableNormalizedNode<?, ?> other = (AbstractImmutableNormalizedNode<?, ?>)obj;
+        if (!nodeIdentifier.equals(other.nodeIdentifier)) {
+            return false;
+        }
+
+        return valueEquals(other);
     }
 
     @Override
-    public int hashCode() {
+    public final int hashCode() {
         int result = nodeIdentifier.hashCode();
-        result = 31 * result + value.hashCode();
+        result = 31 * result + valueHashCode();
         return result;
     }
 }