From 244fe70119386f5363b08f32f44fb0dbc941ac24 Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Thu, 13 Dec 2012 21:52:42 +0000 Subject: [PATCH] [COLLECTION-436] Added emptyIfNull methods to [Collection,Set,List,Map]Utils. Thanks to Arman Sharif for report and patch. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1421567 13f79535-47bb-0310-9956-ffa450edef68 --- src/changes/changes.xml | 4 ++++ .../commons/collections/CollectionUtils.java | 20 +++++++++++++++++-- .../apache/commons/collections/ListUtils.java | 13 ++++++++++++ .../apache/commons/collections/MapUtils.java | 17 +++++++++++++++- .../apache/commons/collections/SetUtils.java | 13 ++++++++++++ .../collections/CollectionUtilsTest.java | 7 +++++++ .../commons/collections/ListUtilsTest.java | 7 +++++++ .../commons/collections/MapUtilsTest.java | 8 ++++++++ .../commons/collections/SetUtilsTest.java | 7 +++++++ 9 files changed, 93 insertions(+), 3 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 302e4014c..6be18bb11 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,10 @@ + + Added "emptyIfNull" methods to classes "CollectionUtils", "ListUtils", "SetUtils" + and "MapUtils". + Added clarifying javadoc wrt runtime complexity of "AbstrictLinkedList#removeAll". diff --git a/src/main/java/org/apache/commons/collections/CollectionUtils.java b/src/main/java/org/apache/commons/collections/CollectionUtils.java index 986a8c5ce..ec1ea58c4 100644 --- a/src/main/java/org/apache/commons/collections/CollectionUtils.java +++ b/src/main/java/org/apache/commons/collections/CollectionUtils.java @@ -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()); + public static final Collection EMPTY_COLLECTION = + UnmodifiableCollection.unmodifiableCollection(new ArrayList()); /** * CollectionUtils should not normally be instantiated. @@ -128,6 +129,7 @@ public class CollectionUtils { * * @see #EMPTY_COLLECTION * @since 4.0 + * @param the element type * @return immutable empty collection */ @SuppressWarnings("unchecked") @@ -135,6 +137,19 @@ public class CollectionUtils { return EMPTY_COLLECTION; } + /** + * Returns an immutable empty collection if the argument is null, + * or the argument itself otherwise. + * + * @param the element type + * @param collection the collection, possibly null + * @return an empty collection if the argument is null + */ + @SuppressWarnings("unchecked") + public static Collection emptyIfNull(Collection collection) { + return collection == null ? EMPTY_COLLECTION : collection; + } + /** * Returns a {@link Collection} containing the union of the given * {@link Collection}s. @@ -188,7 +203,8 @@ public class CollectionUtils { *

* The cardinality of each element e in the returned * {@link Collection} will be equal to - * max(cardinality(e,a),cardinality(e,b)) - min(cardinality(e,a),cardinality(e,b)). + * max(cardinality(e,a),cardinality(e,b)) - min(cardinality(e,a), + * cardinality(e,b)). *

* This is equivalent to * {@link #subtract subtract}({@link #union union(a,b)},{@link #intersection intersection(a,b)}) diff --git a/src/main/java/org/apache/commons/collections/ListUtils.java b/src/main/java/org/apache/commons/collections/ListUtils.java index b3c206756..6bc1c8e4d 100644 --- a/src/main/java/org/apache/commons/collections/ListUtils.java +++ b/src/main/java/org/apache/commons/collections/ListUtils.java @@ -54,6 +54,19 @@ public class ListUtils { } //----------------------------------------------------------------------- + + /** + * Returns an immutable empty list if the argument is null, + * or the argument itself otherwise. + * + * @param the element type + * @param list the list, possibly null + * @return an empty list if the argument is null + */ + public static List emptyIfNull(List list) { + return list == null ? Collections.emptyList() : list; + } + /** * Returns a new list containing all elements that are contained in * both given lists. diff --git a/src/main/java/org/apache/commons/collections/MapUtils.java b/src/main/java/org/apache/commons/collections/MapUtils.java index 6dc07fb3a..0edcd5935 100644 --- a/src/main/java/org/apache/commons/collections/MapUtils.java +++ b/src/main/java/org/apache/commons/collections/MapUtils.java @@ -85,7 +85,8 @@ public class MapUtils { * An empty unmodifiable sorted map. * This is not provided in the JDK. */ - public static final SortedMap EMPTY_SORTED_MAP = UnmodifiableSortedMap.unmodifiableSortedMap(new TreeMap()); + public static final SortedMap EMPTY_SORTED_MAP = + UnmodifiableSortedMap.unmodifiableSortedMap(new TreeMap()); /** * 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 null, + * or the argument itself otherwise. + * + * @param the key type + * @param the value type + * @param map the map, possibly null + * @return an empty map if the argument is null + */ + public static Map emptyIfNull(Map map) { + return map == null ? Collections.emptyMap() : map; + } + /** * Null-safe check if the specified map is empty. *

diff --git a/src/main/java/org/apache/commons/collections/SetUtils.java b/src/main/java/org/apache/commons/collections/SetUtils.java index 0d0ddee70..80c4b184d 100644 --- a/src/main/java/org/apache/commons/collections/SetUtils.java +++ b/src/main/java/org/apache/commons/collections/SetUtils.java @@ -81,6 +81,19 @@ public class SetUtils { } //----------------------------------------------------------------------- + + /** + * Returns an immutable empty set if the argument is null, + * or the argument itself otherwise. + * + * @param the element type + * @param set the set, possibly null + * @return an empty set if the argument is null + */ + public static Set emptyIfNull(Set set) { + return set == null ? Collections.emptySet() : set; + } + /** * Tests two sets for equality as per the equals() contract * in {@link java.util.Set#equals(java.lang.Object)}. diff --git a/src/test/java/org/apache/commons/collections/CollectionUtilsTest.java b/src/test/java/org/apache/commons/collections/CollectionUtilsTest.java index c2c5f1530..1297398bd 100644 --- a/src/test/java/org/apache/commons/collections/CollectionUtilsTest.java +++ b/src/test/java/org/apache/commons/collections/CollectionUtilsTest.java @@ -1341,6 +1341,13 @@ public class CollectionUtilsTest extends MockTestCase { assertEquals(CollectionUtils.EMPTY_COLLECTION, coll); } + @Test + public void emptyIfNull() { + assertTrue(CollectionUtils.emptyIfNull(null).isEmpty()); + Collection collection = new ArrayList(); + 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 diff --git a/src/test/java/org/apache/commons/collections/ListUtilsTest.java b/src/test/java/org/apache/commons/collections/ListUtilsTest.java index a19a0e0bd..83f36229a 100644 --- a/src/test/java/org/apache/commons/collections/ListUtilsTest.java +++ b/src/test/java/org/apache/commons/collections/ListUtilsTest.java @@ -165,6 +165,13 @@ public class ListUtilsTest extends BulkTest { assertEquals(6, list.size()); } + public void testEmptyIfNull() { + assertTrue(ListUtils.emptyIfNull(null).isEmpty()); + + List list = new ArrayList(); + assertSame(list, ListUtils.emptyIfNull(list)); + } + public void testEquals() { Collection data = Arrays.asList( new String[] { "a", "b", "c" }); diff --git a/src/test/java/org/apache/commons/collections/MapUtilsTest.java b/src/test/java/org/apache/commons/collections/MapUtilsTest.java index 4fef61709..985d5f2bc 100644 --- a/src/test/java/org/apache/commons/collections/MapUtilsTest.java +++ b/src/test/java/org/apache/commons/collections/MapUtilsTest.java @@ -711,6 +711,14 @@ public class MapUtilsTest extends BulkTest { } //----------------------------------------------------------------------- + + public void testEmptyIfNull() { + assertTrue(MapUtils.emptyIfNull(null).isEmpty()); + + Map map = new HashMap(); + assertSame(map, MapUtils.emptyIfNull(map)); + } + public void testIsEmptyWithEmptyMap() { Map map = new HashMap(); assertEquals(true, MapUtils.isEmpty(map)); diff --git a/src/test/java/org/apache/commons/collections/SetUtilsTest.java b/src/test/java/org/apache/commons/collections/SetUtilsTest.java index bd5933028..66162f8c9 100644 --- a/src/test/java/org/apache/commons/collections/SetUtilsTest.java +++ b/src/test/java/org/apache/commons/collections/SetUtilsTest.java @@ -69,6 +69,13 @@ public class SetUtilsTest extends BulkTest { } } + public void testEmptyIfNull() { + assertTrue(SetUtils.emptyIfNull(null).isEmpty()); + + Set set = new HashSet(); + assertSame(set, SetUtils.emptyIfNull(set)); + } + public void testEquals() { Collection data = Arrays.asList( new String[] { "a", "b", "c" });