[COLLECTIONS-375] Added ListUtils.defaultIfNull.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1457533 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2013-03-17 19:35:45 +00:00
parent 8194aba1f4
commit 0a131d671c
3 changed files with 25 additions and 1 deletions

View File

@ -22,6 +22,9 @@
<body>
<release version="4.0" date="TBA" description="Next release">
<action issue="COLLECTIONS-375" dev="tn" type="add" due-to="Ivan Hristov">
Added method "ListUtils#defaultIfNull(List, List)".
</action>
<action issue="COLLECTIONS-451" dev="tn" type="change">
The constructors for all Utils classes are now private to prevent instantiation.
</action>

View File

@ -23,6 +23,7 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.commons.collections.bag.HashBag;
import org.apache.commons.collections.list.FixedSizeList;
@ -64,6 +65,19 @@ public class ListUtils {
public static <T> List<T> emptyIfNull(final List<T> list) {
return list == null ? Collections.<T>emptyList() : list;
}
/**
* Returns either the passed in list, or if the list is {@code null},
* the value of {@code defaultList}.
*
* @param <T> the element type
* @param list the list, possibly {@code null}
* @param defaultList the returned values if list is {@code null}
* @return an empty list if the argument is <code>null</code>
*/
public static <T> List<T> defaultIfNull(final List<T> list, final List<T> defaultList) {
return list == null ? defaultList : list;
}
/**
* Returns a new list containing all elements that are contained in

View File

@ -170,7 +170,14 @@ public class ListUtilsTest extends BulkTest {
final List<Long> list = new ArrayList<Long>();
assertSame(list, ListUtils.emptyIfNull(list));
}
public void testDefaultIfNull() {
assertTrue(ListUtils.defaultIfNull(null, Collections.emptyList()).isEmpty());
final List<Long> list = new ArrayList<Long>();
assertSame(list, ListUtils.defaultIfNull(list, Collections.<Long>emptyList()));
}
public void testEquals() {
final Collection<String> data = Arrays.asList( new String[] { "a", "b", "c" });