X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-data-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fdata%2Fimpl%2Fschema%2Fbuilder%2Fimpl%2FImmutableLeafSetNodeBuilder.java;h=3f2609baaaccc9ad1c70db58fb72aee8e1a7f127;hb=8f2876d895936b36aba1fc3ec65b18900e559184;hp=dfc6be4a18a65246d3c32b2a180536e4dd644b86;hpb=72d620c6fb5312f594474a992ec65154c2562b07;p=yangtools.git diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/ImmutableLeafSetNodeBuilder.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/ImmutableLeafSetNodeBuilder.java index dfc6be4a18..3f2609baaa 100644 --- a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/ImmutableLeafSetNodeBuilder.java +++ b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/ImmutableLeafSetNodeBuilder.java @@ -7,91 +7,90 @@ */ package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl; +import com.google.common.collect.Maps; +import java.util.Collection; import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.List; +import java.util.HashMap; import java.util.Map; - +import java.util.Optional; import org.opendaylight.yangtools.concepts.Immutable; +import org.opendaylight.yangtools.util.MapAdaptor; +import org.opendaylight.yangtools.util.UnmodifiableCollection; import org.opendaylight.yangtools.yang.common.QName; -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.YangInstanceIdentifier.NodeIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode; import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode; import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder; 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 org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedValueNode; public class ImmutableLeafSetNodeBuilder implements ListNodeBuilder> { + private static final int DEFAULT_CAPACITY = 4; - private Map> value; - private InstanceIdentifier.NodeIdentifier nodeIdentifier; - private boolean dirty; + private final Map> value; + private NodeIdentifier nodeIdentifier; protected ImmutableLeafSetNodeBuilder() { - value = new LinkedHashMap<>(); - dirty = false; + value = new HashMap<>(DEFAULT_CAPACITY); + } + + protected ImmutableLeafSetNodeBuilder(final int sizeHint) { + if (sizeHint >= 0) { + value = Maps.newHashMapWithExpectedSize(sizeHint); + } else { + value = new HashMap<>(DEFAULT_CAPACITY); + } } protected ImmutableLeafSetNodeBuilder(final ImmutableLeafSetNode node) { nodeIdentifier = node.getIdentifier(); - value = node.getChildren(); - dirty = true; + value = MapAdaptor.getDefaultInstance().takeSnapshot(node.children); } public static ListNodeBuilder> create() { return new ImmutableLeafSetNodeBuilder<>(); } + public static ListNodeBuilder> create(final int sizeHint) { + return new ImmutableLeafSetNodeBuilder<>(sizeHint); + } + public static ListNodeBuilder> create(final LeafSetNode node) { if (!(node instanceof ImmutableLeafSetNode)) { throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass())); } - return new ImmutableLeafSetNodeBuilder((ImmutableLeafSetNode) node); - } - - private void checkDirty() { - if (dirty) { - value = new LinkedHashMap<>(value); - dirty = false; - } + return new ImmutableLeafSetNodeBuilder<>((ImmutableLeafSetNode) node); } @Override public ListNodeBuilder> withChild(final LeafSetEntryNode child) { - checkDirty(); this.value.put(child.getIdentifier(), child); return this; } @Override public ListNodeBuilder> withoutChild(final PathArgument key) { - checkDirty(); this.value.remove(key); return this; } @Override public LeafSetNode build() { - dirty = true; - return new ImmutableLeafSetNode<>(nodeIdentifier, value); + return new ImmutableLeafSetNode<>(nodeIdentifier, MapAdaptor.getDefaultInstance().optimize(value)); } @Override - public ListNodeBuilder> withNodeIdentifier( - final InstanceIdentifier.NodeIdentifier nodeIdentifier) { + public ListNodeBuilder> withNodeIdentifier(final NodeIdentifier nodeIdentifier) { this.nodeIdentifier = nodeIdentifier; return this; } @Override - public ListNodeBuilder> withValue(final List> value) { - checkDirty(); + public ListNodeBuilder> withValue(final Collection> value) { for (final LeafSetEntryNode leafSetEntry : value) { withChild(leafSetEntry); } @@ -102,7 +101,7 @@ public class ImmutableLeafSetNodeBuilder implements ListNodeBuilder> withChildValue(final T value, final Map attributes) { final ImmutableLeafSetEntryNodeBuilder b = ImmutableLeafSetEntryNodeBuilder.create(); - b.withNodeIdentifier(new InstanceIdentifier.NodeWithValue(nodeIdentifier.getNodeType(), value)); + b.withNodeIdentifier(new NodeWithValue<>(nodeIdentifier.getNodeType(), value)); b.withValue(value); b.withAttributes(attributes); return withChild(b.build()); @@ -110,24 +109,24 @@ public class ImmutableLeafSetNodeBuilder implements ListNodeBuilder> withChildValue(final T value) { - return withChildValue(value, Collections.emptyMap()); + return withChildValue(value, Collections.emptyMap()); } - protected final static class ImmutableLeafSetNode extends - AbstractImmutableNormalizedNode>> implements + protected static final class ImmutableLeafSetNode extends + AbstractImmutableNormalizedValueNode>> implements Immutable, LeafSetNode { - private final Map> children; + private final Map> children; - ImmutableLeafSetNode(final InstanceIdentifier.NodeIdentifier nodeIdentifier, - final Map> children) { - super(nodeIdentifier, Iterables.unmodifiableIterable(children.values())); + ImmutableLeafSetNode(final NodeIdentifier nodeIdentifier, + final Map> children) { + super(nodeIdentifier, UnmodifiableCollection.create(children.values())); this.children = children; } @Override - public Optional> getChild(final InstanceIdentifier.NodeWithValue child) { - return Optional.fromNullable(children.get(child)); + public Optional> getChild(final NodeWithValue child) { + return Optional.ofNullable(children.get(child)); } @Override @@ -135,10 +134,6 @@ public class ImmutableLeafSetNodeBuilder implements ListNodeBuilder> getChildren() { - return Collections.unmodifiableMap(children); - } - @Override protected boolean valueEquals(final AbstractImmutableNormalizedNode other) { return children.equals(((ImmutableLeafSetNode) other).children); @@ -152,8 +147,8 @@ public class ImmutableLeafSetNodeBuilder implements ListNodeBuilder, LeafSetNode> removeChild( - final PathArgument key) { + public NormalizedNodeContainerBuilder, LeafSetNode> + removeChild(final PathArgument key) { return withoutChild(key); }