From: Robert Varga Date: Thu, 27 Mar 2014 03:55:56 +0000 (+0100) Subject: BUG-579: Remove ImmutableMap in favor of LinkedHashMap X-Git-Tag: autorelease-tag-v20140601202136_82eb3f9~263^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=1b1b752c8941ef8799a5fa954651500e4819e660 BUG-579: Remove ImmutableMap in favor of LinkedHashMap This prevents a single round og ImmutableMapBuilder.build() as a trade-off for not leaking a mutable reference to the map. Change-Id: Idb97a5f4ece1d2f27af728310d9b0807ae305926 Signed-off-by: Robert Varga --- diff --git a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/tree/StoreMetadataNode.java b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/tree/StoreMetadataNode.java index 8ba0013eb8..ea83047a31 100644 --- a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/tree/StoreMetadataNode.java +++ b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/tree/StoreMetadataNode.java @@ -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, StoreTreeNode { @@ -29,13 +31,19 @@ public class StoreMetadataNode implements Immutable, Identifiable, private final Map 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 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, } public Iterable getChildren() { - return children.values(); + return Iterables.unmodifiableIterable(children.values()); } @Override @@ -110,7 +118,8 @@ public class StoreMetadataNode implements Immutable, Identifiable, private UnsignedLong nodeVersion; private UnsignedLong subtreeVersion; private NormalizedNode data; - private final ImmutableMap.Builder children = ImmutableMap.builder(); + private Map children = new LinkedHashMap<>(); + private boolean dirty = false; private Builder() {} @@ -136,6 +145,10 @@ public class StoreMetadataNode implements Immutable, Identifiable, } public Builder add(final StoreMetadataNode node) { + if (dirty) { + children = new LinkedHashMap<>(children); + dirty = false; + } children.put(node.getIdentifier(), node); return this; } @@ -144,7 +157,8 @@ public class StoreMetadataNode implements Immutable, Identifiable, 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()); + dirty = true; + return new StoreMetadataNode(data, nodeVersion, subtreeVersion, children); } }