Author: oheger
Date: Wed Dec 12 17:58:47 2012
New Revision: 1420889

URL: http://svn.apache.org/viewvc?rev=1420889&view=rev
Log:
Added a merge() method to BasicBuilderParameters.

Modified:
    
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicBuilderParameters.java
    
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicConfigurationBuilder.java
    
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BuilderParameters.java
    
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicBuilderParameters.java

Modified: 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicBuilderParameters.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicBuilderParameters.java?rev=1420889&r1=1420888&r2=1420889&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicBuilderParameters.java
 (original)
+++ 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicBuilderParameters.java
 Wed Dec 12 17:58:47 2012
@@ -138,6 +138,48 @@ public class BasicBuilderParameters impl
     }
 
     /**
+     * Merges this object with the given parameters object. This method adds 
all
+     * property values defined by the passed in parameters object to the
+     * internal storage which are not already in. So properties already defined
+     * in this object take precedence. Property names starting with the 
reserved
+     * parameter prefix are ignored.
+     *
+     * @param p the object whose properties should be merged (must not be
+     *        <b>null</b>)
+     * @throws IllegalArgumentException if the passed in object is <b>null</b>
+     */
+    public void merge(BuilderParameters p)
+    {
+        if (p == null)
+        {
+            throw new IllegalArgumentException(
+                    "Parameters to merge must not be null!");
+        }
+
+        for (Map.Entry<String, Object> e : p.getParameters().entrySet())
+        {
+            if (!properties.containsKey(e.getKey())
+                    && !e.getKey().startsWith(RESERVED_PARAMETER_PREFIX))
+            {
+                storeProperty(e.getKey(), e.getValue());
+            }
+        }
+    }
+
+    /**
+     * Sets a property for this parameters object. Properties are stored in an
+     * internal map. With this method a new entry can be added to this map. It
+     * can be used by sub classes which also store properties in a map.
+     *
+     * @param key the key of the property
+     * @param value the value of the property
+     */
+    protected void storeProperty(String key, Object value)
+    {
+        properties.put(key, value);
+    }
+
+    /**
      * Sets default parameter values.
      */
     private void initDefaults()
@@ -154,7 +196,7 @@ public class BasicBuilderParameters impl
      */
     private BasicBuilderParameters setProperty(String key, Object value)
     {
-        properties.put(key, value);
+        storeProperty(key, value);
         return this;
     }
 }

Modified: 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicConfigurationBuilder.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicConfigurationBuilder.java?rev=1420889&r1=1420888&r2=1420889&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicConfigurationBuilder.java
 (original)
+++ 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicConfigurationBuilder.java
 Wed Dec 12 17:58:47 2012
@@ -100,16 +100,6 @@ public class BasicConfigurationBuilder<T
         ConfigurationBuilder<T>
 {
     /**
-     * Constant for a prefix for reserved initialization parameter keys. If a
-     * parameter was set whose key starts with this prefix, it is filtered out
-     * before the initialization of a newly created result object. This
-     * mechanism allows derived classes to store specific configuration data in
-     * the parameters map which does not represent a property value for the
-     * result object.
-     */
-    public static final String RESERVED_PARAMETER = "config-";
-
-    /**
      * A dummy event source that is used for registering listeners if no
      * compatible result object is available. This source has empty dummy
      * implementations for listener registration methods.
@@ -686,7 +676,7 @@ public class BasicConfigurationBuilder<T
                 .hasNext();)
         {
             String key = it.next();
-            if (key.startsWith(RESERVED_PARAMETER))
+            if (key.startsWith(BuilderParameters.RESERVED_PARAMETER_PREFIX))
             {
                 it.remove();
             }

Modified: 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BuilderParameters.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BuilderParameters.java?rev=1420889&r1=1420888&r2=1420889&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BuilderParameters.java
 (original)
+++ 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BuilderParameters.java
 Wed Dec 12 17:58:47 2012
@@ -40,6 +40,16 @@ import java.util.Map;
 public interface BuilderParameters
 {
     /**
+     * Constant for a prefix for reserved initialization parameter keys. If a
+     * parameter was set whose key starts with this prefix, it is filtered out
+     * before the initialization of a newly created result object. This
+     * mechanism allows implementing classes to store specific configuration
+     * data in the parameters map which does not represent a property value for
+     * the result object.
+     */
+    String RESERVED_PARAMETER_PREFIX = "config-";
+
+    /**
      * Returns a map with all parameters defined by this objects. The keys of
      * the map correspond to concrete properties supported by the
      * {@code Configuration} implementation class the builder produces. The

Modified: 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicBuilderParameters.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicBuilderParameters.java?rev=1420889&r1=1420888&r2=1420889&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicBuilderParameters.java
 (original)
+++ 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicBuilderParameters.java
 Wed Dec 12 17:58:47 2012
@@ -17,6 +17,7 @@
 package org.apache.commons.configuration.builder;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.assertSame;
 
@@ -118,4 +119,42 @@ public class TestBasicBuilderParameters
         assertEquals("Wrong delimiter", Character.valueOf(';'), params
                 .getParameters().get("listDelimiter"));
     }
+
+    /**
+     * Tries a merge with a null object.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testMergeNull()
+    {
+        params.merge(null);
+    }
+
+    /**
+     * Tests whether properties of other parameter objects can be merged.
+     */
+    @Test
+    public void testMerge()
+    {
+        Map<String, Object> props = new HashMap<String, Object>();
+        props.put("throwExceptionOnMissing", Boolean.TRUE);
+        props.put("listDelimiter", Character.valueOf('-'));
+        props.put("other", "test");
+        props.put(BuilderParameters.RESERVED_PARAMETER_PREFIX + "test",
+                "reserved");
+        BuilderParameters p = EasyMock.createMock(BuilderParameters.class);
+        EasyMock.expect(p.getParameters()).andReturn(props);
+        EasyMock.replay(p);
+        params.setListDelimiter('+');
+        params.merge(p);
+        Map<String, Object> map = params.getParameters();
+        assertEquals("Wrong list delimiter", Character.valueOf('+'),
+                map.get("listDelimiter"));
+        assertEquals("Wrong exception flag", Boolean.TRUE,
+                map.get("throwExceptionOnMissing"));
+        assertEquals("Wrong other property", "test", map.get("other"));
+        assertFalse(
+                "Reserved property was copied",
+                map.containsKey(BuilderParameters.RESERVED_PARAMETER_PREFIX
+                        + "test"));
+    }
 }


Reply via email to