From c4253f47d57e2eec96da3ccd31f8c366be6ba051 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Mon, 20 Aug 2007 16:11:47 +0000 Subject: [PATCH] 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 --- .../org/apache/commons/collections/CollectionUtils.java | 3 +++ .../apache/commons/collections/TestCollectionUtils.java | 8 +++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/java/org/apache/commons/collections/CollectionUtils.java b/src/java/org/apache/commons/collections/CollectionUtils.java index 7f247d6a9..efe88e679 100644 --- a/src/java/org/apache/commons/collections/CollectionUtils.java +++ b/src/java/org/apache/commons/collections/CollectionUtils.java @@ -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(); diff --git a/src/test/org/apache/commons/collections/TestCollectionUtils.java b/src/test/org/apache/commons/collections/TestCollectionUtils.java index 7565b7f3f..754893e29 100644 --- a/src/test/org/apache/commons/collections/TestCollectionUtils.java +++ b/src/test/org/apache/commons/collections/TestCollectionUtils.java @@ -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");