Improve type safety of AbstractImmutableDataContainerNode 50/106050/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 18 May 2023 19:49:03 +0000 (21:49 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 18 May 2023 20:02:29 +0000 (22:02 +0200)
Now that DataContainerNode's children are only keyed by NodeIdentifier,
we can improve type safety of our maps -- removing the need for a
verify.

JIRA: YANGTOOLS-1511
Change-Id: I9ea336c52f78c1ac8d73f9bee104bffc13d7d317
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/AbstractImmutableDataContainerNodeBuilder.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/ImmutableChoiceNodeBuilder.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/ImmutableContainerNodeBuilder.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/ImmutableMapEntryNodeBuilder.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/ImmutableUnkeyedListEntryNodeBuilder.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/nodes/AbstractImmutableDataContainerNode.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/nodes/LazyLeafOperations.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/nodes/LazyValues.java

index efc559ff648cf558b710826755ee5582de7c6a07..057dcea5bf9f449fd85156831872e56018753f2d 100644 (file)
@@ -15,6 +15,7 @@ import java.util.Map;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.util.ModifiableMapPhase;
 import org.opendaylight.yangtools.util.UnmodifiableMapPhase;
+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.DataContainerChild;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
@@ -43,7 +44,7 @@ abstract class AbstractImmutableDataContainerNodeBuilder<I extends PathArgument,
         }
     }
 
