BUG-648: do not keep HashMap$Values around
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / nodes / AbstractImmutableDataContainerNode.java
index 7954d56344bc1b729e9bad31c82b2e089c84a550..438b05c7da3af2bab1424fba7e56b00c80a007e7 100644 (file)
@@ -7,36 +7,55 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.nodes;
 
+import java.util.Collections;
 import java.util.Map;
 
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
 
 import com.google.common.base.Optional;
+import com.google.common.collect.Iterables;
 
-public abstract class AbstractImmutableDataContainerNode<K extends InstanceIdentifier.PathArgument>
-        extends AbstractImmutableNormalizedNode<K, Iterable<DataContainerChild<? extends InstanceIdentifier.PathArgument, ?>>>
-        implements DataContainerNode<K> {
+public abstract class AbstractImmutableDataContainerNode<K extends PathArgument> //
+        extends AbstractImmutableNormalizedNode<K, Iterable<DataContainerChild<? extends PathArgument, ?>>> //
+        implements Immutable, DataContainerNode<K> {
 
-    protected Map<InstanceIdentifier.PathArgument, DataContainerChild<? extends InstanceIdentifier.PathArgument, ?>> children;
+    protected final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> children;
 
-    public AbstractImmutableDataContainerNode(Map<InstanceIdentifier.PathArgument, DataContainerChild<? extends InstanceIdentifier.PathArgument, ?>> children, K nodeIdentifier) {
-        super(nodeIdentifier, children.values());
+    public AbstractImmutableDataContainerNode(
+            final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> children, final K nodeIdentifier) {
+        super(nodeIdentifier);
         this.children = children;
     }
 
     @Override
-    public Optional<DataContainerChild<? extends InstanceIdentifier.PathArgument, ?>> getChild(InstanceIdentifier.PathArgument child) {
-        return Optional.<DataContainerChild<? extends InstanceIdentifier.PathArgument, ?>>fromNullable(children.get(child));
+    public final Optional<DataContainerChild<? extends PathArgument, ?>> getChild(final PathArgument child) {
+        return Optional.<DataContainerChild<? extends PathArgument, ?>> fromNullable(children.get(child));
     }
 
     @Override
-    public String toString() {
-        final StringBuffer sb = new StringBuffer("ImmutableContainerNode{");
-        sb.append("nodeIdentifier=").append(nodeIdentifier);
-        sb.append(", children=").append(children);
-        sb.append('}');
-        return sb.toString();
+    public final Iterable<DataContainerChild<? extends PathArgument, ?>> getValue() {
+        return Iterables.unmodifiableIterable(children.values());
+    }
+
+    @Override
+    protected int valueHashCode() {
+        return children.hashCode();
+    }
+
+    public final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> getChildren() {
+        // Make sure we do not leak a mutable view
+        return Collections.unmodifiableMap(children);
+    }
+
+    @Override
+    protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
+        if (!(other instanceof AbstractImmutableDataContainerNode<?>)) {
+            return false;
+        }
+
+        return children.equals(((AbstractImmutableDataContainerNode<?>)other).children);
     }
 }