Author: ggregory Date: Thu Apr 20 03:17:59 2017 New Revision: 1792011 URL: http://svn.apache.org/viewvc?rev=1792011&view=rev Log: [CONFIGURATION-664] Add API org.apache.commons.configuration2.tree.ImmutableNode.getChildren(String).
Modified: commons/proper/configuration/trunk/src/changes/changes.xml commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/tree/ImmutableNode.java commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/tree/TestImmutableNode.java Modified: commons/proper/configuration/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/changes/changes.xml?rev=1792011&r1=1792010&r2=1792011&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/changes/changes.xml (original) +++ commons/proper/configuration/trunk/src/changes/changes.xml Thu Apr 20 03:17:59 2017 @@ -41,6 +41,9 @@ <action dev="ggregory" type="add" issue="CONFIGURATION-658"> Add API org.apache.commons.configuration2.DataConfiguration.getURI(String) methods. </action> + <action dev="ggregory" type="add" issue="CONFIGURATION-664"> + Add API org.apache.commons.configuration2.tree.ImmutableNode.getChildren(String). + </action> <action dev="ggregory" type="update" issue="CONFIGURATION-661"> Update platform requirement from Java 6 to 7. </action> Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/tree/ImmutableNode.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/tree/ImmutableNode.java?rev=1792011&r1=1792010&r2=1792011&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/tree/ImmutableNode.java (original) +++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/tree/ImmutableNode.java Thu Apr 20 03:17:59 2017 @@ -102,6 +102,29 @@ public final class ImmutableNode } /** + * Returns a list with the children of this node. This list cannot be + * modified. + * @param name the node name to find + * + * @return a list with the child nodes + */ + public List<ImmutableNode> getChildren(final String name) + { + final List<ImmutableNode> list = new ArrayList<>(); + if (name == null) { + return list; + } + for (final ImmutableNode node : children) + { + if (name.equals(node.getNodeName())) + { + list.add(node); + } + } + return list; + } + + /** * Returns a map with the attributes of this node. This map cannot be * modified. * Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/tree/TestImmutableNode.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/tree/TestImmutableNode.java?rev=1792011&r1=1792010&r2=1792011&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/tree/TestImmutableNode.java (original) +++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/tree/TestImmutableNode.java Thu Apr 20 03:17:59 2017 @@ -30,6 +30,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import org.junit.Assert; import org.junit.Test; /** @@ -404,6 +405,49 @@ public class TestImmutableNode } /** + * Tests getting named children. + */ + @Test + public void testGetChildrenByName() + { + ImmutableNode node = createDefaultNode(VALUE); + ImmutableNode child2 = + new ImmutableNode.Builder().name("child2").create(); + ImmutableNode node2 = node.addChild(child2); + checkUpdatedNode(node, node2); + assertEquals("child2", node2.getChildren("child2").get(0).getNodeName()); + assertEquals(child2, node2.getChildren("child2").get(0)); + } + + /** + * Tests getting named children. + */ + @Test + public void testGetChildrenByNullName() + { + ImmutableNode node = createDefaultNode(VALUE); + ImmutableNode child2 = + new ImmutableNode.Builder().name("child2").create(); + ImmutableNode node2 = node.addChild(child2); + checkUpdatedNode(node, node2); + assertTrue(node2.getChildren(null).isEmpty()); + } + + /** + * Tests getting named children. + */ + @Test + public void testGetChildrenByMissingName() + { + ImmutableNode node = createDefaultNode(VALUE); + ImmutableNode child2 = + new ImmutableNode.Builder().name("child2").create(); + ImmutableNode node2 = node.addChild(child2); + checkUpdatedNode(node, node2); + assertTrue(node2.getChildren("NotFound").isEmpty()); + } + + /** * Tests whether a new null child node is rejected. */ @Test(expected = IllegalArgumentException.class)