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 2008/04/10 08:08:02 UTC

svn commit: r646661 - in /commons/proper/configuration/branches/configuration2_experimental/src: main/java/org/apache/commons/configuration2/ main/java/org/apache/commons/configuration2/flat/ test/java/org/apache/commons/configuration2/

Author: oheger
Date: Wed Apr  9 23:08:00 2008
New Revision: 646661

URL: http://svn.apache.org/viewvc?rev=646661&view=rev
Log:
Implemented MapConfiguration based on BaseConfiguration

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/MapConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/BaseConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestMapConfiguration.java

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/MapConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/MapConfiguration.java?rev=646661&r1=646660&r2=646661&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/MapConfiguration.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/MapConfiguration.java Wed Apr  9 23:08:00 2008
@@ -17,11 +17,11 @@
 
 package org.apache.commons.configuration2;
 
-import java.util.ArrayList;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
+import org.apache.commons.configuration2.flat.BaseConfiguration;
+
 /**
  * <p>A Map based Configuration.</p>
  * <p><em>Note:</em>Configuration objects of this type can be read concurrently
@@ -32,21 +32,20 @@
  * @version $Revision$, $Date$
  * @since 1.1
  */
-public class MapConfiguration extends AbstractConfiguration implements Cloneable
+public class MapConfiguration extends BaseConfiguration
 {
-    /** The Map decorated by this configuration. */
-    protected Map map;
-
     /**
      * Create a Configuration decorator around the specified Map. The map is
      * used to store the configuration properties, any change will also affect
      * the Map.
      *
-     * @param map the map
+     * @param map the map (must not be <b>null</b>)
+     * @throws IllegalArgumentException if the map is <b>null</b>
      */
+    @SuppressWarnings("unchecked")
     public MapConfiguration(Map map)
     {
-        this.map = map;
+        super(map);
     }
 
     /**
@@ -54,89 +53,33 @@
      *
      * @return the map this configuration is based onto
      */
-    public Map getMap()
+    public Map<String, Object> getMap()
     {
-        return map;
+        return getStore();
     }
 
+    /**
+     * Returns the value of the specified property. This implementation checks
+     * for list delimiters in string. (Because the map was created externally,
+     * we cannot be sure that string splitting was performed when the properties
+     * were added.)
+     *
+     * @param key the key of the property
+     * @return the value of this property
+     */
+    @Override
     public Object getProperty(String key)
     {
-        Object value = map.get(key);
+        Object value = super.getProperty(key);
         if ((value instanceof String) && (!isDelimiterParsingDisabled()))
         {
-            List<String> list = PropertyConverter.split((String) value, getListDelimiter());
+            List<String> list = PropertyConverter.split((String) value,
+                    getListDelimiter());
             return list.size() > 1 ? list : list.get(0);
         }
         else
         {
             return value;
-        }
-    }
-
-    protected void addPropertyDirect(String key, Object value)
-    {
-        Object previousValue = getProperty(key);
-
-        if (previousValue == null)
-        {
-            map.put(key, value);
-        }
-        else if (previousValue instanceof List)
-        {
-            // the value is added to the existing list
-            ((List) previousValue).add(value);
-        }
-        else
-        {
-            // the previous value is replaced by a list containing the previous value and the new value
-            List list = new ArrayList();
-            list.add(previousValue);
-            list.add(value);
-
-            map.put(key, list);
-        }
-    }
-
-    public boolean isEmpty()
-    {
-        return map.isEmpty();
-    }
-
-    public boolean containsKey(String key)
-    {
-        return map.containsKey(key);
-    }
-
-    protected void clearPropertyDirect(String key)
-    {
-        map.remove(key);
-    }
-
-    public Iterator<String> getKeys()
-    {
-        return map.keySet().iterator();
-    }
-
-    /**
-     * Returns a copy of this object. The returned configuration will contain
-     * the same properties as the original. Event listeners are not cloned.
-     *
-     * @return the copy
-     * @since 1.3
-     */
-    public Object clone()
-    {
-        try
-        {
-            MapConfiguration copy = (MapConfiguration) super.clone();
-            copy.clearConfigurationListeners();
-            copy.map = (Map) ConfigurationUtils.clone(map);
-            return copy;
-        }
-        catch (CloneNotSupportedException cex)
-        {
-            // cannot happen
-            throw new ConfigurationRuntimeException(cex);
         }
     }
 }

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/BaseConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/BaseConfiguration.java?rev=646661&r1=646660&r2=646661&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/BaseConfiguration.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/BaseConfiguration.java Wed Apr  9 23:08:00 2008
@@ -56,7 +56,45 @@
 public class BaseConfiguration extends AbstractFlatConfiguration implements Cloneable
 {
     /** stores the configuration key-value pairs */
-    private Map<String, Object> store = new LinkedHashMap<String, Object>();
+    private Map<String, Object> store;
+
+    /**
+     * Creates a new, empty instance of <code>BaseConfiguration</code>.
+     */
+    public BaseConfiguration()
+    {
+        this(new LinkedHashMap<String, Object>());
+    }
+
+    /**
+     * Creates a new instance of <code>BaseConfiguration</code> and
+     * initializes it with the given map. This map will be used for storing the
+     * data of this configuration.
+     *
+     * @param map the map with the data (must not be <b>null</b>)
+     * @throws IllegalArgumentException if the passed in map is <b>null</b>
+     * @since 2.0
+     */
+    public BaseConfiguration(Map<String, Object> map)
+    {
+        if (map == null)
+        {
+            throw new IllegalArgumentException("Map must not be null!");
+        }
+        store = map;
+    }
+
+    /**
+     * Returns the underlying map, in which the data of this configuration is
+     * stored.
+     *
+     * @return the map with the data
+     * @since 2.0
+     */
+    protected Map<String, Object> getStore()
+    {
+        return store;
+    }
 
     /**
      * Adds a key/value pair to the map.  This routine does no magic morphing.

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestMapConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestMapConfiguration.java?rev=646661&r1=646660&r2=646661&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestMapConfiguration.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestMapConfiguration.java Wed Apr  9 23:08:00 2008
@@ -36,7 +36,7 @@
 {
     protected AbstractConfiguration getConfiguration()
     {
-        Map map = new HashMap();
+        Map<String, Object> map = new HashMap<String, Object>();
         map.put("key1", "value1");
         map.put("key2", "value2");
         map.put("list", "value1, value2");
@@ -47,12 +47,12 @@
 
     protected AbstractConfiguration getEmptyConfiguration()
     {
-        return new MapConfiguration(new HashMap());
+        return new MapConfiguration(new HashMap<String, Object>());
     }
 
     public void testGetMap()
     {
-        Map map = new HashMap();
+        Map<String, Object> map = new HashMap<String, Object>();
 
         MapConfiguration conf = new MapConfiguration(map);
         assertEquals(map, conf.getMap());
@@ -72,21 +72,39 @@
     public void testCloneModify()
     {
         MapConfiguration config = (MapConfiguration) getConfiguration();
-        config.addConfigurationListener(new ConfigurationListener()
+        ConfigurationListener cl = new ConfigurationListener()
         {
             public void configurationChanged(ConfigurationEvent event)
             {
                 // Just a dummy
             }
-        });
+        };
+        config.addConfigurationListener(cl);
         MapConfiguration copy = (MapConfiguration) config.clone();
-        assertTrue("Event listeners were copied", copy
-                .getConfigurationListeners().isEmpty());
+        assertFalse("Event listeners were copied", copy
+                .getConfigurationListeners().contains(cl));
 
         config.addProperty("cloneTest", Boolean.TRUE);
         assertFalse("Map not decoupled", copy.containsKey("cloneTest"));
         copy.clearProperty("key1");
         assertEquals("Map not decoupled (2)", "value1", config
                 .getString("key1"));
+    }
+
+    /**
+     * Tries creating an instance with a null map. This should cause an
+     * exception.
+     */
+    public void testInitNullMap()
+    {
+        try
+        {
+            new MapConfiguration(null);
+            fail("Could create instance with null map!");
+        }
+        catch (IllegalArgumentException iex)
+        {
+            // ok
+        }
     }
 }



Re: svn commit: r646661 - in /commons/proper/configuration/branches/configuration2_experimental/src: main/java/org/apache/commons/configuration2/ main/java/org/apache/commons/configuration2/flat/ test/java/org/apache/commons/configuration2/

Posted by Emmanuel Bourg <eb...@apache.org>.
oheger@apache.org a écrit :

> -            List<String> list = PropertyConverter.split((String) value, getListDelimiter());
> +            List<String> list = PropertyConverter.split((String) value,
> +                    getListDelimiter());

Oliver, could you increase the line length setting of your IDE to 120 
please ? Wrapping at 80 characters is quite restrictive and doesn't 
improve the readability.

Emmanuel Bourg

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org