Make sure NormalizedNodeContainer uses Collection
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableUnkeyedListNodeBuilder.java
index 274b781d3e66fded64bdf5485dada1f47561c20f..eb2d3884bbc4dfda6a0e8f2a4a8e9eb39ae73a81 100644 (file)
@@ -7,13 +7,16 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
 
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
-
 import org.opendaylight.yangtools.concepts.Immutable;
-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;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
@@ -21,14 +24,10 @@ import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNo
 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedValueNode;
 
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Iterables;
-
 public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> {
-
     private List<UnkeyedListEntryNode> value;
-    private InstanceIdentifier.NodeIdentifier nodeIdentifier;
-    private boolean dirty = false;
+    private YangInstanceIdentifier.NodeIdentifier nodeIdentifier;
+    private boolean dirty;
 
     protected ImmutableUnkeyedListNodeBuilder() {
         this.value = new LinkedList<>();
@@ -46,6 +45,10 @@ public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<Un
         return new ImmutableUnkeyedListNodeBuilder();
     }
 
+    public static CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> create(final int sizeHint) {
+        return new ImmutableUnkeyedListNodeBuilder();
+    }
+
     public static CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> create(final UnkeyedListNode node) {
         if (!(node instanceof ImmutableUnkeyedListNode)) {
             throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
@@ -70,13 +73,13 @@ public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<Un
 
     @Override
     public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withoutChild(
-            final InstanceIdentifier.PathArgument key) {
+            final YangInstanceIdentifier.PathArgument key) {
         checkDirty();
         throw new UnsupportedOperationException("Children does not have identifiers.");
     }
 
     @Override
-    public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withValue(final List<UnkeyedListEntryNode> value) {
+    public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withValue(final Collection<UnkeyedListEntryNode> value) {
         // TODO replace or putAll ?
         for (final UnkeyedListEntryNode UnkeyedListEntryNode : value) {
             withChild(UnkeyedListEntryNode);
@@ -87,7 +90,7 @@ public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<Un
 
     @Override
     public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withNodeIdentifier(
-            final InstanceIdentifier.NodeIdentifier nodeIdentifier) {
+            final YangInstanceIdentifier.NodeIdentifier nodeIdentifier) {
         this.nodeIdentifier = nodeIdentifier;
         return this;
     }
@@ -95,7 +98,11 @@ public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<Un
     @Override
     public UnkeyedListNode build() {
         dirty = true;
-        return new ImmutableUnkeyedListNode(nodeIdentifier, ImmutableList.copyOf(value));
+        if (value.isEmpty()) {
+            return new EmptyImmutableUnkeyedListNode(nodeIdentifier);
+        } else {
+            return new ImmutableUnkeyedListNode(nodeIdentifier, ImmutableList.copyOf(value));
+        }
     }
 
     @Override
@@ -109,13 +116,44 @@ public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<Un
         return withoutChild(key);
     }
 
+    protected static final class EmptyImmutableUnkeyedListNode extends AbstractImmutableNormalizedNode<YangInstanceIdentifier.NodeIdentifier, Collection<UnkeyedListEntryNode>> implements Immutable, UnkeyedListNode {
+        protected EmptyImmutableUnkeyedListNode(final NodeIdentifier nodeIdentifier) {
+            super(nodeIdentifier);
+        }
+
+        @Override
+        public Collection<UnkeyedListEntryNode> getValue() {
+            return Collections.emptySet();
+        }
+
+        @Override
+        public UnkeyedListEntryNode getChild(final int position) {
+            throw new IndexOutOfBoundsException();
+        }
+
+        @Override
+        public int getSize() {
+            return 0;
+        }
+
+        @Override
+        protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
+            return Collections.EMPTY_LIST.equals(other.getValue());
+        }
+
+        @Override
+        protected int valueHashCode() {
+            return Collections.EMPTY_LIST.hashCode();
+        }
+    }
+
     protected static final class ImmutableUnkeyedListNode extends
-            AbstractImmutableNormalizedValueNode<InstanceIdentifier.NodeIdentifier, Iterable<UnkeyedListEntryNode>>
+            AbstractImmutableNormalizedValueNode<YangInstanceIdentifier.NodeIdentifier, Collection<UnkeyedListEntryNode>>
             implements Immutable, UnkeyedListNode {
 
         private final ImmutableList<UnkeyedListEntryNode> children;
 
-        ImmutableUnkeyedListNode(final InstanceIdentifier.NodeIdentifier nodeIdentifier,
+        ImmutableUnkeyedListNode(final YangInstanceIdentifier.NodeIdentifier nodeIdentifier,
                 final ImmutableList<UnkeyedListEntryNode> children) {
             super(nodeIdentifier, children);
             this.children = children;