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%2FImmutableUnkeyedListNodeBuilder.java;h=e6ae5a9ea438d97f06c8843694b3ef9467c619ce;hb=8ebb6a36888dfdaff979fbf6e8bc8e75a543303e;hp=498b65824599fcf0c48ea91d6f9544a357402c70;hpb=a243f452faae458db1b5b19d77c61981c5e38c9c;p=yangtools.git diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/ImmutableUnkeyedListNodeBuilder.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/ImmutableUnkeyedListNodeBuilder.java index 498b658245..e6ae5a9ea4 100644 --- a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/ImmutableUnkeyedListNodeBuilder.java +++ b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/ImmutableUnkeyedListNodeBuilder.java @@ -7,27 +7,25 @@ */ 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.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.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 com.google.common.collect.ImmutableList; -import com.google.common.collect.Iterables; +import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedValueNode; +import org.opendaylight.yangtools.yang.data.spi.node.AbstractNormalizedNode; public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder { - private List value; - private InstanceIdentifier.NodeIdentifier nodeIdentifier; - private boolean dirty = false; + private NodeIdentifier nodeIdentifier; + private boolean dirty; protected ImmutableUnkeyedListNodeBuilder() { this.value = new LinkedList<>(); @@ -36,8 +34,9 @@ public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder(); - Iterables.addAll(value, node.getValue()); + Iterables.addAll(value, node.body()); this.dirty = true; } @@ -45,7 +44,12 @@ public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder create(final UnkeyedListNode node) { + public static CollectionNodeBuilder create(final int sizeHint) { + return new ImmutableUnkeyedListNodeBuilder(); + } + + public static CollectionNodeBuilder create( + final UnkeyedListNode node) { if (!(node instanceof ImmutableUnkeyedListNode)) { throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass())); } @@ -69,16 +73,17 @@ public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder withoutChild( - final InstanceIdentifier.PathArgument key) { + final PathArgument key) { checkDirty(); throw new UnsupportedOperationException("Children does not have identifiers."); } @Override - public CollectionNodeBuilder withValue(final List value) { + public CollectionNodeBuilder withValue( + final Collection withValue) { // TODO replace or putAll ? - for (final UnkeyedListEntryNode UnkeyedListEntryNode : value) { - withChild(UnkeyedListEntryNode); + for (final UnkeyedListEntryNode node : withValue) { + withChild(node); } return this; @@ -86,14 +91,17 @@ public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder withNodeIdentifier( - final InstanceIdentifier.NodeIdentifier nodeIdentifier) { - this.nodeIdentifier = nodeIdentifier; + final NodeIdentifier withNodeIdentifier) { + this.nodeIdentifier = withNodeIdentifier; return this; } @Override public UnkeyedListNode build() { dirty = true; + if (value.isEmpty()) { + return new EmptyImmutableUnkeyedListNode(nodeIdentifier); + } return new ImmutableUnkeyedListNode(nodeIdentifier, ImmutableList.copyOf(value)); } @@ -103,31 +111,59 @@ public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder removeChild( - final PathArgument key) { + public NormalizedNodeContainerBuilder + removeChild(final PathArgument key) { return withoutChild(key); } - protected static final class ImmutableUnkeyedListNode extends - AbstractImmutableNormalizedNode> - implements Immutable, UnkeyedListNode { + protected static final class EmptyImmutableUnkeyedListNode + extends AbstractNormalizedNode implements UnkeyedListNode { + protected EmptyImmutableUnkeyedListNode(final NodeIdentifier nodeIdentifier) { + super(nodeIdentifier); + } - private final ImmutableList children; + @Override + public ImmutableList body() { + return ImmutableList.of(); + } - ImmutableUnkeyedListNode(final InstanceIdentifier.NodeIdentifier nodeIdentifier, - final ImmutableList children) { - super(nodeIdentifier, children); - this.children = children; + @Override + public UnkeyedListEntryNode getChild(final int position) { + throw new IndexOutOfBoundsException(); + } + + @Override + public int size() { + return 0; + } + + @Override + protected Class implementedType() { + return UnkeyedListNode.class; } @Override protected int valueHashCode() { - return children.hashCode(); + return 1; } @Override - protected boolean valueEquals(final AbstractImmutableNormalizedNode other) { - return children.equals(((ImmutableUnkeyedListNode) other).children); + protected boolean valueEquals(final UnkeyedListNode other) { + return other.isEmpty(); + } + } + + protected static final class ImmutableUnkeyedListNode + extends AbstractImmutableNormalizedValueNode> + implements UnkeyedListNode { + + private final ImmutableList children; + + ImmutableUnkeyedListNode(final NodeIdentifier nodeIdentifier, + final ImmutableList children) { + super(nodeIdentifier, children); + this.children = children; } @Override @@ -136,8 +172,30 @@ public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder implementedType() { + return UnkeyedListNode.class; + } + + @Override + protected int valueHashCode() { + return children.hashCode(); + } + + @Override + protected boolean valueEquals(final UnkeyedListNode other) { + final Collection otherChildren; + if (other instanceof ImmutableUnkeyedListNode) { + otherChildren = ((ImmutableUnkeyedListNode) other).children; + } else { + otherChildren = other.body(); + } + return otherChildren instanceof List ? children.equals(otherChildren) + : Iterables.elementsEqual(children, otherChildren); + } } }