commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r641255 - in /commons/proper/configuration/branches/configuration2_experimental/src: main/java/org/apache/commons/configuration2/combined/UnionCombiner.java test/java/org/apache/commons/configuration2/combined/TestUnionCombiner.java
Date Wed, 26 Mar 2008 11:07:50 GMT
Author: oheger
Date: Wed Mar 26 04:07:42 2008
New Revision: 641255

URL: http://svn.apache.org/viewvc?rev=641255&view=rev
Log:
New UnionCombiner implementation based on node handlers

Added:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/UnionCombiner.java
      - copied, changed from r636104, commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/tree/UnionCombiner.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestUnionCombiner.java
      - copied, changed from r636104, commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/TestUnionCombiner.java

Copied: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/UnionCombiner.java
(from r636104, commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/tree/UnionCombiner.java)
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/UnionCombiner.java?p2=commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/UnionCombiner.java&p1=commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/tree/UnionCombiner.java&r1=636104&r2=641255&rev=641255&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/tree/UnionCombiner.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/UnionCombiner.java
Wed Mar 26 04:07:42 2008
@@ -14,11 +14,13 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.commons.configuration2.tree;
+package org.apache.commons.configuration2.combined;
 
 import java.util.LinkedList;
 import java.util.List;
 
