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/11/01 21:48:26 UTC

svn commit: r1636034 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration2/tree/ test/java/org/apache/commons/configuration2/tree/

Author: oheger
Date: Sat Nov  1 20:48:25 2014
New Revision: 1636034

URL: http://svn.apache.org/r1636034
Log:
Implemented the new methods of the NodeHandler interface.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/tree/AbstractImmutableNodeHandler.java
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/tree/NodeHandlerDecorator.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/tree/AbstractImmutableNodeHandlerTest.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/tree/AbstractImmutableNodeHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/tree/AbstractImmutableNodeHandler.java?rev=1636034&r1=1636033&r2=1636034&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/tree/AbstractImmutableNodeHandler.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/tree/AbstractImmutableNodeHandler.java Sat Nov  1 20:48:25 2014
@@ -57,15 +57,30 @@ abstract class AbstractImmutableNodeHand
     }
 
     @Override
-    public <C> int getMatchingChildrenCount(ImmutableNode node, NodeMatcher<C> matcher, C criterion) {
-        //TODO implementation
-        throw new UnsupportedOperationException("Not yet implemented!");
+    public <C> int getMatchingChildrenCount(ImmutableNode node,
+            NodeMatcher<C> matcher, C criterion)
+    {
+        return getMatchingChildren(node, matcher, criterion).size();
     }
 
+    /**
+     * {@inheritDoc} This implementation returns an immutable list with all
+     * child nodes accepted by the specified matcher.
+     */
     @Override
-    public <C> List<ImmutableNode> getMatchingChildren(ImmutableNode node, NodeMatcher<C> matcher, C criterion) {
-        //TODO implementation
-        throw new UnsupportedOperationException("Not yet implemented!");
+    public <C> List<ImmutableNode> getMatchingChildren(ImmutableNode node,
+            NodeMatcher<C> matcher, C criterion)
+    {
+        List<ImmutableNode> result =
+                new ArrayList<ImmutableNode>(node.getChildren().size());
+        for (ImmutableNode c : node.getChildren())
+        {
+            if (matcher.matches(c, this, criterion))
+            {
+                result.add(c);
+            }
+        }
+        return Collections.unmodifiableList(result);
     }
 
     /**

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/tree/NodeHandlerDecorator.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/tree/NodeHandlerDecorator.java?rev=1636034&r1=1636033&r2=1636034&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/tree/NodeHandlerDecorator.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/tree/NodeHandlerDecorator.java Sat Nov  1 20:48:25 2014
@@ -58,15 +58,19 @@ public abstract class NodeHandlerDecorat
     }
 
     @Override
-    public <C> List<T> getMatchingChildren(T node, NodeMatcher<C> matcher, C criterion) {
-        //TODO implementation
-        throw new UnsupportedOperationException("Not yet implemented!");
+    public <C> List<T> getMatchingChildren(T node, NodeMatcher<C> matcher,
+            C criterion)
+    {
+        return getDecoratedNodeHandler().getMatchingChildren(node, matcher,
+                criterion);
     }
 
     @Override
-    public <C> int getMatchingChildrenCount(T node, NodeMatcher<C> matcher, C criterion) {
-        //TODO implementation
-        throw new UnsupportedOperationException("Not yet implemented!");
+    public <C> int getMatchingChildrenCount(T node, NodeMatcher<C> matcher,
+            C criterion)
+    {
+        return getDecoratedNodeHandler().getMatchingChildrenCount(node,
+                matcher, criterion);
     }
 
     public List<T> getChildren(T node, String name)

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/tree/AbstractImmutableNodeHandlerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/tree/AbstractImmutableNodeHandlerTest.java?rev=1636034&r1=1636033&r2=1636034&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/tree/AbstractImmutableNodeHandlerTest.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/tree/AbstractImmutableNodeHandlerTest.java Sat Nov  1 20:48:25 2014
@@ -357,4 +357,78 @@ public abstract class AbstractImmutableN
                         .create();
         assertFalse("Defined", handler.isDefined(node));
     }
+
+    /**
+     * Tests a filter operation on child nodes.
+     */
+    @Test
+    public void testNodeHandlerGetMatchingChildren()
+    {
+        final NodeHandler<ImmutableNode> handler =
+                createHandler(ROOT_AUTHORS_TREE);
+        final ImmutableNode target =
+                NodeStructureHelper.nodeForKey(ROOT_AUTHORS_TREE,
+                        NodeStructureHelper.author(1));
+        final Set<String> encounteredAuthors = new HashSet<String>();
+
+        NodeMatcher<ImmutableNode> matcher = new NodeMatcher<ImmutableNode>()
+        {
+            @Override
+            public <T> boolean matches(T node, NodeHandler<T> paramHandler,
+                    ImmutableNode criterion)
+            {
+                encounteredAuthors.add(paramHandler.nodeName(node));
+                return node == target;
+            }
+        };
+
+        List<ImmutableNode> result =
+                handler.getMatchingChildren(handler.getRootNode(), matcher,
+                        target);
+        assertEquals("Wrong number of matched nodes", 1, result.size());
+        assertSame("Wrong result", target, result.get(0));
+        assertEquals("Wrong number of encountered nodes",
+                NodeStructureHelper.authorsLength(), encounteredAuthors.size());
+        for (int i = 0; i < NodeStructureHelper.authorsLength(); i++)
+        {
+            assertTrue("Author not found: " + NodeStructureHelper.author(i),
+                    encounteredAuthors.contains(NodeStructureHelper.author(i)));
+        }
+    }
+
+    /**
+     * Tests that the list returned by getMatchingChildren() cannot be modified.
+     */
+    @Test(expected = UnsupportedOperationException.class)
+    public void testNodeHandlerGetMatchingChildrenImmutable()
+    {
+        NodeHandler<ImmutableNode> handler = createHandler(ROOT_AUTHORS_TREE);
+        List<ImmutableNode> result =
+                handler.getMatchingChildren(handler.getRootNode(),
+                        new DummyNodeMatcher(), this);
+        result.clear();
+    }
+
+    /**
+     * Tests whether filtered nodes can be counted.
+     */
+    @Test
+    public void testNodeHandlerGetMatchingChildrenCount()
+    {
+        NodeHandler<ImmutableNode> handler = createHandler(ROOT_AUTHORS_TREE);
+        assertEquals("Wrong result", NodeStructureHelper.authorsLength(),
+                handler.getMatchingChildrenCount(handler.getRootNode(),
+                        new DummyNodeMatcher(), this));
+    }
+
+    /**
+     * A dummy NodeMatcher implementation that will simply accept all passed in nodes.
+     */
+    private static class DummyNodeMatcher implements NodeMatcher<Object>
+    {
+        @Override
+        public <T> boolean matches(T node, NodeHandler<T> handler, Object criterion) {
+            return true;
+        }
+    }
 }