[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
This commit is contained in:
Thomas Neidhart 2012-12-13 21:52:42 +00:00
parent 806c1d78d5
commit 244fe70119
9 changed files with 93 additions and 3 deletions

View File

@ -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>

View File

@ -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")
@ -135,6 +137,19 @@ public class CollectionUtils {
return EMPTY_COLLECTION;
}
/**
* 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.
@ -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>

View File

@ -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.

View File

@ -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>

View File

@ -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)}.

View File

@ -1341,6 +1341,13 @@ public class CollectionUtilsTest extends MockTestCase {
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

View File

@ -165,6 +165,13 @@ public class ListUtilsTest extends BulkTest {
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" });

View File

@ -711,6 +711,14 @@ public class MapUtilsTest extends BulkTest {
}
//-----------------------------------------------------------------------
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));

View File

@ -69,6 +69,13 @@ public class SetUtilsTest extends BulkTest {
}
}
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" });