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
This commit is contained in:
parent
cf197bd0e0
commit
fc7d80e70b
|
@ -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;
|
||||
|
|
|
@ -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");
|
||||
|
|
Loading…
Reference in New Issue