BUG-865: Remove TreeNodeFactory.createTreeNodeRecursively() 62/32862/15
authorRobert Varga <robert.varga@pantheon.sk>
Sat, 16 Jan 2016 01:12:16 +0000 (02:12 +0100)
committerRobert Varga <nite@hq.sk>
Wed, 18 Oct 2017 09:50:56 +0000 (09:50 +0000)
Lazy equivalents offer lower memory footprint, remove the recursive
one.

Change-Id: Ic1646d54826c36e30e59bacd9bc67afbe056cdc6
Signed-off-by: Robert Varga <robert.varga@pantheon.sk>
yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/tree/spi/TreeNodeFactory.java
yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/ModificationMetadataTreeTest.java
yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/StoreTreeNodesTest.java

index 29b1b37c82e92bb9fd5f07e8a93578f272370555..7bae55c2c32889e7d96525ad5d59abc03d456571 100644 (file)
@@ -7,9 +7,6 @@
  */
 package org.opendaylight.yangtools.yang.data.api.schema.tree.spi;
 
-import java.util.HashMap;
-import java.util.Map;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
 import org.opendaylight.yangtools.yang.data.api.schema.OrderedNodeContainer;
@@ -23,102 +20,6 @@ public final class TreeNodeFactory {
         throw new UnsupportedOperationException("Utility class should not be instantiated");
     }
 
