BUG-579: Remove ImmutableMap in favor of LinkedHashMap
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / tree / StoreMetadataNode.java
index 974f817c71776c1bb7a7a27437eba0920e993482..ea83047a3101996e891a033f49f0fe34eee13990 100644 (file)
@@ -9,6 +9,7 @@ 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.Map;
 
 import org.opendaylight.yangtools.concepts.Identifiable;
@@ -18,7 +19,8 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
 
 import com.google.common.base.Optional;
-import com.google.common.collect.ImmutableMap;
+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> {
@@ -29,13 +31,19 @@ public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>,
 
     private final Map<PathArgument, StoreMetadataNode> children;
 
-    protected StoreMetadataNode(final NormalizedNode<?, ?> data, final UnsignedLong nodeVersion, final UnsignedLong subtreeVersion,
-            final Map<PathArgument, StoreMetadataNode> children) {
+    /**
+     *
+     * @param data
+     * @param nodeVersion
+     * @param subtreeVersion
+     * @param children Map of children, must not be modified externally
+     */
+    protected StoreMetadataNode(final NormalizedNode<?, ?> data, final UnsignedLong nodeVersion,
+            final UnsignedLong subtreeVersion, final Map<PathArgument, StoreMetadataNode> children) {
         this.nodeVersion = nodeVersion;
         this.subtreeVersion = subtreeVersion;
         this.data = data;
-        this.children = ImmutableMap.copyOf(children);
-
+        this.children = Preconditions.checkNotNull(children);
     }
 
     public static Builder builder() {
@@ -60,7 +68,7 @@ public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>,
     }
 
     public Iterable<StoreMetadataNode> getChildren() {
-        return children.values();
+        return Iterables.unmodifiableIterable(children.values());
     }
 
     @Override
@@ -80,24 +88,26 @@ public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>,
         return Optional.absent();
     }
 
-    public static Optional<StoreMetadataNode> getChild(final Optional<StoreMetadataNode> parent, final PathArgument child) {
+    public static Optional<StoreMetadataNode> getChild(final Optional<StoreMetadataNode> parent,
+            final PathArgument child) {
         if (parent.isPresent()) {
             return parent.get().getChild(child);
         }
         return Optional.absent();
     }
 
-    public static final StoreMetadataNode createRecursivelly(final NormalizedNode<?, ?> node, final UnsignedLong version) {
+    public static final StoreMetadataNode createRecursivelly(final NormalizedNode<?, ?> node,
+            final UnsignedLong nodeVersion, final UnsignedLong subtreeVersion) {
         Builder builder = builder() //
-                .setNodeVersion(version) //
-                .setSubtreeVersion(version) //
+                .setNodeVersion(nodeVersion) //
+                .setSubtreeVersion(subtreeVersion) //
                 .setData(node);
-        if(node instanceof NormalizedNodeContainer<?, ?, ?>) {
+        if (node instanceof NormalizedNodeContainer<?, ?, ?>) {
 
             @SuppressWarnings("unchecked")
             NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> nodeContainer = (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) node;
-            for(NormalizedNode<?, ?> subNode : nodeContainer.getValue()) {
-                builder.add(createRecursivelly(subNode, version));
+            for (NormalizedNode<?, ?> subNode : nodeContainer.getValue()) {
+                builder.add(createRecursivelly(subNode, nodeVersion, subtreeVersion));
             }
         }
         return builder.build();
@@ -105,15 +115,14 @@ public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>,
 
     public static class Builder {
 
-        private Builder() {
-
-        }
+        private UnsignedLong nodeVersion;
+        private UnsignedLong subtreeVersion;
+        private NormalizedNode<?, ?> data;
+        private Map<PathArgument, StoreMetadataNode> children = new LinkedHashMap<>();
+        private boolean dirty = false;
 
-        UnsignedLong nodeVersion = UnsignedLong.valueOf(0);
-        UnsignedLong subtreeVersion = UnsignedLong.valueOf(0);
-        NormalizedNode<?, ?> data;
+        private Builder() {}
 
-        final ImmutableMap.Builder<PathArgument, StoreMetadataNode> children = ImmutableMap.builder();
 
         public UnsignedLong getVersion() {
             return nodeVersion;
@@ -130,21 +139,31 @@ public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>,
             return this;
         }
 
-        public Builder setData(final NormalizedNode<?,?> data) {
+        public Builder setData(final NormalizedNode<?, ?> data) {
             this.data = data;
             return this;
         }
 
         public Builder add(final StoreMetadataNode node) {
+            if (dirty) {
+                children = new LinkedHashMap<>(children);
+                dirty = false;
+            }
             children.put(node.getIdentifier(), node);
             return this;
         }
 
         public StoreMetadataNode build() {
-            checkState(data != null,"Data node should not be null.");
-            checkState(subtreeVersion.compareTo(nodeVersion) >= 0, "Subtree version must be equals or greater than node version.");
-            return new StoreMetadataNode(data, nodeVersion, subtreeVersion, children.build());
+            checkState(data != null, "Data node should not be null.");
+            checkState(subtreeVersion.compareTo(nodeVersion) >= 0,
+                    "Subtree version must be equals or greater than node version.");
+            dirty = true;
+            return new StoreMetadataNode(data, nodeVersion, subtreeVersion, children);
         }
     }
 
+    public static StoreMetadataNode createRecursivelly(final NormalizedNode<?, ?> node, final UnsignedLong version) {
+        return createRecursivelly(node, version, version);
+    }
+
 }