Author: oheger
Date: Thu Aug 22 20:00:52 2013
New Revision: 1516577

URL: http://svn.apache.org/r1516577
Log:
Changed handling of return values for getList() and getCollection().

If the passed in key cannot be resolved and the default value is null, the
methods now return null rather than an empty collection. This is the
behavior which was used by DataConfiguration in version 1.x, and it makes
sense to return the default value directly if no data is found.

Modified:
    
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/AbstractConfiguration.java
    
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ImmutableConfiguration.java
    
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java

Modified: 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/AbstractConfiguration.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/AbstractConfiguration.java?rev=1516577&r1=1516576&r2=1516577&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/AbstractConfiguration.java
 (original)
+++ 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/AbstractConfiguration.java
 Thu Aug 22 20:00:52 2013
@@ -1424,7 +1424,10 @@ public abstract class AbstractConfigurat
     public <T> List<T> getList(Class<T> cls, String key, List<T> defaultValue)
     {
         List<T> result = new ArrayList<T>();
-        getCollection(cls, key, result, defaultValue);
+        if (getCollection(cls, key, result, defaultValue) == null)
+        {
+            return null;
+        }
         return result;
     }
 
@@ -1690,23 +1693,19 @@ public abstract class AbstractConfigurat
     private static <T> Collection<T> handleDefaultCollection(Collection<T> 
target,
             Collection<T> defaultValue)
     {
-        Collection<T> defCol;
-        if (defaultValue != null)
-        {
-            defCol = defaultValue;
-        }
-        else
+        if (defaultValue == null)
         {
-            defCol = Collections.emptyList();
+            return null;
         }
+
         Collection<T> result;
         if (target == null)
         {
-            result = new ArrayList<T>(defCol);
+            result = new ArrayList<T>(defaultValue);
         }
         else
         {
-            target.addAll(defCol);
+            target.addAll(defaultValue);
             result = target;
         }
         return result;

Modified: 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ImmutableConfiguration.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ImmutableConfiguration.java?rev=1516577&r1=1516576&r2=1516577&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ImmutableConfiguration.java
 (original)
+++ 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ImmutableConfiguration.java
 Thu Aug 22 20:00:52 2013
@@ -637,12 +637,9 @@ public interface ImmutableConfiguration
 
     /**
      * Get a collection of typed objects associated with the given 
configuration
-     * key. This method is similar to {@code getList()}, however, it allows
-     * specifying a target collection. Results are added to this collection.
-     * This is useful if the data retrieved should be added to a specific kind
-     * of collection, e.g. a set to remove duplicates. The return value is the
-     * passed in collection if it is not <b>null</b>. Otherwise, a default
-     * collection is created and returned.
+     * key. This method works like
+     * {@link #getCollection(Class, String, Collection, Collection)} passing in
+     * <b>null</b> as default value.
      *
      * @param <T> the element type of the result list
      * @param cls the the element class of the result list
@@ -658,7 +655,21 @@ public interface ImmutableConfiguration
     /**
      * Get a collection of typed objects associated with the given 
configuration
      * key using the values in the specified default collection if the key does
-     * not map to an existing object.
+     * not map to an existing object. This method is similar to
+     * {@code getList()}, however, it allows specifying a target collection.
+     * Results are added to this collection. This is useful if the data
+     * retrieved should be added to a specific kind of collection, e.g. a set 
to
+     * remove duplicates. The return value is as follows:
+     * <ul>
+     * <li>If the key does not map to an existing object and the default value
+     * is <b>null</b>, the method returns <b>null</b>.</li>
+     * <li>If the target collection is not <b>null</b> and data has been added
+     * (either from the resolved property value or from the default 
collection),
+     * the target collection is returned.</li>
+     * <li>If the target collection is <b>null</b> and data has been added
+     * (either from the resolved property value or from the default 
collection),
+     * return value is the target collection created by this method.</li>
+     * </ul>
      *
      * @param <T> the element type of the result list
      * @param cls the the element class of the result list
@@ -668,7 +679,6 @@ public interface ImmutableConfiguration
      * @return the collection to which data was added
      * @throws ConversionException if the conversion is not possible
      * @since 2.0
-     * @see #getCollection(Class, String, Collection)
      */
     <T> Collection<T> getCollection(Class<T> cls, String key,
             Collection<T> target, Collection<T> defaultValue);

Modified: 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java?rev=1516577&r1=1516576&r2=1516577&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java
 (original)
+++ 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java
 Thu Aug 22 20:00:52 2013
@@ -845,7 +845,7 @@ public class TestAbstractConfigurationBa
     public void testGetListUnknownNoDefault()
     {
         PropertiesConfiguration config = new PropertiesConfiguration();
-        assertTrue("Wrong result", config.getList(Integer.class, 
KEY_PREFIX).isEmpty());
+        assertNull("Wrong result", config.getList(Integer.class, KEY_PREFIX));
     }
 
     /**
@@ -908,7 +908,7 @@ public class TestAbstractConfigurationBa
     {
         PropertiesConfiguration config = new PropertiesConfiguration();
         List<Integer> result = new ArrayList<Integer>();
-        assertSame("Wrong result", result, config.getCollection(Integer.class, 
KEY_PREFIX, result));
+        assertNull("Wrong result", config.getCollection(Integer.class, 
KEY_PREFIX, result));
         assertTrue("Got elements", result.isEmpty());
     }
 


Reply via email to