commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r676048 - in /commons/proper/configuration/branches/configuration2_experimental/src: main/java/org/apache/commons/configuration2/combined/ main/java/org/apache/commons/configuration2/expr/ test/java/org/apache/commons/configuration2/combine...
Date Fri, 11 Jul 2008 19:03:11 GMT
Author: oheger
Date: Fri Jul 11 12:03:11 2008
New Revision: 676048

URL: http://svn.apache.org/viewvc?rev=676048&view=rev
Log:
CONFIGURATION-333: Added the indexOfChild() method to the NodeHandler interface and updated
the implementations available.

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/CombinedConfigurationNodeHandler.java
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/AbstractNodeHandler.java
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/NodeHandler.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestCombinedConfigurationNodeHandler.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/TestConfigurationNodeHandler.java

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/CombinedConfigurationNodeHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/CombinedConfigurationNodeHandler.java?rev=676048&r1=676047&r2=676048&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/CombinedConfigurationNodeHandler.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/CombinedConfigurationNodeHandler.java
Fri Jul 11 12:03:11 2008
@@ -364,6 +364,18 @@
     }
 
     /**
+     * Determines the index of the specified child node. This implementation
+     * delegates to the node handler responsible for the passed in node.
+     *
+     * @param node the node
+     * @return the index of this node relative to its parent
+     */
+    public int indexOfChild(Object node)
+    {
+        return fetchHandler(node).indexOfChild(node);
+    }
+
+    /**
      * Adds a new sub registry to this node handler registry.
      *
      * @param subreg the registry to add

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/AbstractNodeHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/AbstractNodeHandler.java?rev=676048&r1=676047&r2=676048&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/AbstractNodeHandler.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/AbstractNodeHandler.java
Fri Jul 11 12:03:11 2008
@@ -88,4 +88,33 @@
         setValue(child, value);
         return child;
     }
+
+    /**
+     * Determines the index of the given child node. This implementation
+     * retrieves the parent of this node and then iterates over all children
+     * with the same name.
+     *
+     * @param node the node
+     * @return the index of this child node
+     */
+    public int indexOfChild(T node)
+    {
+        T parent = getParent(node);
+        if (parent == null)
+        {
+            return -1;
+        }
+
+        int index = 0;
+        for (T child : getChildren(parent, nodeName(node)))
+        {
+            if (child == node)
+            {
+                return index;
+            }
+            index++;
+        }
+
+        return -1;
+    }
 }

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/NodeHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/NodeHandler.java?rev=676048&r1=676047&r2=676048&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/NodeHandler.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/NodeHandler.java
Fri Jul 11 12:03:11 2008
@@ -122,6 +122,20 @@
     T getChild(T node, int index);
 
     /**
+     * Returns the index of the given child node relative to its name. This
+     * method will be called when a unique identifier for a specific node is
+     * needed. The node name alone might not be sufficient because there may be
+     * multiple child nodes with the same name. This method returns 0 if the
+     * given node is the first child node with this name, 1 for the second child
+     * node and so on. If the node has no parent node or if it is an attribute,
+     * -1 will be returned.
+     *
+     * @param node a child node whose index is to be retrieved
+     * @return the index of this child node
+     */
+    int indexOfChild(T node);
+
+    /**
      * Returns the number of children of the specified node with the given name.
      * This method exists for performance reasons: for some node implementations
      * it may be by far more efficient to count the children than to query a

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestCombinedConfigurationNodeHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestCombinedConfigurationNodeHandler.java?rev=676048&r1=676047&r2=676048&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestCombinedConfigurationNodeHandler.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestCombinedConfigurationNodeHandler.java
Fri Jul 11 12:03:11 2008
@@ -21,6 +21,8 @@
 import java.util.List;
 import java.util.Map;
 
+import junit.framework.TestCase;
+
 import org.apache.commons.configuration2.ConfigurationRuntimeException;
 import org.apache.commons.configuration2.expr.ConfigurationNodeHandler;
 import org.apache.commons.configuration2.expr.NodeHandler;
@@ -29,8 +31,6 @@
 import org.apache.commons.configuration2.tree.DefaultConfigurationNode;
 import org.easymock.EasyMock;
 
-import junit.framework.TestCase;
-
 /**
  * Test class for CombinedConfigurationNodeHandler.
  *
@@ -453,4 +453,16 @@
         handler.setValue(this, VALUE);
         EasyMock.verify(subHandler);
     }
+
+    /**
+     * Tests the indexOfChild() implementation.
+     */
+    public void testIndexOfChild()
+    {
+        final int index = 11;
+        EasyMock.expect(subHandler.indexOfChild(this)).andReturn(index);
+        EasyMock.replay(subHandler);
+        assertEquals("Wrong index", index, handler.indexOfChild(this));
+        EasyMock.verify(subHandler);
+    }
 }

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/TestConfigurationNodeHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/TestConfigurationNodeHandler.java?rev=676048&r1=676047&r2=676048&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/TestConfigurationNodeHandler.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/TestConfigurationNodeHandler.java
Fri Jul 11 12:03:11 2008
@@ -20,12 +20,12 @@
 import java.util.Collection;
 import java.util.List;
 
+import junit.framework.TestCase;
+
 import org.apache.commons.configuration2.tree.ConfigurationNode;
 import org.apache.commons.configuration2.tree.DefaultConfigurationNode;
 import org.easymock.EasyMock;
 
-import junit.framework.TestCase;
-
 /**
  * Test class for ConfigurationNodeHandler.
  *
@@ -371,4 +371,54 @@
                 .getChildrenCount(node, NAME));
         EasyMock.verify(node);
     }
+
+    /**
+     * Tests determining the index of a child node when this is the first and
+     * only child.
+     */
+    public void testIndexOfChildOnlyChild()
+    {
+        ConfigurationNode node = new DefaultConfigurationNode();
+        ConfigurationNode child = new DefaultConfigurationNode(NAME);
+        node.addChild(child);
+        assertEquals("Wrong index for child", 0, handler.indexOfChild(child));
+    }
+
+    /**
+     * Tests querying the index of a child node if there are multiple children.
+     */
+    public void testIndexOfChildMultipleChildren()
+    {
+        ConfigurationNode node = new DefaultConfigurationNode();
+        node.addChild(new DefaultConfigurationNode(NAME));
+        node.addChild(new DefaultConfigurationNode("AnotherName"));
+        node.addChild(new DefaultConfigurationNode(NAME));
+        node.addChild(new DefaultConfigurationNode("DifferentName"));
+        ConfigurationNode child = new DefaultConfigurationNode(NAME);
+        node.addChild(child);
+        node.addChild(new DefaultConfigurationNode(NAME));
+        assertEquals("Wrong index for child", 2, handler.indexOfChild(child));
+    }
+
+    /**
+     * Tests the indexOfChild() implementation when no parent node exists.
+     */
+    public void testIndexOfChildNoParent()
+    {
+        ConfigurationNode node = new DefaultConfigurationNode(NAME);
+        assertEquals("Wrong index for node without parent", -1, handler
+                .indexOfChild(node));
+    }
+
+    /**
+     * Tests querying the index of an attribute node. Result should be -1.
+     */
+    public void testIndexOfChildAttribute()
+    {
+        ConfigurationNode node = new DefaultConfigurationNode();
+        ConfigurationNode attr = new DefaultConfigurationNode(NAME);
+        node.addAttribute(attr);
+        assertEquals("Wrong index for attribute", -1, handler
+                .indexOfChild(attr));
+    }
 }



Mime
View raw message