+import org.apache.commons.configuration2.expr.NodeHandler;
+
 /**
  * <p>
  * A specialized implementation of the <code>NodeCombiner</code> interface
@@ -113,7 +115,7 @@
  * href="http://commons.apache.org/configuration/team-list.html">Commons
  * Configuration team</a>
  * @version $Id$
- * @since 1.3
+ * @since 2.0
  */
 public class UnionCombiner extends NodeCombiner
 {
@@ -121,36 +123,39 @@
      * Combines the given nodes to a new union node.
      *
      * @param node1 the first source node
+     * @param handler1 the handler for the first source node
      * @param node2 the second source node
+     * @param handler2 the handler for the second source node
      * @return the union node
      */
-    public ConfigurationNode combine(ConfigurationNode node1, ConfigurationNode node2)
+    public <T, U> CombinedNode combine(T node1, NodeHandler<T> handler1, U node2,
NodeHandler<U> handler2)
     {
-        ViewNode result = createViewNode();
-        result.setName(node1.getName());
-        result.appendAttributes(node1);
-        result.appendAttributes(node2);
+        CombinedNode result = createViewNode();
+        result.setName(handler1.nodeName(node1));
+        appendAttributes(result, node1, handler1);
+        appendAttributes(result, node2, handler2);
 
         // Check if nodes can be combined
-        List<ConfigurationNode> children2 = new LinkedList<ConfigurationNode>(node2.getChildren());
-        for (ConfigurationNode child1 : node1.getChildren())
+        List<U> children2 = new LinkedList<U>(handler2.getChildren(node2));
+        for (T child1 : handler1.getChildren(node1))
         {
-            ConfigurationNode child2 = findCombineNode(node1, node2, child1, children2);
+            String childName = handler1.nodeName(child1);
+            U child2 = findCombineNode(node1, handler1, node2, handler2, child1, children2);
             if (child2 != null)
             {
-                result.addChild(combine(child1, child2));
+                result.addChild(childName, combine(child1, handler1, child2, handler2));
                 children2.remove(child2);
             }
             else
             {
-                result.addChild(child1);
+                result.addChild(childName, child1);
             }
         }
 
         // Add remaining children of node 2
-        for (ConfigurationNode child : children2)
+        for (U child : children2)
         {
-            result.addChild(child);
+            result.addChild(handler2.nodeName(child), child);
         }
 
         return result;
@@ -158,11 +163,11 @@
 
     /**
      * <p>
-     * Tries to find a child node of the second source node, with whitch a child
+     * Tries to find a child node of the second source node, with which a child
      * of the first source node can be combined. During combining of the source
      * nodes an iteration over the first source node's children is performed.
      * For each child node it is checked whether a corresponding child node in
-     * the second source node exists. If this is the case, these corresponsing
+     * the second source node exists. If this is the case, these corresponding
      * child nodes are recursively combined and the result is added to the
      * combined node. This method implements the checks whether such a recursive
      * combination is possible. The actual implementation tests the following
@@ -178,25 +183,28 @@
      * </ul>
      * </p>
      * <p>
-     * If all of these tests are successfull, the matching child node of the
+     * If all of these tests are successful, the matching child node of the
      * second source node is returned. Otherwise the result is <b>null</b>.
      * </p>
      *
      * @param node1 the first source node
+     * @param handler1 the node handler for the first source node
      * @param node2 the second source node
+     * @param handler2 the node handler for the second source node
      * @param child the child node of the first source node to be checked
      * @param children a list with all children of the second source node
      * @return the matching child node of the second source node or <b>null</b>
      * if there is none
      */
-    protected ConfigurationNode findCombineNode(ConfigurationNode node1, ConfigurationNode
node2, ConfigurationNode child, List children)
+    protected <T, U> U findCombineNode(T node1, NodeHandler<T> handler1, U node2,
NodeHandler<U> handler2, T child, List<U> children)
     {
-        if (child.getValue() == null && !isListNode(child)
-                && node1.getChildrenCount(child.getName()) == 1
-                && node2.getChildrenCount(child.getName()) == 1)
+        String childName = handler1.nodeName(child);
+        if (handler1.getValue(child) == null && !isListNode(child, handler1)
+                && handler1.getChildrenCount(node1, childName) == 1
+                && handler2.getChildrenCount(node2, childName)== 1)
         {
-            ConfigurationNode child2 = node2.getChildren(child.getName()).iterator().next();
-            if (child2.getValue() == null)
+            U child2 = handler2.getChildren(node2, childName).get(0);
+            if (handler2.getValue(child2) == null)
             {
                 return child2;
             }

Copied: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestUnionCombiner.java
(from r636104, commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/TestUnionCombiner.java)
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestUnionCombiner.java?p2=commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestUnionCombiner.java&p1=commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/TestUnionCombiner.java&r1=636104&r2=641255&rev=641255&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/TestUnionCombiner.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestUnionCombiner.java
Wed Mar 26 04:07:42 2008
@@ -14,12 +14,10 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.commons.configuration2.tree;
+package org.apache.commons.configuration2.combined;
 
 import org.apache.commons.configuration2.ConfigurationException;
-import org.apache.commons.configuration2.HierarchicalConfiguration;
-import org.apache.commons.configuration2.tree.NodeCombiner;
-import org.apache.commons.configuration2.tree.UnionCombiner;
+import org.apache.commons.configuration2.InMemoryConfiguration;
 
 /**
  * Test class for UnionCombiner.
@@ -33,6 +31,7 @@
      *
      * @return the combiner
      */
+    @Override
     protected NodeCombiner createCombiner()
     {
         return new UnionCombiner();
@@ -43,7 +42,7 @@
      */
     public void testSimpleValues() throws ConfigurationException
     {
-        HierarchicalConfiguration config = createCombinedConfiguration();
+        InMemoryConfiguration config = createCombinedConfiguration();
         assertEquals("Too few bgcolors", 1, config.getMaxIndex("gui.bgcolor"));
         assertEquals("Wrong first color", "green", config
                 .getString("gui.bgcolor(0)"));
@@ -60,7 +59,7 @@
      */
     public void testSimpleValuesWithAttributes() throws ConfigurationException
     {
-        HierarchicalConfiguration config = createCombinedConfiguration();
+        InMemoryConfiguration config = createCombinedConfiguration();
         assertEquals("Too few level elements", 1, config
                 .getMaxIndex("gui.level"));
         assertEquals("Wrong value of first element", 1, config
@@ -80,7 +79,7 @@
      */
     public void testAttributes() throws ConfigurationException
     {
-        HierarchicalConfiguration config = createCombinedConfiguration();
+        InMemoryConfiguration config = createCombinedConfiguration();
         assertEquals("Too few attributes", 1, config
                 .getMaxIndex("database.tables.table(0)[@id]"));
         assertEquals("Wrong value of first attribute", 1, config
@@ -94,7 +93,7 @@
      */
     public void testLists() throws ConfigurationException
     {
-        HierarchicalConfiguration config = createCombinedConfiguration();
+        InMemoryConfiguration config = createCombinedConfiguration();
         assertEquals("Too few list elements", 2, config
                 .getMaxIndex("net.service.url"));
         assertEquals("Wrong first service", "http://service1.org", config
@@ -115,7 +114,7 @@
     public void testTableList() throws ConfigurationException
     {
         combiner.addListNode("table");
-        HierarchicalConfiguration config = createCombinedConfiguration();
+        InMemoryConfiguration config = createCombinedConfiguration();
         assertEquals("Wrong name of first table", "documents", config
                 .getString("database.tables.table(0).name"));
         assertEquals("Wrong id of first table", 1, config



Mime
View raw message