From c324046a543ecb0661d1860404f8218426707e9d Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Wed, 31 Mar 2004 21:43:27 +0000 Subject: [PATCH] Add size(Object) method to find the size of various collection-like objects bug 27909, from Steven Melzer git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131612 13f79535-47bb-0310-9956-ffa450edef68 --- RELEASE-NOTES.html | 1 + project.xml | 3 + .../commons/collections/CollectionUtils.java | 119 ++++++++++++------ .../collections/TestCollectionUtils.java | 63 +++++++++- 4 files changed, 143 insertions(+), 43 deletions(-) diff --git a/RELEASE-NOTES.html b/RELEASE-NOTES.html index 8694c393d..aada0e65d 100644 --- a/RELEASE-NOTES.html +++ b/RELEASE-NOTES.html @@ -34,6 +34,7 @@ No interface changes, or deprecations have occurred.
  • AbstractHashedMap,AbstractLinkedMap - Add methods to access entry methods when protected scope blocks
  • Functors - Add get methods to retrieve internal state [27515]
  • MultiHashMap - Add five methods to improve the API
  • +
  • CollectionUtils - Add size(Object) method to find the size of various collection-like objects [27909]
  • BUG FIXES

    diff --git a/project.xml b/project.xml index df3c56a53..c1de7a9f6 100644 --- a/project.xml +++ b/project.xml @@ -174,6 +174,9 @@ Brian McCallister + + Steven Melzer + Leon Messerschmidt diff --git a/src/java/org/apache/commons/collections/CollectionUtils.java b/src/java/org/apache/commons/collections/CollectionUtils.java index e325208df..183ec4ce1 100644 --- a/src/java/org/apache/commons/collections/CollectionUtils.java +++ b/src/java/org/apache/commons/collections/CollectionUtils.java @@ -37,7 +37,7 @@ import org.apache.commons.collections.collection.UnmodifiableCollection; * Provides utility methods and decorators for {@link Collection} instances. * * @since Commons Collections 1.0 - * @version $Revision: 1.56 $ $Date: 2004/02/18 01:15:42 $ + * @version $Revision: 1.57 $ $Date: 2004/03/31 21:43:27 $ * * @author Rodney Waldhoff * @author Paul Jack @@ -48,6 +48,7 @@ import org.apache.commons.collections.collection.UnmodifiableCollection; * @author Matthew Hawthorne * @author Janek Bogucki * @author Phil Steitz + * @author Steven Melzer */ public class CollectionUtils { @@ -739,13 +740,13 @@ public class CollectionUtils { return ((Object[])obj)[idx]; } else if(obj instanceof Enumeration) { - Enumeration enum = (Enumeration)obj; - while(enum.hasMoreElements()) { + Enumeration it = (Enumeration)obj; + while(it.hasMoreElements()) { idx--; if(idx == -1) { - return enum.nextElement(); + return it.nextElement(); } else { - enum.nextElement(); + it.nextElement(); } } } @@ -803,55 +804,91 @@ public class CollectionUtils { */ public static Object get(Object object, int index) { if (index < 0) { - throw new IndexOutOfBoundsException("Index cannot be negative."); + throw new IndexOutOfBoundsException("Index cannot be negative: " + index); } - if(object instanceof Map) { - Map map = (Map)object; + if (object instanceof Map) { + Map map = (Map) object; Iterator iterator = map.entrySet().iterator(); return get(iterator, index); - } - else if(object instanceof List) { - return ((List)object).get(index); - } - else if(object instanceof Object[]) { - return ((Object[])object)[index]; - } - else if(object instanceof Enumeration) { - Enumeration enum = (Enumeration)object; - while(enum.hasMoreElements()) { + } else if (object instanceof List) { + return ((List) object).get(index); + } else if (object instanceof Object[]) { + return ((Object[]) object)[index]; + } else if (object instanceof Enumeration) { + Enumeration it = (Enumeration) object; + while (it.hasMoreElements()) { index--; - if(index == -1) { - return enum.nextElement(); + if (index == -1) { + return it.nextElement(); } else { - enum.nextElement(); + it.nextElement(); } } - throw new IndexOutOfBoundsException("Entry does not exist."); - } - else if(object instanceof Iterator) { - return get((Iterator)object, index); - } - else if(object instanceof Collection) { - Iterator iterator = ((Collection)object).iterator(); + throw new IndexOutOfBoundsException("Entry does not exist: " + index); + } else if (object instanceof Iterator) { + Iterator it = (Iterator) object; + while (it.hasNext()) { + index--; + if (index == -1) { + return it.next(); + } else { + it.next(); + } + } + throw new IndexOutOfBoundsException("Entry does not exist: " + index); + } else if (object instanceof Collection) { + Iterator iterator = ((Collection) object).iterator(); return get(iterator, index); } else { - throw new IllegalArgumentException("Unsupported object type."); + throw new IllegalArgumentException("Unsupported object type: " + + (object == null ? "null" : object.getClass().getName())); } } - private static Object get(Iterator iterator, int index) { - while(iterator.hasNext()) { - index--; - if(index == -1) { - return iterator.next(); - } else { - iterator.next(); - } - } - throw new IndexOutOfBoundsException("Entry does not exist."); - } - /** + * Gets the size of the collection/iterator specified. + *

    + * This method can handles objects as follows + *

    + * + * @param object the object to get the size of + * @return the size of the specified collection + * @throws IllegalArgumentException thrown if object is not recognised or null + */ + public static int size(Object object) { + int total = 0; + if (object instanceof Map) { + total = ((Map) object).size(); + } else if (object instanceof Collection) { + total = ((Collection) object).size(); + } else if (object instanceof Object[]) { + total = ((Object[]) object).length; + } else if (object instanceof Iterator) { + Iterator it = (Iterator) object; + while (it.hasNext()) { + total++; + it.next(); + } + } else if (object instanceof Enumeration) { + Enumeration it = (Enumeration) object; + while (it.hasMoreElements()) { + total++; + it.nextElement(); + } + } else { + throw new IllegalArgumentException("Unsupported object type: " + + (object == null ? "null" : object.getClass().getName())); + } + return total; + } + + /** * Reverses the order of the given array. * * @param array the array to reverse diff --git a/src/test/org/apache/commons/collections/TestCollectionUtils.java b/src/test/org/apache/commons/collections/TestCollectionUtils.java index ee310d441..230db7286 100644 --- a/src/test/org/apache/commons/collections/TestCollectionUtils.java +++ b/src/test/org/apache/commons/collections/TestCollectionUtils.java @@ -46,8 +46,9 @@ import org.apache.commons.collections.collection.UnmodifiableCollection; * @author Matthew Hawthorne * @author Stephen Colebourne * @author Phil Steitz + * @author Steven Melzer * - * @version $Revision: 1.35 $ $Date: 2004/02/18 01:20:35 $ + * @version $Revision: 1.36 $ $Date: 2004/03/31 21:43:27 $ */ public class TestCollectionUtils extends TestCase { @@ -686,7 +687,65 @@ public class TestCollectionUtils extends TestCase { } } - + public void testSize_List() { + List list = new ArrayList(); + assertEquals(0, CollectionUtils.size(list)); + list.add("a"); + assertEquals(1, CollectionUtils.size(list)); + list.add("b"); + assertEquals(2, CollectionUtils.size(list)); + } + public void testSize_Map() { + Map map = new HashMap(); + assertEquals(0, CollectionUtils.size(map)); + map.put("1", "a"); + assertEquals(1, CollectionUtils.size(map)); + map.put("2", "b"); + assertEquals(2, CollectionUtils.size(map)); + } + public void testSize_Array() { + Object[] objectArray = new Object[0]; + assertEquals(0, CollectionUtils.size(objectArray)); + + String[] stringArray = new String[3]; + assertEquals(3, CollectionUtils.size(stringArray)); + stringArray[0] = "a"; + stringArray[1] = "b"; + stringArray[2] = "c"; + assertEquals(3, CollectionUtils.size(stringArray)); + } + public void testSize_Enumeration() { + Vector list = new Vector(); + assertEquals(0, CollectionUtils.size(list.elements())); + list.add("a"); + assertEquals(1, CollectionUtils.size(list.elements())); + list.add("b"); + assertEquals(2, CollectionUtils.size(list.elements())); + } + public void testSize_Iterator() { + List list = new ArrayList(); + assertEquals(0, CollectionUtils.size(list.iterator())); + list.add("a"); + assertEquals(1, CollectionUtils.size(list.iterator())); + list.add("b"); + 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"); + } catch (IllegalArgumentException e) {} + try { + CollectionUtils.size(new int[0]); + fail("Expecting IllegalArgumentException"); + } catch (IllegalArgumentException e) {} + } + + //----------------------------------------------------------------------- private static Predicate EQUALS_TWO = new Predicate() { public boolean evaluate(Object input) { return (input.equals("Two"));