commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t.@apache.org
Subject svn commit: r1421567 - in /commons/proper/collections/trunk/src: changes/ main/java/org/apache/commons/collections/ test/java/org/apache/commons/collections/
Date Thu, 13 Dec 2012 21:52:57 GMT
Author: tn
Date: Thu Dec 13 21:52:42 2012
New Revision: 1421567

URL: http://svn.apache.org/viewvc?rev=1421567&view=rev
Log:
[COLLECTION-436] Added emptyIfNull methods to [Collection,Set,List,Map]Utils. Thanks to Arman
Sharif for report and patch.

Modified:
    commons/proper/collections/trunk/src/changes/changes.xml
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/CollectionUtils.java
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/ListUtils.java
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/MapUtils.java
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/SetUtils.java
    commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/CollectionUtilsTest.java
    commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/ListUtilsTest.java
    commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/MapUtilsTest.java
    commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/SetUtilsTest.java

Modified: commons/proper/collections/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/changes/changes.xml?rev=1421567&r1=1421566&r2=1421567&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/changes/changes.xml (original)
+++ commons/proper/collections/trunk/src/changes/changes.xml Thu Dec 13 21:52:42 2012
@@ -22,6 +22,10 @@
   <body>
 
   <release version="4.0" date="TBA" description="Next release">
