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:
parent
1d5610521f
commit
c4253f47d5
|
@ -892,6 +892,9 @@ public class CollectionUtils {
|
||||||
* @since Commons Collections 3.1
|
* @since Commons Collections 3.1
|
||||||
*/
|
*/
|
||||||
public static int size(Object object) {
|
public static int size(Object object) {
|
||||||
|
if (object == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
int total = 0;
|
int total = 0;
|
||||||
if (object instanceof Map) {
|
if (object instanceof Map) {
|
||||||
total = ((Map) object).size();
|
total = ((Map) object).size();
|
||||||
|
|
|
@ -739,7 +739,9 @@ public class TestCollectionUtils extends TestCase {
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
public void testSize_List() {
|
public void testSize_List() {
|
||||||
List list = new ArrayList();
|
List list = null;
|
||||||
|
assertEquals(0, CollectionUtils.size(list));
|
||||||
|
list = new ArrayList();
|
||||||
assertEquals(0, CollectionUtils.size(list));
|
assertEquals(0, CollectionUtils.size(list));
|
||||||
list.add("a");
|
list.add("a");
|
||||||
assertEquals(1, CollectionUtils.size(list));
|
assertEquals(1, CollectionUtils.size(list));
|
||||||
|
@ -793,10 +795,6 @@ public class TestCollectionUtils extends TestCase {
|
||||||
assertEquals(2, CollectionUtils.size(list.iterator()));
|
assertEquals(2, CollectionUtils.size(list.iterator()));
|
||||||
}
|
}
|
||||||
public void testSize_Other() {
|
public void testSize_Other() {
|
||||||
try {
|
|
||||||
CollectionUtils.size(null);
|
|
||||||
fail("Expecting IllegalArgumentException");
|
|
||||||
} catch (IllegalArgumentException e) {}
|
|
||||||
try {
|
try {
|
||||||
CollectionUtils.size("not a list");
|
CollectionUtils.size("not a list");
|
||||||
fail("Expecting IllegalArgumentException");
|
fail("Expecting IllegalArgumentException");
|
||||||
|
|
Loading…
Reference in New Issue