Add NormalizedNodeContainer.size()
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableLeafSetNodeBuilder.java
index 82e8dabafc97b91e7baa6965b2ba5f9953e3f235..0edc0b9f2573c65b6b9db07dacc2f97a737596d4 100644 (file)
@@ -7,17 +7,16 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
 
-import com.google.common.base.Optional;
+import com.google.common.collect.Maps;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
-import org.opendaylight.yangtools.concepts.Immutable;
+import java.util.Optional;
+import org.eclipse.jdt.annotation.NonNull;
 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.YangInstanceIdentifier;
 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;
@@ -28,15 +27,20 @@ import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableN
 
 public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
     private static final int DEFAULT_CAPACITY = 4;
-    private final Map<YangInstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> value;
-    private YangInstanceIdentifier.NodeIdentifier nodeIdentifier;
+
+    private final Map<NodeWithValue, LeafSetEntryNode<T>> value;
+    private NodeIdentifier nodeIdentifier;
 
     protected ImmutableLeafSetNodeBuilder() {
         value = new HashMap<>(DEFAULT_CAPACITY);
     }
 
     protected ImmutableLeafSetNodeBuilder(final int sizeHint) {
-        value = new HashMap<>(sizeHint * 4 / 3);
+        if (sizeHint >= 0) {
+            value = Maps.newHashMapWithExpectedSize(sizeHint);
+        } else {
+            value = new HashMap<>(DEFAULT_CAPACITY);
+        }
     }
 
     protected ImmutableLeafSetNodeBuilder(final ImmutableLeafSetNode<T> node) {
@@ -44,20 +48,20 @@ public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSe
         value = MapAdaptor.getDefaultInstance().takeSnapshot(node.children);
     }
 
-    public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
+    public static <T> @NonNull ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
         return new ImmutableLeafSetNodeBuilder<>();
     }
 
-    public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create(final int sizeHint) {
+    public static <T> @NonNull ListNodeBuilder<T, LeafSetEntryNode<T>> create(final int sizeHint) {
         return new ImmutableLeafSetNodeBuilder<>(sizeHint);
     }
 
-    public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafSetNode<T> node) {
+    public static <T> @NonNull ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafSetNode<T> node) {
         if (!(node instanceof ImmutableLeafSetNode<?>)) {
             throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
         }
 
-        return new ImmutableLeafSetNodeBuilder<T>((ImmutableLeafSetNode<T>) node);
+        return new ImmutableLeafSetNodeBuilder<>((ImmutableLeafSetNode<T>) node);
     }
 
     @Override
@@ -78,50 +82,46 @@ public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSe
     }
 
     @Override
-    public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(
-            final YangInstanceIdentifier.NodeIdentifier nodeIdentifier) {
-        this.nodeIdentifier = nodeIdentifier;
+    public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
+        this.nodeIdentifier = withNodeIdentifier;
         return this;
     }
 
     @Override
-    public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final Collection<LeafSetEntryNode<T>> value) {
-        for (final LeafSetEntryNode<T> leafSetEntry : value) {
+    public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final Collection<LeafSetEntryNode<T>> withValue) {
+        for (final LeafSetEntryNode<T> leafSetEntry : withValue) {
             withChild(leafSetEntry);
         }
         return this;
     }
 
-
     @Override
-    public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value, final Map<QName, String> attributes) {
-        final ImmutableLeafSetEntryNodeBuilder<T> b = ImmutableLeafSetEntryNodeBuilder.create();
-        b.withNodeIdentifier(new YangInstanceIdentifier.NodeWithValue(nodeIdentifier.getNodeType(), value));
-        b.withValue(value);
-        b.withAttributes(attributes);
-        return withChild(b.build());
+    public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T childValue) {
+        return withChild(ImmutableLeafSetEntryNodeBuilder.<T>create()
+            .withNodeIdentifier(new NodeWithValue<>(nodeIdentifier.getNodeType(), childValue))
+            .withValue(childValue).build());
     }
 
-    @Override
-    public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value) {
-        return withChildValue(value, Collections.<QName,String>emptyMap());
-    }
+    protected static final class ImmutableLeafSetNode<T> extends
+            AbstractImmutableNormalizedValueNode<NodeIdentifier, Collection<LeafSetEntryNode<T>>> implements
+            LeafSetNode<T> {
 
-    protected final static class ImmutableLeafSetNode<T> extends
-            AbstractImmutableNormalizedValueNode<YangInstanceIdentifier.NodeIdentifier, Collection<LeafSetEntryNode<T>>> implements
-            Immutable, LeafSetNode<T> {
+        private final Map<NodeWithValue, LeafSetEntryNode<T>> children;
 
-        private final Map<YangInstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children;
-
-        ImmutableLeafSetNode(final YangInstanceIdentifier.NodeIdentifier nodeIdentifier,
-                final Map<YangInstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children) {
+        ImmutableLeafSetNode(final NodeIdentifier nodeIdentifier,
+                final Map<NodeWithValue, LeafSetEntryNode<T>> children) {
             super(nodeIdentifier, UnmodifiableCollection.create(children.values()));
             this.children = children;
         }
 
         @Override
-        public Optional<LeafSetEntryNode<T>> getChild(final YangInstanceIdentifier.NodeWithValue child) {
-            return Optional.fromNullable(children.get(child));
+        public Optional<LeafSetEntryNode<T>> getChild(final NodeWithValue child) {
+            return Optional.ofNullable(children.get(child));
+        }
+
+        @Override
+        public int size() {
+            return children.size();
         }
 
         @Override
@@ -142,8 +142,8 @@ public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSe
     }
 
     @Override
-    public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> removeChild(
-            final PathArgument key) {
+    public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>>
+            removeChild(final PathArgument key) {
         return withoutChild(key);
     }