Add NormalizedNodeContainer.size()
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableOrderedMapNodeBuilder.java
index fb413da5c44e0764cfc3fd5bcf5969688d67c13d..178525be51563cfb83c077202cee229fc8623342 100644 (file)
@@ -7,14 +7,15 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
 
-import com.google.common.base.Optional;
 import com.google.common.collect.Iterables;
 import java.util.Collection;
 import java.util.LinkedHashMap;
 import java.util.Map;
-import org.opendaylight.yangtools.concepts.Immutable;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import java.util.Optional;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.util.UnmodifiableCollection;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
@@ -25,8 +26,9 @@ import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableN
 
 public class ImmutableOrderedMapNodeBuilder implements CollectionNodeBuilder<MapEntryNode, OrderedMapNode> {
     private static final int DEFAULT_CAPACITY = 4;
-    private Map<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> value;
-    private YangInstanceIdentifier.NodeIdentifier nodeIdentifier;
+
+    private Map<NodeIdentifierWithPredicates, MapEntryNode> value;
+    private NodeIdentifier nodeIdentifier;
     private boolean dirty;
 
     protected ImmutableOrderedMapNodeBuilder() {
@@ -35,7 +37,11 @@ public class ImmutableOrderedMapNodeBuilder implements CollectionNodeBuilder<Map
     }
 
     protected ImmutableOrderedMapNodeBuilder(final int sizeHint) {
-        this.value = new LinkedHashMap<>(DEFAULT_CAPACITY);
+        if (sizeHint >= 0) {
+            this.value = new LinkedHashMap<>(sizeHint + sizeHint / 3);
+        } else {
+            this.value = new LinkedHashMap<>(DEFAULT_CAPACITY);
+        }
         this.dirty = false;
     }
 
@@ -45,15 +51,15 @@ public class ImmutableOrderedMapNodeBuilder implements CollectionNodeBuilder<Map
         this.dirty = true;
     }
 
-    public static CollectionNodeBuilder<MapEntryNode, OrderedMapNode> create() {
+    public static @NonNull CollectionNodeBuilder<MapEntryNode, OrderedMapNode> create() {
         return new ImmutableOrderedMapNodeBuilder();
     }
 
-    public static CollectionNodeBuilder<MapEntryNode, OrderedMapNode> create(final int sizeHint) {
+    public static @NonNull CollectionNodeBuilder<MapEntryNode, OrderedMapNode> create(final int sizeHint) {
         return new ImmutableOrderedMapNodeBuilder(sizeHint);
     }
 
-    public static CollectionNodeBuilder<MapEntryNode, OrderedMapNode> create(final MapNode node) {
+    public static @NonNull CollectionNodeBuilder<MapEntryNode, OrderedMapNode> create(final MapNode node) {
         if (!(node instanceof ImmutableOrderedMapNode)) {
             throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
         }
@@ -76,16 +82,16 @@ public class ImmutableOrderedMapNodeBuilder implements CollectionNodeBuilder<Map
     }
 
     @Override
-    public CollectionNodeBuilder<MapEntryNode, OrderedMapNode> withoutChild(final YangInstanceIdentifier.PathArgument key) {
+    public CollectionNodeBuilder<MapEntryNode, OrderedMapNode> withoutChild(final PathArgument key) {
         checkDirty();
         this.value.remove(key);
         return this;
     }
 
     @Override
-    public CollectionNodeBuilder<MapEntryNode, OrderedMapNode> withValue(final Collection<MapEntryNode> value) {
+    public CollectionNodeBuilder<MapEntryNode, OrderedMapNode> withValue(final Collection<MapEntryNode> withValue) {
         // TODO replace or putAll ?
-        for (final MapEntryNode mapEntryNode : value) {
+        for (final MapEntryNode mapEntryNode : withValue) {
             withChild(mapEntryNode);
         }
 
@@ -93,8 +99,9 @@ public class ImmutableOrderedMapNodeBuilder implements CollectionNodeBuilder<Map
     }
 
     @Override
-    public CollectionNodeBuilder<MapEntryNode, OrderedMapNode> withNodeIdentifier(final YangInstanceIdentifier.NodeIdentifier nodeIdentifier) {
-        this.nodeIdentifier = nodeIdentifier;
+    public CollectionNodeBuilder<MapEntryNode, OrderedMapNode> withNodeIdentifier(
+            final NodeIdentifier withNodeIdentifier) {
+        this.nodeIdentifier = withNodeIdentifier;
         return this;
     }
 
@@ -117,19 +124,31 @@ public class ImmutableOrderedMapNodeBuilder implements CollectionNodeBuilder<Map
         return withoutChild(key);
     }
 
-    protected static final class ImmutableOrderedMapNode extends AbstractImmutableNormalizedNode<YangInstanceIdentifier.NodeIdentifier, Iterable<MapEntryNode>> implements Immutable,OrderedMapNode {
+    protected static final class ImmutableOrderedMapNode
+            extends AbstractImmutableNormalizedNode<NodeIdentifier, Collection<MapEntryNode>>
+            implements OrderedMapNode {
 
-        private final Map<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> children;
+        private final Map<NodeIdentifierWithPredicates, MapEntryNode> children;
 
-        ImmutableOrderedMapNode(final YangInstanceIdentifier.NodeIdentifier nodeIdentifier,
-                         final Map<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> children) {
+        ImmutableOrderedMapNode(final NodeIdentifier nodeIdentifier,
+                         final Map<NodeIdentifierWithPredicates, MapEntryNode> children) {
             super(nodeIdentifier);
             this.children = children;
         }
 
         @Override
-        public Optional<MapEntryNode> getChild(final YangInstanceIdentifier.NodeIdentifierWithPredicates child) {
-            return Optional.fromNullable(children.get(child));
+        public Optional<MapEntryNode> getChild(final NodeIdentifierWithPredicates child) {
+            return Optional.ofNullable(children.get(child));
+        }
+
+        @Override
+        public MapEntryNode getChild(final int position) {
+            return Iterables.get(children.values(), position);
+        }
+
+        @Override
+        public int size() {
+            return children.size();
         }
 
         @Override
@@ -142,19 +161,14 @@ public class ImmutableOrderedMapNodeBuilder implements CollectionNodeBuilder<Map
             return children.equals(((ImmutableOrderedMapNode) other).children);
         }
 
-        @Override
-        public MapEntryNode getChild(final int position) {
-            return Iterables.get(children.values(), position);
-        }
-
         @Override
         public int getSize() {
             return children.size();
         }
 
         @Override
-        public Iterable<MapEntryNode> getValue() {
-            return Iterables.unmodifiableIterable(children.values());
+        public Collection<MapEntryNode> getValue() {
+            return UnmodifiableCollection.create(children.values());
         }
     }
 }