-    private Map<PathArgument, Object> value;
+    private Map<NodeIdentifier, Object> value;
     private I nodeIdentifier;
 
     /*
@@ -86,13 +87,13 @@ abstract class AbstractImmutableDataContainerNodeBuilder<I extends PathArgument,
         return nodeIdentifier;
     }
 
-    protected final @Nullable DataContainerChild getChild(final PathArgument child) {
+    protected final @Nullable DataContainerChild getChild(final NodeIdentifier child) {
         return LazyLeafOperations.getChild(value, child);
     }
 
-    protected final Map<PathArgument, Object> buildValue() {
+    protected final Map<NodeIdentifier, Object> buildValue() {
         if (value instanceof ModifiableMapPhase) {
-            return ((ModifiableMapPhase<PathArgument, Object>)value).toUnmodifiableMap();
+            return ((ModifiableMapPhase<NodeIdentifier, Object>)value).toUnmodifiableMap();
         }
 
         dirty = true;
@@ -102,9 +103,9 @@ abstract class AbstractImmutableDataContainerNodeBuilder<I extends PathArgument,
     private void checkDirty() {
         if (dirty) {
             if (value instanceof UnmodifiableMapPhase) {
-                value = ((UnmodifiableMapPhase<PathArgument, Object>) value).toModifiableMap();
+                value = ((UnmodifiableMapPhase<NodeIdentifier, Object>) value).toModifiableMap();
             } else if (value instanceof CloneableMap) {
-                value = ((CloneableMap<PathArgument, Object>) value).createMutableClone();
+                value = ((CloneableMap<NodeIdentifier, Object>) value).createMutableClone();
             } else {
                 value = newHashMap(value);
             }
index 445191fc2420f0b20241095438a262c5905fa0ff..7de8f15c4db7f9365845240cb1e32abe49052206 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
 import java.util.Map;
 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.ChoiceNode;
 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableDataContainerNode;
@@ -50,7 +49,7 @@ public class ImmutableChoiceNodeBuilder extends AbstractImmutableDataContainerNo
 
     private static final class ImmutableChoiceNode
             extends AbstractImmutableDataContainerNode<NodeIdentifier, ChoiceNode> implements ChoiceNode {
-        ImmutableChoiceNode(final NodeIdentifier nodeIdentifier, final Map<PathArgument, Object> children) {
+        ImmutableChoiceNode(final NodeIdentifier nodeIdentifier, final Map<NodeIdentifier, Object> children) {
             super(children, nodeIdentifier);
         }
 
index e62e81e80db74767dafbce99ebf3ad0ed8b4a9dc..5f4ea979d196f7f038592961f58b59f89672452e 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
 import java.util.Map;
 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.ContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableDataContainerNode;
@@ -51,7 +50,7 @@ public class ImmutableContainerNodeBuilder
 
     protected static final class ImmutableContainerNode
             extends AbstractImmutableDataContainerNode<NodeIdentifier, ContainerNode> implements ContainerNode {
-        ImmutableContainerNode(final NodeIdentifier nodeIdentifier, final Map<PathArgument, Object> children) {
+        ImmutableContainerNode(final NodeIdentifier nodeIdentifier, final Map<NodeIdentifier, Object> children) {
             super(children, nodeIdentifier);
         }
 
index d529ce81a4e3cc9426692a934078273962bd5ea7..1219132739121de8dfd04ea4085532626971a034 100644 (file)
@@ -10,13 +10,11 @@ package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
 import java.util.Collection;
 import java.util.LinkedHashMap;
 import java.util.Map;
-import java.util.Map.Entry;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.QName;
+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.DataContainerChild;
-import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
@@ -28,8 +26,8 @@ import org.slf4j.LoggerFactory;
 public class ImmutableMapEntryNodeBuilder
         extends AbstractImmutableDataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> {
     private static final Logger LOG = LoggerFactory.getLogger(ImmutableMapEntryNodeBuilder.class);
-    // FIXME: NodeIdentifier instead
-    protected final Map<QName, PathArgument> childrenQNamesToPaths;
+
+    protected final Map<QName, NodeIdentifier> childrenQNamesToPaths;
 
     protected ImmutableMapEntryNodeBuilder() {
         childrenQNamesToPaths = new LinkedHashMap<>();
@@ -63,14 +61,13 @@ public class ImmutableMapEntryNodeBuilder
         return new ImmutableMapEntryNodeBuilder(immutableNode);
     }
 
-    private static void fillQNames(final Iterable<DataContainerChild> iterable, final Map<QName, PathArgument> out) {
-        for (final DataContainerChild child : iterable) {
+    private static void fillQNames(final Iterable<DataContainerChild> iterable, final Map<QName, NodeIdentifier> out) {
+        for (var child : iterable) {
             putQName(out, child);
         }
     }
 
-    private static void putQName(final Map<QName, PathArgument> map, final DataContainerChild child) {
-        // Augmentation nodes cannot be keys, and do not have to be present in childrenQNamesToPaths map
+    private static void putQName(final Map<QName, NodeIdentifier> map, final DataContainerChild child) {
         final var identifier = child.name();
         map.put(identifier.getNodeType(), identifier);
     }
@@ -91,12 +88,12 @@ public class ImmutableMapEntryNodeBuilder
 
     @Override
     public MapEntryNode build() {
-        for (final Entry<QName, Object> key : getNodeIdentifier().entrySet()) {
-            final DataContainerChild childNode = getChild(childrenQNamesToPaths.get(key.getKey()));
+        for (var key : getNodeIdentifier().entrySet()) {
+            final var childNode = getChild(childrenQNamesToPaths.get(key.getKey()));
 
             // We have enough information to fill-in missing leaf nodes, so let's do that
             if (childNode == null) {
-                LeafNode<Object> leaf = ImmutableNodes.leafNode(key.getKey(), key.getValue());
+                final var leaf = ImmutableNodes.leafNode(key.getKey(), key.getValue());
                 LOG.debug("Adding leaf {} implied by key {}", leaf, key);
                 withChild(leaf);
             } else {
@@ -113,7 +110,7 @@ public class ImmutableMapEntryNodeBuilder
             implements MapEntryNode {
 
         ImmutableMapEntryNode(final NodeIdentifierWithPredicates nodeIdentifier,
-                final Map<PathArgument, Object> children) {
+                final Map<NodeIdentifier, Object> children) {
             super(children, nodeIdentifier);
         }
 
index e15d08bf6920263cc9479f8e32ff32791954cab9..1a7b311ec8947bfa73ab5d2a5674275b7052ec4f 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
 import java.util.Map;
 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.builder.DataContainerNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableDataContainerNode;
@@ -55,7 +54,7 @@ public class ImmutableUnkeyedListEntryNodeBuilder
     protected static final class ImmutableUnkeyedListEntryNode
             extends AbstractImmutableDataContainerNode<NodeIdentifier, UnkeyedListEntryNode>
             implements UnkeyedListEntryNode {
-        ImmutableUnkeyedListEntryNode(final NodeIdentifier nodeIdentifier, final Map<PathArgument, Object> children) {
+        ImmutableUnkeyedListEntryNode(final NodeIdentifier nodeIdentifier, final Map<NodeIdentifier, Object> children) {
             super(children, nodeIdentifier);
         }
 
index cb393c8b7a83df64f40cddb3bc8d345e43c9a45f..08892e449b350d6c488237a475464061bff15a24 100644 (file)
@@ -18,9 +18,9 @@ import org.opendaylight.yangtools.yang.data.spi.node.AbstractNormalizedNode;
 
 public abstract class AbstractImmutableDataContainerNode<K extends PathArgument, N extends DataContainerNode>
         extends AbstractNormalizedNode<K, N> implements DataContainerNode {
-    private final Map<PathArgument, Object> children;
+    private final Map<NodeIdentifier, Object> children;
 
-    protected AbstractImmutableDataContainerNode(final Map<PathArgument, Object> children, final K nodeIdentifier) {
+    protected AbstractImmutableDataContainerNode(final Map<NodeIdentifier, Object> children, final K nodeIdentifier) {
         super(nodeIdentifier);
         this.children = ImmutableOffsetMap.unorderedCopyOf(children);
     }
@@ -49,7 +49,7 @@ public abstract class AbstractImmutableDataContainerNode<K extends PathArgument,
      *
      * @return An unmodifiable view if this node's children.
      */
