Making it so CollectionUtils.size(null) returns 0 and not an IllegalArgumentException as per COLLECTIONS-230

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@567739 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2007-08-20 16:11:47 +00:00
parent 1d5610521f
commit c4253f47d5
2 changed files with 6 additions and 5 deletions

View File

@ -892,6 +892,9 @@ public class CollectionUtils {
* @since Commons Collections 3.1
*/
public static int size(Object object) {
if (object == null) {
return 0;
}
int total = 0;
if (object instanceof Map) {
total = ((Map) object).size();

View File

@ -739,7 +739,9 @@ public class TestCollectionUtils extends TestCase {
//-----------------------------------------------------------------------
public void testSize_List() {
List list = new ArrayList();
List list = null;
assertEquals(0, CollectionUtils.size(list));
list = new ArrayList();
assertEquals(0, CollectionUtils.size(list));
list.add("a");
assertEquals(1, CollectionUtils.size(list));
@ -793,10 +795,6 @@ public class TestCollectionUtils extends TestCase {
assertEquals(2, CollectionUtils.size(list.iterator()));
}
public void testSize_Other() {
try {
CollectionUtils.size(null);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException e) {}
try {
CollectionUtils.size("not a list");
fail("Expecting IllegalArgumentException");