Author: oheger
Date: Thu Feb 20 20:47:21 2014
New Revision: 1570346

URL: http://svn.apache.org/r1570346
Log:
InMemoryModel now uses a visitor for updating the parent mapping.

The iteration loop for traversing the nodes structure has been replaced by
NodeTreeWalker.walkBFS().

Modified:
    
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/InMemoryNodeModel.java
    
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/ModelTransaction.java

Modified: 
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/InMemoryNodeModel.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/InMemoryNodeModel.java?rev=1570346&r1=1570345&r2=1570346&view=diff
==============================================================================
--- 
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/InMemoryNodeModel.java
 (original)
+++ 
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/InMemoryNodeModel.java
 Thu Feb 20 20:47:21 2014
@@ -196,7 +196,7 @@ public class InMemoryNodeModel implement
         if (valuesNotEmpty(values))
         {
             TreeData currentStructure = getTreeData();
-            ModelTransaction tx = new ModelTransaction(currentStructure);
+            ModelTransaction tx = new ModelTransaction(this);
             NodeAddData<ImmutableNode> addData =
                     resolver.resolveAddKey(currentStructure.getRoot(), key,
                             this);
@@ -225,7 +225,7 @@ public class InMemoryNodeModel implement
     public void clearTree(String key, NodeKeyResolver resolver)
     {
         TreeData currentStructure = getTreeData();
-        ModelTransaction tx = new ModelTransaction(currentStructure);
+        ModelTransaction tx = new ModelTransaction(this);
         for (QueryResult<ImmutableNode> result : resolver.resolveKey(
                 currentStructure.getRoot(), key, this))
         {
@@ -288,21 +288,21 @@ public class InMemoryNodeModel implement
      * @param parents the map with parent nodes
      * @param root the root node of the current tree
      */
-    static void updateParentMapping(Map<ImmutableNode, ImmutableNode> parents,
+    void updateParentMapping(final Map<ImmutableNode, ImmutableNode> parents,
             ImmutableNode root)
     {
-        List<ImmutableNode> pendingNodes = new LinkedList<ImmutableNode>();
-        pendingNodes.add(root);
-
-        while (!pendingNodes.isEmpty())
-        {
-            ImmutableNode node = pendingNodes.remove(0);
-            for (ImmutableNode c : node.getChildren())
-            {
-                pendingNodes.add(c);
-                parents.put(c, node);
-            }
-        }
+        NodeTreeWalker.INSTANCE.walkBFS(root,
+                new ConfigurationNodeVisitorAdapter<ImmutableNode>()
+                {
+                    public void visitBeforeChildren(ImmutableNode node,
+                            NodeHandler<ImmutableNode> handler)
+                    {
+                        for (ImmutableNode c : node.getChildren())
+                        {
+                            parents.put(c, node);
+                        }
+                    }
+                }, this);
     }
 
     /**
@@ -319,6 +319,18 @@ public class InMemoryNodeModel implement
     }
 
     /**
+     * Creates a {@code TreeData} object for the specified root node.
+     *
+     * @param root the root node of the current tree
+     * @return the {@code TreeData} describing the current tree
+     */
+    private TreeData createTreeData(ImmutableNode root)
+    {
+        return new TreeData(root, createParentMapping(root),
+                new HashMap<ImmutableNode, ImmutableNode>());
+    }
+
+    /**
      * Handles an add property operation if the property to be added is a node.
      *
      * @param tx the transaction
@@ -451,18 +463,6 @@ public class InMemoryNodeModel implement
     }
 
     /**
-     * Creates a {@code TreeData} object for the specified root node.
-     *
-     * @param root the root node of the current tree
-     * @return the {@code TreeData} describing the current tree
-     */
-    private static TreeData createTreeData(ImmutableNode root)
-    {
-        return new TreeData(root, createParentMapping(root),
-                new HashMap<ImmutableNode, ImmutableNode>());
-    }
-
-    /**
      * Creates the mapping to parent nodes for the nodes structured represented
      * by the passed in root node. Each node is assigned its parent node. Here
      * an iterative algorithm is used rather than a recursive one to avoid 
stack
@@ -471,7 +471,7 @@ public class InMemoryNodeModel implement
      * @param root the root node of the structure
      * @return the parent node mapping
      */
-    private static Map<ImmutableNode, ImmutableNode> createParentMapping(
+    private Map<ImmutableNode, ImmutableNode> createParentMapping(
             ImmutableNode root)
     {
         Map<ImmutableNode, ImmutableNode> parents =

Modified: 
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/ModelTransaction.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/ModelTransaction.java?rev=1570346&r1=1570345&r2=1570346&view=diff
==============================================================================
--- 
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/ModelTransaction.java
 (original)
+++ 
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/ModelTransaction.java
 Thu Feb 20 20:47:21 2014
@@ -77,6 +77,9 @@ class ModelTransaction
     /** Constant for an unknown level. */
     private static final int LEVEL_UNKNOWN = -1;
 
+    /** The model owning this transaction. */
+    private final InMemoryNodeModel model;
+
     /** Stores the current tree data of the calling node model. */
     private final InMemoryNodeModel.TreeData currentData;
 
@@ -107,13 +110,14 @@ class ModelTransaction
      * Creates a new instance of {@code ModelTransaction} for the current tree
      * data.
      *
-     * @param data the current {@code TreeData} object
+     * @param nodeModel the owning {@code InMemoryNodeModel}
      */
-    public ModelTransaction(InMemoryNodeModel.TreeData data)
+    public ModelTransaction(InMemoryNodeModel nodeModel)
     {
-        currentData = data;
-        replacedNodes = data.copyReplacementMapping();
-        parentMapping = data.copyParentMapping();
+        model = nodeModel;
+        currentData = model.getTreeData();
+        replacedNodes = currentData.copyReplacementMapping();
+        parentMapping = currentData.copyParentMapping();
         operations = new TreeMap<Integer, Map<ImmutableNode, Operations>>();
         addedNodes = new LinkedList<ImmutableNode>();
         removedNodes = new LinkedList<ImmutableNode>();
@@ -306,7 +310,7 @@ class ModelTransaction
     {
         replacedNodes.clear();
         parentMapping.clear();
-        InMemoryNodeModel.updateParentMapping(parentMapping, newRoot);
+        model.updateParentMapping(parentMapping, newRoot);
     }
 
     /**
@@ -316,7 +320,7 @@ class ModelTransaction
     {
         for (ImmutableNode node : addedNodes)
         {
-            InMemoryNodeModel.updateParentMapping(parentMapping, node);
+            model.updateParentMapping(parentMapping, node);
         }
     }
 


Reply via email to