Merge "Remove unused imports"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableMapNodeBuilder.java
index 7c26eb14ecddc78bb769a13e9cf90c9c1454aa05..c7c58e1899942c016bd4f4bc906ae886eaf08e07 100644 (file)
@@ -7,34 +7,56 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
 
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
 import org.opendaylight.yangtools.concepts.Immutable;
 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.MapEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
 
 import com.google.common.base.Optional;
 import com.google.common.collect.Iterables;
-import com.google.common.collect.Maps;
 
 public class ImmutableMapNodeBuilder
         implements CollectionNodeBuilder<MapEntryNode, MapNode> {
 
-    private Map<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> value = Maps.newLinkedHashMap();
+    private Map<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> value;
     private InstanceIdentifier.NodeIdentifier nodeIdentifier;
     private boolean dirty = false;
 
+    protected ImmutableMapNodeBuilder() {
+        this.value = new LinkedHashMap<>();
+        this.dirty = false;
+    }
+
+    protected ImmutableMapNodeBuilder(final ImmutableMapNode node) {
+        this.nodeIdentifier = node.getIdentifier();
+        this.value = node.children;
+        this.dirty = true;
+    }
+
     public static CollectionNodeBuilder<MapEntryNode, MapNode> create() {
         return new ImmutableMapNodeBuilder();
     }
 
+    public static CollectionNodeBuilder<MapEntryNode, MapNode> create(final MapNode node) {
+        if (!(node instanceof ImmutableMapNode)) {
+            throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
+        }
+
+        return new ImmutableMapNodeBuilder((ImmutableMapNode) node);
+    }
+
     private void checkDirty() {
         if (dirty) {
-            value = Maps.newLinkedHashMap(value);
+            value = new LinkedHashMap<>(value);
             dirty = false;
         }
     }
@@ -46,6 +68,13 @@ public class ImmutableMapNodeBuilder
         return this;
     }
 
+    @Override
+    public CollectionNodeBuilder<MapEntryNode, MapNode> withoutChild(final InstanceIdentifier.PathArgument key) {
+        checkDirty();
+        this.value.remove(key);
+        return this;
+    }
+
     @Override
     public CollectionNodeBuilder<MapEntryNode, MapNode> withValue(final List<MapEntryNode> value) {
         // TODO replace or putAll ?
@@ -74,7 +103,14 @@ public class ImmutableMapNodeBuilder
         return withChild(child);
     }
 
-    static final class ImmutableMapNode extends AbstractImmutableNormalizedNode<InstanceIdentifier.NodeIdentifier, Iterable<MapEntryNode>> implements Immutable,MapNode {
+
+    @Override
+    public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, MapEntryNode, MapNode> removeChild(
+            final PathArgument key) {
+        return withoutChild(key);
+    }
+
+    protected static final class ImmutableMapNode extends AbstractImmutableNormalizedNode<InstanceIdentifier.NodeIdentifier, Iterable<MapEntryNode>> implements Immutable,MapNode {
 
         private final Map<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> children;