-    /**
-     * Method creates and returns Container root Node and whole subtree for each child node specified in children nodes.
-     * <br>
-     * Reason why is method used recursively is that for each child in children nodes there is call to
-     * {@link TreeNodeFactory#createTreeNodeRecursively}. Each call to <code>createTreeNodeRecursively</code>
-     * calls either {@link #createNormalizedNodeRecursively} or {@link #createOrderedNodeRecursively}
-     * which depends on type of child node.
-     * <br> The root node that is returned holds reference to data node and whole subtree of children also containing
-     * references to data nodes.
-     *
-     * @param version version of indexed data
-     * @param data reference to data node
-     * @param children direct children of root node that is being created
-     * @return Root node with reference to data node and whole subtree of child nodes
-     */
-    @Deprecated
-    private static AbstractContainerNode createNodeRecursively(final Version version, final NormalizedNode<?, ?> data,
-        final Iterable<NormalizedNode<?, ?>> children) {
-
-        final Map<PathArgument, TreeNode> map = new HashMap<>();
-        for (NormalizedNode<?, ?> child : children) {
-            map.put(child.getIdentifier(), TreeNodeFactory.createTreeNodeRecursively(child, version));
-        }
-
-        return new MaterializedContainerNode(data, version, map, version);
-    }
-
-    /**
-     * Method creates and returns Normalized Node Container as root and recursively creates whole subtree
-     * from all of the container child iterables stored in
-     * {@link org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer#getValue()}
-     * <br>
-     * The reason why is this method called recursively is that in background method calls
-     * {@link TreeNodeFactory#createTreeNodeRecursively} for each child stored in NormalizedNode and after each child
-     * is created the method calls again {@link #createNormalizedNodeRecursively} method until all of the children are
-     * resolved.
-     *
-     * @param version version of indexed data
-     * @param container Normalized Node Container
-     * @return Normalized Node Container as root and all whole subtree created from container iterables.
-     *
-     */
-    @Deprecated
-    private static AbstractContainerNode createNormalizedNodeRecursively(final Version version,
-        final NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> container) {
-        return createNodeRecursively(version, container, container.getValue());
-    }
-
-    /**
-     * Method creates and returns Ordered Node Container as root and recursively creates whole subtree
-     * from all of the container child iterables stored in
-     * {@link org.opendaylight.yangtools.yang.data.api.schema.OrderedNodeContainer#getValue()}
-     * <br>
-     * The reason why is this method called recursively is that in background method calls
-     * {@link TreeNodeFactory#createTreeNodeRecursively} for each child stored in NormalizedNode and after each child
-     * is created the method calls again {@link #createNormalizedNodeRecursively} method until all of the children are
-     * resolved.
-     *
-     * @param version version of indexed data
-     * @param container Ordered Node Container
-     * @return Normalized Ordered Container as root and all whole subtree created from container iterables.
-     */
-    @Deprecated
-    private static AbstractContainerNode createOrderedNodeRecursively(final Version version,
-        final OrderedNodeContainer<NormalizedNode<?, ?>> container) {
-        return createNodeRecursively(version, container, container.getValue());
-    }
-
-    /**
-     * Create a new AbstractTreeNode from a data node, descending recursively as needed.
-     * This method should only ever be used for new data.
-     *
-     * @param data data node
-     * @param version data node version
-     * @return new AbstractTreeNode instance, covering the data tree provided
-     *
-     * @deprecated Use lazy node initialization via {@link #createTreeNode(NormalizedNode, Version)}.
-     */
-    @Deprecated
-    public static TreeNode createTreeNodeRecursively(final NormalizedNode<?, ?> data, final Version version) {
-        if (data instanceof NormalizedNodeContainer<?, ?, ?>) {
-            @SuppressWarnings("unchecked")
-            NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> container =
-                (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) data;
-            return createNormalizedNodeRecursively(version, container);
-
-        }
-        if (data instanceof OrderedNodeContainer<?>) {
-            @SuppressWarnings("unchecked")
-            OrderedNodeContainer<NormalizedNode<?, ?>> container = (OrderedNodeContainer<NormalizedNode<?, ?>>) data;
-            return createOrderedNodeRecursively(version, container);
-        }
-
-        return new ValueNode(data, version);
-    }
-
     /**
      * Create a new AbstractTreeNode from a data node.
      *
index fe7240182fb30901574df78ccb55470879567a83..3c75e42cf0626c650ec520bbfa38881d6dc224d3 100644 (file)
@@ -141,7 +141,7 @@ public class ModificationMetadataTreeTest {
     public void basicReadWrites() {
         final DataTreeModification modificationTree = new InMemoryDataTreeModification(
             new InMemoryDataTreeSnapshot(schemaContext,
-                TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper), rootOper);
+                TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper), rootOper);
         final Optional<NormalizedNode<?, ?>> originalBarNode = modificationTree.readNode(OUTER_LIST_2_PATH);
         assertTrue(originalBarNode.isPresent());
         assertSame(BAR_NODE, originalBarNode.get());
index 32df7e0420ed28b7e1f6a4d57853892192759612..4635ee64458a281304319d34faea331c0f85af14 100644 (file)
@@ -16,7 +16,7 @@ import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.ma
 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
 
-import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Optional;
 import org.junit.Before;
 import org.junit.Test;
@@ -84,27 +84,10 @@ public class StoreTreeNodesTest {
 
     }
 
-    private static ContainerNode createTestContainer() {
-        return ImmutableContainerNodeBuilder
-                .create()
-                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
-                .withChild(
-                        mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
-                        .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
-                        .withChild(BAR_NODE).build()).build();
-    }
-
-    private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
-        assertNotNull(potential);
-        assertTrue(potential.isPresent());
-        assertTrue(type.isInstance(potential.get()));
-        return type.cast(potential.get());
-    }
-
     @Test
     public void findNodeTestNodeFound() {
         final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
-                TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
+                TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
         final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
         final Optional<TreeNode> node = StoreTreeNodes.findNode(rootNode, OUTER_LIST_1_PATH);
         assertPresentAndType(node, TreeNode.class);
@@ -113,7 +96,7 @@ public class StoreTreeNodesTest {
     @Test
     public void findNodeTestNodeNotFound() {
         final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
-                TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
+                TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
         final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
         final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
                 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
@@ -125,7 +108,7 @@ public class StoreTreeNodesTest {
     @Test
     public void findNodeCheckedTestNodeFound() {
         final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
-                TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
+                TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
         final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
         TreeNode foundNode = null;
         try {
@@ -139,7 +122,7 @@ public class StoreTreeNodesTest {
     @Test
     public void findNodeCheckedTestNodeNotFound() {
         final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
-                TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
+                TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
         final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
         final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
                 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
@@ -155,38 +138,39 @@ public class StoreTreeNodesTest {
     @Test
     public void findClosestOrFirstMatchTestNodeExists() {
         final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
-                TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
+                TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
         final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
         final Optional<TreeNode> expectedNode = StoreTreeNodes.findNode(rootNode, TWO_TWO_PATH);
         assertPresentAndType(expectedNode, TreeNode.class);
-        final Map.Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode,
-            TWO_TWO_PATH);
-        assertEquals("Expected node and actual node are not the same", expectedNode.get(), actualNode.getValue());
+
+        final Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode, TWO_TWO_PATH);
+        assertTreeNodeEquals(expectedNode.get(), actualNode.getValue());
     }
 
     @Test
     public void findClosestOrFirstMatchTestNodeDoesNotExist() {
         final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
-                TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
+                TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
         final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
         final YangInstanceIdentifier outerListInnerListPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
                 .node(TestModel.INNER_LIST_QNAME)
                 .build();
         final YangInstanceIdentifier twoTwoInvalidPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
-                .node(TestModel.INNER_LIST_QNAME) //
-                .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "three") //
+                .node(TestModel.INNER_LIST_QNAME)
+                .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "three")
                 .build();
         final Optional<TreeNode> expectedNode = StoreTreeNodes.findNode(rootNode, outerListInnerListPath);
         assertPresentAndType(expectedNode, TreeNode.class);
-        final Map.Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode,
+
+        final Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode,
             twoTwoInvalidPath);
-        assertEquals("Expected node and actual node are not the same", expectedNode.get(), actualNode.getValue());
+        assertTreeNodeEquals(expectedNode.get(), actualNode.getValue());
     }
 
     @Test
     public void getChildTestChildFound() {
         final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
-                TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
+                TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
         final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
         final Optional<TreeNode> node = StoreTreeNodes.getChild(Optional.ofNullable(rootNode),
                 TestModel.TEST_PATH.getLastPathArgument());
@@ -196,10 +180,36 @@ public class StoreTreeNodesTest {
     @Test
     public void getChildTestChildNotFound() {
         final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
-                TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
+                TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
         final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
         final Optional<TreeNode> node = StoreTreeNodes.getChild(Optional.ofNullable(rootNode),
                 TestModel.OUTER_LIST_PATH.getLastPathArgument());
         assertFalse(node.isPresent());
     }
+
+    private static ContainerNode createTestContainer() {
+        return ImmutableContainerNodeBuilder
+                .create()
+                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
+                .withChild(
+                        mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
+                        .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
+                        .withChild(BAR_NODE).build()).build();
+    }
+
+    private static <T extends TreeNode> T assertPresentAndType(final Optional<TreeNode> potential,
+            final Class<T> type) {
+        assertNotNull(potential);
+        assertTrue(potential.isPresent());
+        assertTrue(type.isInstance(potential.get()));
+        return type.cast(potential.get());
+    }
+
+    private static void assertTreeNodeEquals(final TreeNode expected, final TreeNode actual) {
+        assertEquals(expected.getIdentifier(), actual.getIdentifier());
+        assertEquals(expected.getVersion(), actual.getVersion());
+        assertEquals(expected.getSubtreeVersion(), actual.getSubtreeVersion());
+        assertEquals(expected.getData(), actual.getData());
+        assertEquals(expected.toString(), actual.toString());
+    }
 }