commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1643924 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java test/java/org/apache/commons/configuration2/TestAbstractHierarchicalConfiguration.java
Date Mon, 08 Dec 2014 21:05:57 GMT
Author: oheger
Date: Mon Dec  8 21:05:57 2014
New Revision: 1643924

URL: http://svn.apache.org/r1643924
Log:
[CONFIGURATION-200] Added a specialized size() implementation for hierarchical configurations.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestAbstractHierarchicalConfiguration.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java?rev=1643924&r1=1643923&r2=1643924&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java
(original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java
Mon Dec  8 21:05:57 2014
@@ -615,6 +615,18 @@ public abstract class AbstractHierarchic
     }
 
     /**
+     * {@inheritDoc} This implementation is slightly more efficient than the
+     * default implementation. It does not iterate over the key set, but
+     * directly queries its size after it has been constructed. Note that
+     * constructing the key set is still an O(n) operation.
+     */
+    @Override
+    protected int sizeInternal()
+    {
+        return visitDefinedKeys().getKeyList().size();
+    }
+
+    /**
      * Returns an iterator with all keys defined in this configuration.
      * Note that the keys returned by this method will not contain any
      * indices. This means that some structure will be lost.</p>
@@ -624,12 +636,21 @@ public abstract class AbstractHierarchic
     @Override
     protected Iterator<String> getKeysInternal()
     {
+        return visitDefinedKeys().getKeyList().iterator();
+    }
+
+    /**
+     * Creates a {@code DefinedKeysVisitor} and visits all defined keys with it.
+     *
+     * @return the visitor after all keys have been visited
+     */
+    private DefinedKeysVisitor visitDefinedKeys()
+    {
         DefinedKeysVisitor visitor = new DefinedKeysVisitor();
         NodeHandler<T> nodeHandler = getModel().getNodeHandler();
         NodeTreeWalker.INSTANCE.walkDFS(nodeHandler.getRootNode(), visitor,
                 nodeHandler);
-
-        return visitor.getKeyList().iterator();
+        return visitor;
     }
 
     /**

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestAbstractHierarchicalConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestAbstractHierarchicalConfiguration.java?rev=1643924&r1=1643923&r2=1643924&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestAbstractHierarchicalConfiguration.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestAbstractHierarchicalConfiguration.java
Mon Dec  8 21:05:57 2014
@@ -423,6 +423,15 @@ public class TestAbstractHierarchicalCon
         assertFalse("More keys than expected", it.hasNext());
     }
 
+    /**
+     * Tests whether the correct size is calculated.
+     */
+    @Test
+    public void testSize()
+    {
+        assertEquals("Wrong size", 2, config.size());
+    }
+
     @Test
     public void testAddProperty()
     {



Mime
View raw message