Author: oheger
Date: Mon Dec 8 21:03:30 2014
New Revision: 1643919
URL: http://svn.apache.org/r1643919
Log:
[CONFIGURATION-200] Added a default implementation for sizeInternal().
This implementation calculates the size of the configuration based on the
iterator returned by getKeys().
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java?rev=1643919&r1=1643918&r2=1643919&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java
(original)
+++
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java
Mon Dec 8 21:03:30 2014
@@ -1055,10 +1055,23 @@ public abstract class AbstractConfigurat
}
}
+ /**
+ * Actually calculates the size of this configuration. This method is
called
+ * by {@code size()} with a read lock held. The base implementation
provided
+ * here calculates the size based on the iterator returned by
+ * {@code getKeys()}. Sub classes which can determine the size in a more
+ * efficient way should override this method.
+ *
+ * @return the size of this configuration (i.e. the number of keys)
+ */
protected int sizeInternal()
{
- // TODO implementation
- return 1;
+ int size = 0;
+ for (Iterator<String> keyIt = getKeysInternal(); keyIt.hasNext();
size++)
+ {
+ keyIt.next();
+ }
+ return size;
}
/**
Modified:
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java?rev=1643919&r1=1643918&r2=1643919&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java
(original)
+++
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java
Mon Dec 8 21:03:30 2014
@@ -1026,6 +1026,21 @@ public class TestAbstractConfigurationBa
}
/**
+ * Tests the default implementation of sizeInternal().
+ */
+ @Test
+ public void testSizeInternal()
+ {
+ AbstractConfiguration config =
+ new TestConfigurationImpl(new PropertiesConfiguration());
+ for (int i = 0; i < PROP_COUNT; i++)
+ {
+ config.addProperty(KEY_PREFIX + i, "value" + i);
+ }
+ assertEquals("Wrong size", PROP_COUNT, config.size());
+ }
+
+ /**
* Creates the source configuration for testing the copy() and append()
* methods. This configuration contains keys with an odd index and values
* starting with the prefix "src". There are also some list properties.