commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r831803 - in /commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2: AbstractHierarchicalConfiguration.java expr/NodeVisitorAdapter.java
Date Mon, 02 Nov 2009 06:41:12 GMT
Author: oheger
Date: Mon Nov  2 06:41:11 2009
New Revision: 831803

URL: http://svn.apache.org/viewvc?rev=831803&view=rev
Log:
Moved default implementation of visitor mechanism to NodeVisitorAdapter so that it can be
better reused.

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/NodeVisitorAdapter.java

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java?rev=831803&r1=831802&r2=831803&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java
Mon Nov  2 06:41:11 2009
@@ -712,17 +712,7 @@
      */
     protected void visit(T node, NodeVisitor<T> visitor)
     {
-        if (!visitor.terminate())
-        {
-            visitor.visitBeforeChildren(node, getNodeHandler());
-
-            for (Iterator<T> it = getNodeHandler().getChildren(node).iterator(); it.hasNext()
&& !visitor.terminate();)
-            {
-                visit(it.next(), visitor);
-            }
-
-            visitor.visitAfterChildren(node, getNodeHandler());
-        }
+        NodeVisitorAdapter.visit(visitor, node, getNodeHandler());
     }
 
     /**

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/NodeVisitorAdapter.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/NodeVisitorAdapter.java?rev=831803&r1=831802&r2=831803&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/NodeVisitorAdapter.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/NodeVisitorAdapter.java
Mon Nov  2 06:41:11 2009
@@ -16,6 +16,8 @@
  */
 package org.apache.commons.configuration2.expr;
 
+import java.util.Iterator;
+
 /**
  * <p>
  * A simple adapter class that simplifies writing custom node visitor
@@ -23,11 +25,20 @@
  * </p>
  * <p>
  * This class provides dummy implementations for the methods defined in the
- * <code>ConfigurationNodeVisitor</code> interface. Derived classes only need
- * to override the methods they really need.
+ * {@link NodeVisitor} interface. Derived classes only need to override the
+ * methods they really need.
+ * </p>
+ * <p>
+ * In addition to the dummy implementations of the {@link NodeVisitor} methods,
+ * this class also provides a static method that implements the default visiting
+ * mechanism: The {@code visit()} method is passed a {@code NodeVisitor}, a
+ * node, and a corresponding {@code NodeHandler}. It then traverses the whole
+ * nodes structure and invokes the visitor for each encountered node.
  * </p>
  *
- * @author Oliver Heger
+ * @author <a
+ *         href="http://commons.apache.org/configuration/team-list.html">Commons
+ *         Configuration team</a>
  * @version $Id$
  * @param <T> the type of the involved nodes
  */
@@ -65,4 +76,34 @@
     public void visitBeforeChildren(T node, NodeHandler<T> handler)
     {
     }
+
+    /**
+     * Traverses the nodes structure below the specified root node and calls the
+     * visitor for each encountered node. If the {@code terminate()} method of
+     * the visitor returns <b>true</b>, the visit operation is aborted. This
+     * method provides a default implementation of the visitor mechanism.
+     *
+     * @param <N> the type of the nodes involved
+     * @param visitor the visitor
+     * @param node the root node of the hierarchy
+     * @param handler the node handler
+     * @throws NullPointerException if a required parameter is missing
+     */
+    public static <N> void visit(NodeVisitor<N> visitor, N node,
+            NodeHandler<N> handler)
+    {
+        if (!visitor.terminate())
+        {
+            visitor.visitBeforeChildren(node, handler);
+
+            for (Iterator<N> it = handler.getChildren(node).iterator(); it
+                    .hasNext()
+                    && !visitor.terminate();)
+            {
+                visit(visitor, it.next(), handler);
+            }
+
+            visitor.visitAfterChildren(node, handler);
+        }
+    }
 }



Mime
View raw message