You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2014/04/13 17:58:42 UTC
svn commit: r1587011 - in
/commons/proper/configuration/branches/immutableNodes/src:
main/java/org/apache/commons/configuration/tree/UnionCombiner.java
test/java/org/apache/commons/configuration/tree/TestUnionCombiner.java
Author: oheger
Date: Sun Apr 13 15:58:42 2014
New Revision: 1587011
URL: http://svn.apache.org/r1587011
Log:
Reworked UnionCombiner to operate on ImmutableNode objects.
The class and its test class were adapted to be compatible with the changes
on the NodeCombinder base class.
Modified:
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/UnionCombiner.java
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestUnionCombiner.java
Modified: commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/UnionCombiner.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/UnionCombiner.java?rev=1587011&r1=1587010&r2=1587011&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/UnionCombiner.java (original)
+++ commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/UnionCombiner.java Sun Apr 13 15:58:42 2014
@@ -25,7 +25,7 @@ import java.util.List;
* that constructs a union from two passed in node hierarchies.
* </p>
* <p>
- * The given source hierarchies are traversed and their nodes are added to the
+ * The given source hierarchies are traversed, and their nodes are added to the
* resulting structure. Under some circumstances two nodes can be combined
* rather than adding both. This is the case if both nodes are single children
* (no lists) of their parents and do not have values. The corresponding check
@@ -108,10 +108,14 @@ import java.util.List;
* must not combine the {@code Table} nodes, but add it both to the
* resulting tree.
* </p>
+ * <p>
+ * Another limitation is the handling of attributes: Attributes can only
+ * have a single value. So if two nodes are to be combined which both have
+ * an attribute with the same name, it is not possible to construct a
+ * proper union attribute. In this case, the attribute value from the
+ * first node is used.
+ * </p>
*
- * @author <a
- * href="http://commons.apache.org/configuration/team-list.html">Commons
- * Configuration team</a>
* @version $Id$
* @since 1.3
*/
@@ -125,20 +129,22 @@ public class UnionCombiner extends NodeC
* @return the union node
*/
@Override
- public ConfigurationNode combine(ConfigurationNode node1,
- ConfigurationNode node2)
+ public ImmutableNode combine(ImmutableNode node1,
+ ImmutableNode node2)
{
- ViewNode result = createViewNode();
- result.setName(node1.getName());
- result.appendAttributes(node1);
- result.appendAttributes(node2);
+ ImmutableNode.Builder result = new ImmutableNode.Builder();
+ result.name(node1.getNodeName());
+
+ // attributes of the first node take precedence
+ result.addAttributes(node2.getAttributes());
+ result.addAttributes(node1.getAttributes());
// Check if nodes can be combined
- List<ConfigurationNode> children2 = new LinkedList<ConfigurationNode>(node2.getChildren());
- for (ConfigurationNode child1 : node1.getChildren())
+ List<ImmutableNode> children2 = new LinkedList<ImmutableNode>(node2.getChildren());
+ for (ImmutableNode child1 : node1.getChildren())
{
- ConfigurationNode child2 = findCombineNode(node1, node2, child1,
- children2);
+ ImmutableNode child2 = findCombineNode(node1, node2, child1
+ );
if (child2 != null)
{
result.addChild(combine(child1, child2));
@@ -151,12 +157,12 @@ public class UnionCombiner extends NodeC
}
// Add remaining children of node 2
- for (ConfigurationNode c : children2)
+ for (ImmutableNode c : children2)
{
result.addChild(c);
}
- return result;
+ return result.create();
}
/**
@@ -188,19 +194,18 @@ public class UnionCombiner extends NodeC
* @param node1 the first source node
* @param node2 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<ConfigurationNode> children)
+ protected ImmutableNode findCombineNode(ImmutableNode node1,
+ ImmutableNode node2, ImmutableNode child)
{
if (child.getValue() == null && !isListNode(child)
- && node1.getChildrenCount(child.getName()) == 1
- && node2.getChildrenCount(child.getName()) == 1)
+ && HANDLER.getChildrenCount(node1, child.getNodeName()) == 1
+ && HANDLER.getChildrenCount(node2, child.getNodeName()) == 1)
{
- ConfigurationNode child2 = node2.getChildren(
- child.getName()).iterator().next();
+ ImmutableNode child2 =
+ HANDLER.getChildren(node2, child.getNodeName()).get(0);
if (child2.getValue() == null)
{
return child2;
Modified: commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestUnionCombiner.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestUnionCombiner.java?rev=1587011&r1=1587010&r2=1587011&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestUnionCombiner.java (original)
+++ commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestUnionCombiner.java Sun Apr 13 15:58:42 2014
@@ -19,7 +19,7 @@ package org.apache.commons.configuration
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
-import org.apache.commons.configuration.HierarchicalConfiguration;
+import org.apache.commons.configuration.BaseHierarchicalConfiguration;
import org.apache.commons.configuration.ex.ConfigurationException;
import org.junit.Test;
@@ -47,7 +47,7 @@ public class TestUnionCombiner extends A
@Test
public void testSimpleValues() throws ConfigurationException
{
- HierarchicalConfiguration config = createCombinedConfiguration();
+ BaseHierarchicalConfiguration config = createCombinedConfiguration();
assertEquals("Too few bgcolors", 1, config.getMaxIndex("gui.bgcolor"));
assertEquals("Wrong first color", "green", config
.getString("gui.bgcolor(0)"));
@@ -65,7 +65,7 @@ public class TestUnionCombiner extends A
@Test
public void testSimpleValuesWithAttributes() throws ConfigurationException
{
- HierarchicalConfiguration config = createCombinedConfiguration();
+ BaseHierarchicalConfiguration config = createCombinedConfiguration();
assertEquals("Too few level elements", 1, config
.getMaxIndex("gui.level"));
assertEquals("Wrong value of first element", 1, config
@@ -86,13 +86,11 @@ public class TestUnionCombiner extends A
@Test
public void testAttributes() throws ConfigurationException
{
- HierarchicalConfiguration config = createCombinedConfiguration();
- assertEquals("Too few attributes", 1, config
+ BaseHierarchicalConfiguration config = createCombinedConfiguration();
+ assertEquals("Wrong number of attributes", 0, config
.getMaxIndex("database.tables.table(0)[@id]"));
- assertEquals("Wrong value of first attribute", 1, config
+ assertEquals("Wrong value of attribute", 1, config
.getInt("database.tables.table(0)[@id](0)"));
- assertEquals("Wrong value of second attribute", 2, config
- .getInt("database.tables.table(0)[@id](1)"));
}
/**
@@ -101,7 +99,7 @@ public class TestUnionCombiner extends A
@Test
public void testLists() throws ConfigurationException
{
- HierarchicalConfiguration config = createCombinedConfiguration();
+ BaseHierarchicalConfiguration config = createCombinedConfiguration();
assertEquals("Too few list elements", 2, config
.getMaxIndex("net.service.url"));
assertEquals("Wrong first service", "http://service1.org", config
@@ -123,7 +121,7 @@ public class TestUnionCombiner extends A
public void testTableList() throws ConfigurationException
{
combiner.addListNode("table");
- HierarchicalConfiguration config = createCombinedConfiguration();
+ BaseHierarchicalConfiguration config = createCombinedConfiguration();
assertEquals("Wrong name of first table", "documents", config
.getString("database.tables.table(0).name"));
assertEquals("Wrong id of first table", 1, config