BUG-648: do not keep HashMap$Values around
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableLeafSetNodeBuilder.java
index f352b984c7ab348693b5e083b89ac1c712106e7e..6d04d5989cd41cb2a965425a2a1ec6f01b5e7f55 100644 (file)
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
 
+import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.util.MapAdaptor;
+import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedValueNode;
 
 import com.google.common.base.Optional;
-import com.google.common.collect.Maps;
-import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
+import com.google.common.collect.Iterables;
 
-public class ImmutableLeafSetNodeBuilder<T>
-        implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
+public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
 
-    protected Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> value;
-    protected InstanceIdentifier.NodeIdentifier nodeIdentifier;
+    private final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> value;
+    private InstanceIdentifier.NodeIdentifier nodeIdentifier;
+
+    protected ImmutableLeafSetNodeBuilder() {
+        value = new HashMap<>();
+    }
+
+    protected ImmutableLeafSetNodeBuilder(final ImmutableLeafSetNode<T> node) {
+        nodeIdentifier = node.getIdentifier();
+        value = MapAdaptor.getDefaultInstance().takeSnapshot(node.children);
+    }
 
     public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
         return new ImmutableLeafSetNodeBuilder<>();
     }
 
-    public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(LeafSetEntryNode<T> child) {
-        if(this.value == null) {
-            this.value = Maps.newLinkedHashMap();
+    public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafSetNode<T> node) {
+        if (!(node instanceof ImmutableLeafSetNode<?>)) {
+            throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
         }
 
+        return new ImmutableLeafSetNodeBuilder<T>((ImmutableLeafSetNode<T>) node);
+    }
+
+    @Override
+    public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
         this.value.put(child.getIdentifier(), child);
         return this;
     }
 
+    @Override
+    public ListNodeBuilder<T, LeafSetEntryNode<T>> withoutChild(final PathArgument key) {
+        this.value.remove(key);
+        return this;
+    }
+
     @Override
     public LeafSetNode<T> build() {
-        return new ImmutableLeafSetNode<>(nodeIdentifier, value);
+        return new ImmutableLeafSetNode<>(nodeIdentifier, MapAdaptor.getDefaultInstance().optimize(value));
     }
 
     @Override
-    public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(InstanceIdentifier.NodeIdentifier nodeIdentifier) {
+    public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(
+            final InstanceIdentifier.NodeIdentifier nodeIdentifier) {
         this.nodeIdentifier = nodeIdentifier;
         return this;
     }
 
     @Override
-    public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(List<LeafSetEntryNode<T>> value) {
-        for (LeafSetEntryNode<T> leafSetEntry : value) {
+    public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final List<LeafSetEntryNode<T>> value) {
+        for (final LeafSetEntryNode<T> leafSetEntry : value) {
             withChild(leafSetEntry);
         }
-
         return this;
     }
 
+
     @Override
-    public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(T value) {
-        return withChild(new ImmutableLeafSetEntryNodeBuilder.ImmutableLeafSetEntryNode<>(new InstanceIdentifier.NodeWithValue(nodeIdentifier.getNodeType(), value), value));
+    public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value, final Map<QName, String> attributes) {
+        final ImmutableLeafSetEntryNodeBuilder<T> b = ImmutableLeafSetEntryNodeBuilder.create();
+        b.withNodeIdentifier(new InstanceIdentifier.NodeWithValue(nodeIdentifier.getNodeType(), value));
+        b.withValue(value);
+        b.withAttributes(attributes);
+        return withChild(b.build());
     }
 
-    final class ImmutableLeafSetNode<T> extends AbstractImmutableNormalizedNode<InstanceIdentifier.NodeIdentifier, Iterable<LeafSetEntryNode<T>>> implements LeafSetNode<T> {
+    @Override
+    public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value) {
+        return withChildValue(value, Collections.<QName,String>emptyMap());
+    }
+
+    protected final static class ImmutableLeafSetNode<T> extends
+            AbstractImmutableNormalizedValueNode<InstanceIdentifier.NodeIdentifier, Iterable<LeafSetEntryNode<T>>> implements
+            Immutable, LeafSetNode<T> {
 
-        private final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> mappedChildren;
+        private final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children;
 
-        ImmutableLeafSetNode(InstanceIdentifier.NodeIdentifier nodeIdentifier, Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children) {
-            super(nodeIdentifier, children.values());
-            this.mappedChildren = children;
+        ImmutableLeafSetNode(final InstanceIdentifier.NodeIdentifier nodeIdentifier,
+                final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children) {
+            super(nodeIdentifier, Iterables.unmodifiableIterable(children.values()));
+            this.children = children;
+        }
+
+        @Override
+        public Optional<LeafSetEntryNode<T>> getChild(final InstanceIdentifier.NodeWithValue child) {
+            return Optional.fromNullable(children.get(child));
         }
 
         @Override
-        public Optional<LeafSetEntryNode<T>> getChild(InstanceIdentifier.NodeWithValue child) {
-            return Optional.fromNullable(mappedChildren.get(child));
+        protected int valueHashCode() {
+            return children.hashCode();
         }
 
         @Override
-        public String toString() {
-            final StringBuffer sb = new StringBuffer("ImmutableLeafSetNode{");
-            sb.append("nodeIdentifier=").append(nodeIdentifier);
-            sb.append(", children=").append(value);
-            sb.append('}');
-            return sb.toString();
+        protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
+            return children.equals(((ImmutableLeafSetNode<?>) other).children);
         }
     }
 
+    @Override
+    public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
+            final LeafSetEntryNode<T> child) {
+        return withChild(child);
+    }
+
+    @Override
+    public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> removeChild(
+            final PathArgument key) {
+        return withoutChild(key);
+    }
+
 }