From fc7d80e70bf22698236f18de96faeab09a6574b1 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Sun, 18 May 2008 07:36:46 +0000 Subject: [PATCH] Modifying sizeIsEmpty such that null -> true and not an IllegalArgumentException as per Benjamin Bentmann's patch to COLLECTIONS-298 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@657503 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/commons/collections/CollectionUtils.java | 12 ++++++------ .../commons/collections/TestCollectionUtils.java | 7 +++---- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/java/org/apache/commons/collections/CollectionUtils.java b/src/java/org/apache/commons/collections/CollectionUtils.java index f9a51d552..fa3853405 100644 --- a/src/java/org/apache/commons/collections/CollectionUtils.java +++ b/src/java/org/apache/commons/collections/CollectionUtils.java @@ -941,13 +941,15 @@ public class CollectionUtils { * Note: This method is named to avoid clashing with * {@link #isEmpty(Collection)}. * - * @param object the object to get the size of, not null - * @return true if empty - * @throws IllegalArgumentException thrown if object is not recognised or null + * @param object the object to get the size of, may be null + * @return true if empty or null + * @throws IllegalArgumentException thrown if object is not recognised * @since Commons Collections 3.2 */ public static boolean sizeIsEmpty(Object object) { - if (object instanceof Collection) { + if (object == null) { + return true; + } else if (object instanceof Collection) { return ((Collection) object).isEmpty(); } else if (object instanceof Map) { return ((Map) object).isEmpty(); @@ -957,8 +959,6 @@ public class CollectionUtils { return ((Iterator) object).hasNext() == false; } else if (object instanceof Enumeration) { return ((Enumeration) object).hasMoreElements() == false; - } else if (object == null) { - throw new IllegalArgumentException("Unsupported object type: null"); } else { try { return Array.getLength(object) == 0; diff --git a/src/test/org/apache/commons/collections/TestCollectionUtils.java b/src/test/org/apache/commons/collections/TestCollectionUtils.java index 754893e29..68fe0f197 100644 --- a/src/test/org/apache/commons/collections/TestCollectionUtils.java +++ b/src/test/org/apache/commons/collections/TestCollectionUtils.java @@ -802,6 +802,9 @@ public class TestCollectionUtils extends TestCase { } //----------------------------------------------------------------------- + public void testSizeIsEmpty_Null() { + assertEquals(true, CollectionUtils.sizeIsEmpty(null)); + } public void testSizeIsEmpty_List() { List list = new ArrayList(); assertEquals(true, CollectionUtils.sizeIsEmpty(list)); @@ -855,10 +858,6 @@ public class TestCollectionUtils extends TestCase { assertEquals(true, CollectionUtils.sizeIsEmpty(it)); } public void testSizeIsEmpty_Other() { - try { - CollectionUtils.sizeIsEmpty(null); - fail("Expecting IllegalArgumentException"); - } catch (IllegalArgumentException ex) {} try { CollectionUtils.sizeIsEmpty("not a list"); fail("Expecting IllegalArgumentException");