Author: oheger
Date: Thu May 17 20:15:12 2018
New Revision: 1831806
URL: http://svn.apache.org/viewvc?rev=1831806&view=rev
Log:
CONFIGURATION-701: Added addConfigurationFirst() method.
It is now possible to add configurations with a higher priority as
child configurations to a CompositeConfiguration.
Thanks to nerdynick at gmail dot com for the patch.
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/CompositeConfiguration.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestCompositeConfiguration.java
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/CompositeConfiguration.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/CompositeConfiguration.java?rev=1831806&r1=1831805&r2=1831806&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/CompositeConfiguration.java
(original)
+++
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/CompositeConfiguration.java
Thu May 17 20:15:12 2018
@@ -214,6 +214,58 @@ implements Cloneable
}
/**
+ * Add a configuration to the start of the list of child configurations.
+ *
+ * @param config the configuration to add
+ * @since 2.3
+ */
+ public void addConfigurationFirst(Configuration config)
+ {
+ addConfigurationFirst(config, false);
+ }
+
+ /**
+ * Adds a child configuration to the start of the collection and optionally
+ * makes it the <em>in-memory configuration</em>. This means that all
future
+ * property write operations are executed on this configuration. Note that
+ * the current in-memory configuration is replaced by the new one. If it
was
+ * created automatically or passed to the constructor, it is removed from
the
+ * list of child configurations! Otherwise, it stays in the list of child
configurations
+ * at its current position, but it passes its role as in-memory
configuration to the new one.
+ *
+ * @param config the configuration to be added
+ * @param asInMemory <b>true</b> if this configuration becomes the new
+ * <em>in-memory</em> configuration, <b>false</b> otherwise
+ * @since 2.3
+ */
+ public void addConfigurationFirst(Configuration config, boolean asInMemory)
+ {
+ beginWrite(false);
+ try
+ {
+ if (!configList.contains(config))
+ {
+ if (asInMemory)
+ {
+ replaceInMemoryConfiguration(config);
+ inMemoryConfigIsChild = true;
+ }
+ configList.add(0, config);
+
+ if (config instanceof AbstractConfiguration)
+ {
+ ((AbstractConfiguration) config)
+
.setThrowExceptionOnMissing(isThrowExceptionOnMissing());
+ }
+ }
+ }
+ finally
+ {
+ endWrite();
+ }
+ }
+
+ /**
* Remove a configuration. The in memory configuration cannot be removed.
*
* @param config The configuration to remove
Modified:
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestCompositeConfiguration.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestCompositeConfiguration.java?rev=1831806&r1=1831805&r2=1831806&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestCompositeConfiguration.java
(original)
+++
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestCompositeConfiguration.java
Thu May 17 20:15:12 2018
@@ -112,6 +112,21 @@ public class TestCompositeConfiguration
}
@Test
+ public void testAddFirstRemoveConfigurations() throws Exception
+ {
+ cc.addConfigurationFirst(conf1);
+ assertEquals("Number of configurations", 2,
cc.getNumberOfConfigurations());
+ cc.addConfigurationFirst(conf1);
+ assertEquals("Number of configurations", 2,
cc.getNumberOfConfigurations());
+ cc.addConfigurationFirst(conf2);
+ assertEquals("Number of configurations", 3,
cc.getNumberOfConfigurations());
+ cc.removeConfiguration(conf1);
+ assertEquals("Number of configurations", 2,
cc.getNumberOfConfigurations());
+ cc.clear();
+ assertEquals("Number of configurations", 1,
cc.getNumberOfConfigurations());
+ }
+
+ @Test
public void testGetPropertyWIncludes() throws Exception
{
cc.addConfiguration(conf1);
@@ -131,6 +146,12 @@ public class TestCompositeConfiguration
cc.addConfiguration(conf2);
cc.addConfiguration(conf1);
assertEquals("Make sure we get the property from conf2 first",
"test2.properties", cc.getString("propertyInOrder"));
+ cc.clear();
+
+ cc.addConfiguration(conf1);
+ cc.addConfigurationFirst(conf2);
+ assertEquals("Make sure we get the property from conf2 first",
"test2.properties", cc.getString("propertyInOrder"));
+ cc.clear();
}
@Test