X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-dom-broker%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fmd%2Fsal%2Fdom%2Fstore%2Fimpl%2Ftree%2FStoreMetadataNode.java;h=455777b7fb9a1b315c036c6a5d9d6482473e0fa7;hb=d1243dc6da239191adb49fe6c78a933553af1939;hp=8ba0013eb8c0f20870e3a62cc1fa239e6d649f32;hpb=335a9cfe42385a217ef002e4ab42f9cd958ad202;p=controller.git 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..455777b7fb 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,8 @@ package org.opendaylight.controller.md.sal.dom.store.impl.tree; import static com.google.common.base.Preconditions.checkState; +import java.util.Collections; +import java.util.LinkedHashMap; import java.util.Map; import org.opendaylight.yangtools.concepts.Identifiable; @@ -18,7 +20,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,19 +32,39 @@ 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 StoreMetadataNode createEmpty(final NormalizedNode data) { + return new StoreMetadataNode(data, UnsignedLong.ZERO, UnsignedLong.ZERO, + Collections.emptyMap()); + } + public StoreMetadataNode(final NormalizedNode data, final UnsignedLong nodeVersion, + final UnsignedLong subtreeVersion) { + this(data, nodeVersion, subtreeVersion, Collections.emptyMap()); } public static Builder builder() { return new Builder(); } + public static Builder builder(StoreMetadataNode node) { + return new Builder(node); + } + public UnsignedLong getNodeVersion() { return this.nodeVersion; } @@ -60,7 +83,7 @@ public class StoreMetadataNode implements Immutable, Identifiable, } public Iterable getChildren() { - return children.values(); + return Iterables.unmodifiableIterable(children.values()); } @Override @@ -88,7 +111,7 @@ public class StoreMetadataNode implements Immutable, Identifiable, 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) // @@ -99,7 +122,7 @@ public class StoreMetadataNode implements Immutable, Identifiable, @SuppressWarnings("unchecked") NormalizedNodeContainer> nodeContainer = (NormalizedNodeContainer>) node; for (NormalizedNode subNode : nodeContainer.getValue()) { - builder.add(createRecursivelly(subNode, nodeVersion, subtreeVersion)); + builder.add(createRecursively(subNode, nodeVersion, subtreeVersion)); } } return builder.build(); @@ -110,10 +133,16 @@ 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; + private boolean dirty = false; - private Builder() {} + private Builder() { + children = new LinkedHashMap<>(); + } + public Builder(StoreMetadataNode node) { + children = new LinkedHashMap<>(node.children); + } public UnsignedLong getVersion() { return nodeVersion; @@ -136,20 +165,33 @@ 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; } + public Builder remove(final PathArgument id) { + if (dirty) { + children = new LinkedHashMap<>(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, "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); } } - 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); } - }