Merge changes Ibe0145be,I0764bfa9
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / tree / StoreMetadataNode.java
index ea83047a3101996e891a033f49f0fe34eee13990..b8ad7368b5ea6acebc126b630e1b73bcfdd75404 100644 (file)
@@ -9,7 +9,8 @@ package org.opendaylight.controller.md.sal.dom.store.impl.tree;
 
 import static com.google.common.base.Preconditions.checkState;
 
-import java.util.LinkedHashMap;
+import java.util.Collections;
+import java.util.HashMap;
 import java.util.Map;
 
 import org.opendaylight.yangtools.concepts.Identifiable;
@@ -20,7 +21,6 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
 
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
-import com.google.common.collect.Iterables;
 import com.google.common.primitives.UnsignedLong;
 
 public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>, StoreTreeNode<StoreMetadataNode> {
@@ -46,10 +46,24 @@ public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>,
         this.children = Preconditions.checkNotNull(children);
     }
 
+    public static StoreMetadataNode createEmpty(final NormalizedNode<?, ?> data) {
+        return new StoreMetadataNode(data, UnsignedLong.ZERO, UnsignedLong.ZERO,
+                Collections.<PathArgument, StoreMetadataNode>emptyMap());
+    }
+
+    public StoreMetadataNode(final NormalizedNode<?, ?> data, final UnsignedLong nodeVersion,
+            final UnsignedLong subtreeVersion) {
+        this(data, nodeVersion, subtreeVersion, Collections.<PathArgument, StoreMetadataNode>emptyMap());
+    }
+
     public static Builder builder() {
         return new Builder();
     }
 
+    public static Builder builder(StoreMetadataNode node) {
+        return new Builder(node);
+    }
+
     public UnsignedLong getNodeVersion() {
         return this.nodeVersion;
     }
@@ -67,10 +81,6 @@ public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>,
         return this.data;
     }
 
-    public Iterable<StoreMetadataNode> getChildren() {
-        return Iterables.unmodifiableIterable(children.values());
-    }
-
     @Override
     public Optional<StoreMetadataNode> getChild(final PathArgument key) {
         return Optional.fromNullable(children.get(key));
@@ -96,7 +106,7 @@ public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>,
         return Optional.absent();
     }
 
-    public static final StoreMetadataNode createRecursivelly(final NormalizedNode<?, ?> node,
+    public static final StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node,
             final UnsignedLong nodeVersion, final UnsignedLong subtreeVersion) {
         Builder builder = builder() //
                 .setNodeVersion(nodeVersion) //
@@ -107,7 +117,7 @@ public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>,
             @SuppressWarnings("unchecked")
             NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> nodeContainer = (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) node;
             for (NormalizedNode<?, ?> subNode : nodeContainer.getValue()) {
-                builder.add(createRecursivelly(subNode, nodeVersion, subtreeVersion));
+                builder.add(createRecursively(subNode, nodeVersion, subtreeVersion));
             }
         }
         return builder.build();
@@ -118,11 +128,16 @@ public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>,
         private UnsignedLong nodeVersion;
         private UnsignedLong subtreeVersion;
         private NormalizedNode<?, ?> data;
-        private Map<PathArgument, StoreMetadataNode> children = new LinkedHashMap<>();
+        private Map<PathArgument, StoreMetadataNode> children;
         private boolean dirty = false;
 
-        private Builder() {}
+        private Builder() {
+            children = new HashMap<>();
+        }
 
+        public Builder(StoreMetadataNode node) {
+            children = new HashMap<>(node.children);
+        }
 
         public UnsignedLong getVersion() {
             return nodeVersion;
@@ -146,13 +161,22 @@ public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>,
 
         public Builder add(final StoreMetadataNode node) {
             if (dirty) {
-                children = new LinkedHashMap<>(children);
+                children = new HashMap<>(children);
                 dirty = false;
             }
             children.put(node.getIdentifier(), node);
             return this;
         }
 
+        public Builder remove(final PathArgument id) {
+            if (dirty) {
+                children = new HashMap<>(children);
+                dirty = false;
+            }
+            children.remove(id);
+            return this;
+        }
+
         public StoreMetadataNode build() {
             checkState(data != null, "Data node should not be null.");
             checkState(subtreeVersion.compareTo(nodeVersion) >= 0,
@@ -162,8 +186,7 @@ public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>,
         }
     }
 
-    public static StoreMetadataNode createRecursivelly(final NormalizedNode<?, ?> node, final UnsignedLong version) {
-        return createRecursivelly(node, version, version);
+    public static StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node, final UnsignedLong version) {
+        return createRecursively(node, version, version);
     }
-
 }