+    <action issue="COLLECTIONS-436" dev="tn" type="add" due-to="Arman Sharif">
+      Added "emptyIfNull" methods to classes "CollectionUtils", "ListUtils", "SetUtils"
+      and "MapUtils".
+    </action>
     <action issue="COLLECTIONS-415" dev="tn" type="add" due-to="Adrian Nistor">
       Added clarifying javadoc wrt runtime complexity of "AbstrictLinkedList#removeAll".
     </action>

Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/CollectionUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/CollectionUtils.java?rev=1421567&r1=1421566&r2=1421567&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/CollectionUtils.java
(original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/CollectionUtils.java
Thu Dec 13 21:52:42 2012
@@ -115,7 +115,8 @@ public class CollectionUtils {
      * undesirable. This implementation only implements Collection.
      */
     @SuppressWarnings("rawtypes") // we deliberately use the raw type here
-    public static final Collection EMPTY_COLLECTION = UnmodifiableCollection.unmodifiableCollection(new
ArrayList<Object>());
+    public static final Collection EMPTY_COLLECTION =
+        UnmodifiableCollection.unmodifiableCollection(new ArrayList<Object>());
 
     /**
      * <code>CollectionUtils</code> should not normally be instantiated.
@@ -128,6 +129,7 @@ public class CollectionUtils {
      *
      * @see #EMPTY_COLLECTION
      * @since 4.0
+     * @param <T> the element type
      * @return immutable empty collection
      */
     @SuppressWarnings("unchecked")
@@ -136,6 +138,19 @@ public class CollectionUtils {
     }
 
     /**
+     * Returns an immutable empty collection if the argument is <code>null</code>,
+     * or the argument itself otherwise.
+     * 
+     * @param <T> the element type
+     * @param collection the collection, possibly <code>null</code>
+     * @return an empty collection if the argument is <code>null</code>
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> Collection<T> emptyIfNull(Collection<T> collection)
{
+        return collection == null ? EMPTY_COLLECTION : collection;
+    }
+
+    /**
      * Returns a {@link Collection} containing the union of the given
      * {@link Collection}s.
      * <p>
@@ -188,7 +203,8 @@ public class CollectionUtils {
      * <p>
      * The cardinality of each element <i>e</i> in the returned
      * {@link Collection} will be equal to
-     * <tt>max(cardinality(<i>e</i>,<i>a</i>),cardinality(<i>e</i>,<i>b</i>))
- min(cardinality(<i>e</i>,<i>a</i>),cardinality(<i>e</i>,<i>b</i>))</tt>.
+     * <tt>max(cardinality(<i>e</i>,<i>a</i>),cardinality(<i>e</i>,<i>b</i>))
- min(cardinality(<i>e</i>,<i>a</i>),
+     * cardinality(<i>e</i>,<i>b</i>))</tt>.
      * <p>
      * This is equivalent to
      * <tt>{@link #subtract subtract}({@link #union union(a,b)},{@link #intersection
intersection(a,b)})</tt>

Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/ListUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/ListUtils.java?rev=1421567&r1=1421566&r2=1421567&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/ListUtils.java
(original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/ListUtils.java
Thu Dec 13 21:52:42 2012
@@ -54,6 +54,19 @@ public class ListUtils {
     }
 
     //-----------------------------------------------------------------------
+    
+    /**
+     * Returns an immutable empty list if the argument is <code>null</code>,
+     * or the argument itself otherwise.
+     * 
+     * @param <T> the element type
+     * @param list the list, possibly <code>null</code>
+     * @return an empty list if the argument is <code>null</code>
+     */
+    public static <T> List<T> emptyIfNull(List<T> list) {
+        return list == null ? Collections.<T>emptyList() : list;
+    }
+    
     /**
      * Returns a new list containing all elements that are contained in
      * both given lists.

Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/MapUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/MapUtils.java?rev=1421567&r1=1421566&r2=1421567&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/MapUtils.java
(original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/MapUtils.java
Thu Dec 13 21:52:42 2012
@@ -85,7 +85,8 @@ public class MapUtils {
      * An empty unmodifiable sorted map.
      * This is not provided in the JDK.
      */
-    public static final SortedMap<Object, Object> EMPTY_SORTED_MAP = UnmodifiableSortedMap.unmodifiableSortedMap(new
TreeMap<Object, Object>());
+    public static final SortedMap<Object, Object> EMPTY_SORTED_MAP =
+            UnmodifiableSortedMap.unmodifiableSortedMap(new TreeMap<Object, Object>());
 
     /**
      * String used to indent the verbose and debug Map prints.
@@ -1161,6 +1162,20 @@ public class MapUtils {
     }
 
     //-----------------------------------------------------------------------
+    
+    /**
+     * Returns an immutable empty map if the argument is <code>null</code>,
+     * or the argument itself otherwise.
+     * 
+     * @param <K> the key type
+     * @param <V> the value type
+     * @param map the map, possibly <code>null</code>
+     * @return an empty map if the argument is <code>null</code>
+     */
+    public static <K,V> Map<K,V> emptyIfNull(Map<K,V> map) {
+        return map == null ? Collections.<K,V>emptyMap() : map;
+    }
+    
     /**
      * Null-safe check if the specified map is empty.
      * <p>

Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/SetUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/SetUtils.java?rev=1421567&r1=1421566&r2=1421567&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/SetUtils.java
(original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/SetUtils.java
Thu Dec 13 21:52:42 2012
@@ -81,6 +81,19 @@ public class SetUtils {
     }
 
     //-----------------------------------------------------------------------
+    
+    /**
+     * Returns an immutable empty set if the argument is <code>null</code>,
+     * or the argument itself otherwise.
+     * 
+     * @param <T> the element type
+     * @param set the set, possibly <code>null</code>
+     * @return an empty set if the argument is <code>null</code>
+     */
+    public static <T> Set<T> emptyIfNull(Set<T> set) {
+        return set == null ? Collections.<T>emptySet() : set;
+    }
+    
     /**
      * Tests two sets for equality as per the <code>equals()</code> contract
      * in {@link java.util.Set#equals(java.lang.Object)}.

Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/CollectionUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/CollectionUtilsTest.java?rev=1421567&r1=1421566&r2=1421567&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/CollectionUtilsTest.java
(original)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/CollectionUtilsTest.java
Thu Dec 13 21:52:42 2012
@@ -1341,6 +1341,13 @@ public class CollectionUtilsTest extends
         assertEquals(CollectionUtils.EMPTY_COLLECTION, coll);
     }
 
+    @Test
+    public void emptyIfNull() {
+        assertTrue(CollectionUtils.emptyIfNull(null).isEmpty());
+        Collection<Object> collection = new ArrayList<Object>();
+        assertSame(collection, CollectionUtils.emptyIfNull(collection));
+    }
+    
     /**
      * This test ensures that {@link Iterable}s are supported by {@link CollectionUtils}.
      * Specifically, it uses mocks to ensure that if the passed in

Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/ListUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/ListUtilsTest.java?rev=1421567&r1=1421566&r2=1421567&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/ListUtilsTest.java
(original)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/ListUtilsTest.java
Thu Dec 13 21:52:42 2012
@@ -165,6 +165,13 @@ public class ListUtilsTest extends BulkT
         assertEquals(6, list.size());
     }
 
+    public void testEmptyIfNull() {
+        assertTrue(ListUtils.emptyIfNull(null).isEmpty());
+        
+        List<Long> list = new ArrayList<Long>();
+        assertSame(list, ListUtils.emptyIfNull(list));
+    }
+    
     public void testEquals() {
         Collection<String> data = Arrays.asList( new String[] { "a", "b", "c" });
 

Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/MapUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/MapUtilsTest.java?rev=1421567&r1=1421566&r2=1421567&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/MapUtilsTest.java
(original)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/MapUtilsTest.java
Thu Dec 13 21:52:42 2012
@@ -711,6 +711,14 @@ public class MapUtilsTest extends BulkTe
     }
 
     //-----------------------------------------------------------------------
+    
+    public void testEmptyIfNull() {
+        assertTrue(MapUtils.emptyIfNull(null).isEmpty());
+        
+        Map<Long, Long> map = new HashMap<Long, Long>();
+        assertSame(map, MapUtils.emptyIfNull(map));
+    }
+    
     public void testIsEmptyWithEmptyMap() {
         Map<Object, Object> map = new HashMap<Object, Object>();
         assertEquals(true, MapUtils.isEmpty(map));

Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/SetUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/SetUtilsTest.java?rev=1421567&r1=1421566&r2=1421567&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/SetUtilsTest.java
(original)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/SetUtilsTest.java
Thu Dec 13 21:52:42 2012
@@ -69,6 +69,13 @@ public class SetUtilsTest extends BulkTe
         }
     }
 
+    public void testEmptyIfNull() {
+        assertTrue(SetUtils.emptyIfNull(null).isEmpty());
+        
+        Set<Long> set = new HashSet<Long>();
+        assertSame(set, SetUtils.emptyIfNull(set));
+    }
+    
     public void testEquals() {
         Collection<String> data = Arrays.asList( new String[] { "a", "b", "c" });
 



Mime
View raw message