-    public final Map<PathArgument, Object> getChildren() {
+    public final Map<NodeIdentifier, Object> getChildren() {
         return children;
     }
 
index ef3ca40c5f80da1707701833c940b79f37b56836..b4c6403538ee9ba99a3fe306394731a79871c0ec 100644 (file)
@@ -15,7 +15,6 @@ import java.util.Map;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 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.DataContainerChild;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
@@ -30,26 +29,26 @@ public final class LazyLeafOperations {
         // Hidden on purpose
     }
 
-    public static @Nullable DataContainerChild getChild(final Map<PathArgument, Object> map, final PathArgument key) {
+    public static @Nullable DataContainerChild getChild(final Map<NodeIdentifier, Object> map,
+            final NodeIdentifier key) {
         final Object value = map.get(key);
         return value == null ? null : decodeChild(key, value);
     }
 
-    public static void putChild(final Map<PathArgument, Object> map, final DataContainerChild child) {
+    public static void putChild(final Map<NodeIdentifier, Object> map, final DataContainerChild child) {
         final var node = requireNonNull(child);
         map.put(node.name(), encodeExpendableChild(node));
     }
 
-    static @NonNull LeafNode<?> coerceLeaf(final PathArgument key, final Object value) {
-        verify(key instanceof NodeIdentifier, "Unexpected value %s for child %s", value, key);
-        return ImmutableNodes.leafNode((NodeIdentifier) key, value);
+    static @NonNull LeafNode<?> coerceLeaf(final NodeIdentifier key, final Object value) {
+        return ImmutableNodes.leafNode(key, value);
     }
 
-    private static @Nullable DataContainerChild decodeChild(final PathArgument key, final @NonNull Object value) {
+    private static @Nullable DataContainerChild decodeChild(final NodeIdentifier key, final @NonNull Object value) {
         return decodeExpendableChild(key, value);
     }
 
-    private static @NonNull DataContainerChild decodeExpendableChild(final PathArgument key,
+    private static @NonNull DataContainerChild decodeExpendableChild(final NodeIdentifier key,
             final @NonNull Object value) {
         return value instanceof DataContainerChild child ? child : coerceLeaf(key, value);
     }
index 60ca848087351d659ea528c56e6d34d11c856006..502041b9d004086e9b0287bf481f0695fb3bb5e8 100644 (file)
@@ -13,7 +13,7 @@ import java.util.AbstractCollection;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Map.Entry;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
 
 // This is *almost* the same as Guava's TransformedCollection. The main difference is delegation of hashCode()/equals()
@@ -22,9 +22,9 @@ import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
 // Collection.equals() is undefined, but the expectation from users is that we will return the same view object, which
 // equals on identity.
 final class LazyValues extends AbstractCollection<DataContainerChild> {
-    private final Map<PathArgument, Object> map;
+    private final Map<NodeIdentifier, Object> map;
 
-    LazyValues(final Map<PathArgument, Object> map) {
+    LazyValues(final Map<NodeIdentifier, Object> map) {
         this.map = requireNonNull(map);
     }
 
@@ -54,9 +54,9 @@ final class LazyValues extends AbstractCollection<DataContainerChild> {
     }
 
     private static final class Iter implements Iterator<DataContainerChild> {
-        private final Iterator<Entry<PathArgument, Object>> iterator;
+        private final Iterator<Entry<NodeIdentifier, Object>> iterator;
 
-        Iter(final Iterator<Entry<PathArgument, Object>> iterator) {
+        Iter(final Iterator<Entry<NodeIdentifier, Object>> iterator) {
             this.iterator = requireNonNull(iterator);
         }