Split out yang-data-spi
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableUnkeyedListNodeBuilder.java
index 425162fca6b239c81bc2b73338623d4eabadc11e..e6ae5a9ea438d97f06c8843694b3ef9467c619ce 100644 (file)
@@ -9,23 +9,22 @@ 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.Collections;
+import java.util.Collection;
 import java.util.LinkedList;
 import java.util.List;
-import org.opendaylight.yangtools.concepts.Immutable;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.eclipse.jdt.annotation.NonNull;
 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;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
-import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedValueNode;
+import org.opendaylight.yangtools.yang.data.spi.node.AbstractNormalizedNode;
 
 public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> {
     private List<UnkeyedListEntryNode> value;
-    private YangInstanceIdentifier.NodeIdentifier nodeIdentifier;
+    private NodeIdentifier nodeIdentifier;
     private boolean dirty;
 
     protected ImmutableUnkeyedListNodeBuilder() {
@@ -35,8 +34,9 @@ public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<Un
 
     protected ImmutableUnkeyedListNodeBuilder(final ImmutableUnkeyedListNode node) {
         this.nodeIdentifier = node.getIdentifier();
+        // FIXME: clean this up, notably reuse unmodified lists
         this.value = new LinkedList<>();
-        Iterables.addAll(value, node.getValue());
+        Iterables.addAll(value, node.body());
         this.dirty = true;
     }
 
@@ -48,7 +48,8 @@ public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<Un
         return new ImmutableUnkeyedListNodeBuilder();
     }
 
-    public static CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> create(final UnkeyedListNode node) {
+    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()));
         }
@@ -72,16 +73,17 @@ public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<Un
 
     @Override
     public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withoutChild(
-            final YangInstanceIdentifier.PathArgument key) {
+            final 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> withValue) {
         // TODO replace or putAll ?
-        for (final UnkeyedListEntryNode UnkeyedListEntryNode : value) {
-            withChild(UnkeyedListEntryNode);
+        for (final UnkeyedListEntryNode node : withValue) {
+            withChild(node);
         }
 
         return this;
@@ -89,8 +91,8 @@ public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<Un
 
     @Override
     public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withNodeIdentifier(
-            final YangInstanceIdentifier.NodeIdentifier nodeIdentifier) {
-        this.nodeIdentifier = nodeIdentifier;
+            final NodeIdentifier withNodeIdentifier) {
+        this.nodeIdentifier = withNodeIdentifier;
         return this;
     }
 
@@ -99,9 +101,8 @@ public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<Un
         dirty = true;
         if (value.isEmpty()) {
             return new EmptyImmutableUnkeyedListNode(nodeIdentifier);
-        } else {
-            return new ImmutableUnkeyedListNode(nodeIdentifier, ImmutableList.copyOf(value));
         }
+        return new ImmutableUnkeyedListNode(nodeIdentifier, ImmutableList.copyOf(value));
     }
 
     @Override
@@ -110,19 +111,20 @@ public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<Un
     }
 
     @Override
-    public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, UnkeyedListEntryNode, UnkeyedListNode> removeChild(
-            final PathArgument key) {
+    public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, UnkeyedListEntryNode, UnkeyedListNode>
+            removeChild(final PathArgument key) {
         return withoutChild(key);
     }
 
-    protected static final class EmptyImmutableUnkeyedListNode extends AbstractImmutableNormalizedNode<YangInstanceIdentifier.NodeIdentifier, Iterable<UnkeyedListEntryNode>> implements Immutable, UnkeyedListNode {
+    protected static final class EmptyImmutableUnkeyedListNode
+            extends AbstractNormalizedNode<NodeIdentifier, UnkeyedListNode> implements UnkeyedListNode {
         protected EmptyImmutableUnkeyedListNode(final NodeIdentifier nodeIdentifier) {
             super(nodeIdentifier);
         }
 
         @Override
-        public Iterable<UnkeyedListEntryNode> getValue() {
-            return Collections.emptyList();
+        public ImmutableList<UnkeyedListEntryNode> body() {
+            return ImmutableList.of();
         }
 
         @Override
@@ -131,51 +133,69 @@ public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<Un
         }
 
         @Override
-        public int getSize() {
+        public int size() {
             return 0;
         }
 
         @Override
-        protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
-            return Collections.EMPTY_LIST.equals(other.getValue());
+        protected Class<UnkeyedListNode> implementedType() {
+            return UnkeyedListNode.class;
         }
 
         @Override
         protected int valueHashCode() {
-            return Collections.EMPTY_LIST.hashCode();
+            return 1;
+        }
+
+        @Override
+        protected boolean valueEquals(final UnkeyedListNode other) {
+            return other.isEmpty();
         }
     }
 
-    protected static final class ImmutableUnkeyedListNode extends
-            AbstractImmutableNormalizedValueNode<YangInstanceIdentifier.NodeIdentifier, Iterable<UnkeyedListEntryNode>>
-            implements Immutable, UnkeyedListNode {
+    protected static final class ImmutableUnkeyedListNode
+            extends AbstractImmutableNormalizedValueNode<NodeIdentifier, UnkeyedListNode,
+                Collection<@NonNull UnkeyedListEntryNode>>
+            implements UnkeyedListNode {
 
         private final ImmutableList<UnkeyedListEntryNode> children;
 
-        ImmutableUnkeyedListNode(final YangInstanceIdentifier.NodeIdentifier nodeIdentifier,
+        ImmutableUnkeyedListNode(final NodeIdentifier nodeIdentifier,
                 final ImmutableList<UnkeyedListEntryNode> children) {
             super(nodeIdentifier, children);
             this.children = children;
         }
 
         @Override
-        protected int valueHashCode() {
-            return children.hashCode();
+        public UnkeyedListEntryNode getChild(final int position) {
+            return children.get(position);
         }
 
         @Override
-        protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
-            return children.equals(((ImmutableUnkeyedListNode) other).children);
+        public int size() {
+            return children.size();
         }
 
         @Override
-        public UnkeyedListEntryNode getChild(final int position) {
-            return children.get(position);
+        protected Class<UnkeyedListNode> implementedType() {
+            return UnkeyedListNode.class;
         }
 
         @Override
-        public int getSize() {
-            return children.size();
+        protected int valueHashCode() {
+            return children.hashCode();
+        }
+
+        @Override
+        protected boolean valueEquals(final UnkeyedListNode other) {
+            final Collection<UnkeyedListEntryNode> otherChildren;
+            if (other instanceof ImmutableUnkeyedListNode) {
+                otherChildren = ((ImmutableUnkeyedListNode) other).children;
+            } else {
+                otherChildren = other.body();
+            }
+            return otherChildren instanceof List ? children.equals(otherChildren)
+                : Iterables.elementsEqual(children, otherChildren);
         }
     }
 }