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
This commit is contained in:
Stephen Colebourne 2004-03-31 21:43:27 +00:00
parent c1d10f003e
commit c324046a54
4 changed files with 143 additions and 43 deletions

View File

@ -34,6 +34,7 @@ No interface changes, or deprecations have occurred.
<li>AbstractHashedMap,AbstractLinkedMap - Add methods to access entry methods when protected scope blocks</li> <li>AbstractHashedMap,AbstractLinkedMap - Add methods to access entry methods when protected scope blocks</li>
<li>Functors - Add get methods to retrieve internal state [27515]</li> <li>Functors - Add get methods to retrieve internal state [27515]</li>
<li>MultiHashMap - Add five methods to improve the API</li> <li>MultiHashMap - Add five methods to improve the API</li>
<li>CollectionUtils - Add size(Object) method to find the size of various collection-like objects [27909]</li>
</ul> </ul>
<center><h3>BUG FIXES</h3></center> <center><h3>BUG FIXES</h3></center>

View File

@ -174,6 +174,9 @@
<contributor> <contributor>
<name>Brian McCallister</name> <name>Brian McCallister</name>
</contributor> </contributor>
<contributor>
<name>Steven Melzer</name>
</contributor>
<contributor> <contributor>
<name>Leon Messerschmidt</name> <name>Leon Messerschmidt</name>
</contributor> </contributor>

View File

@ -37,7 +37,7 @@ import org.apache.commons.collections.collection.UnmodifiableCollection;
* Provides utility methods and decorators for {@link Collection} instances. * Provides utility methods and decorators for {@link Collection} instances.
* *
* @since Commons Collections 1.0 * @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 Rodney Waldhoff
* @author Paul Jack * @author Paul Jack
@ -48,6 +48,7 @@ import org.apache.commons.collections.collection.UnmodifiableCollection;
* @author Matthew Hawthorne * @author Matthew Hawthorne
* @author Janek Bogucki * @author Janek Bogucki
* @author Phil Steitz * @author Phil Steitz
* @author Steven Melzer
*/ */
public class CollectionUtils { public class CollectionUtils {
@ -739,13 +740,13 @@ public class CollectionUtils {
return ((Object[])obj)[idx]; return ((Object[])obj)[idx];
} }
else if(obj instanceof Enumeration) { else if(obj instanceof Enumeration) {
Enumeration enum = (Enumeration)obj; Enumeration it = (Enumeration)obj;
while(enum.hasMoreElements()) { while(it.hasMoreElements()) {
idx--; idx--;
if(idx == -1) { if(idx == -1) {
return enum.nextElement(); return it.nextElement();
} else { } else {
enum.nextElement(); it.nextElement();
} }
} }
} }
@ -803,55 +804,91 @@ public class CollectionUtils {
*/ */
public static Object get(Object object, int index) { public static Object get(Object object, int index) {
if (index < 0) { if (index < 0) {
throw new IndexOutOfBoundsException("Index cannot be negative."); throw new IndexOutOfBoundsException("Index cannot be negative: " + index);
} }
if(object instanceof Map) { if (object instanceof Map) {
Map map = (Map)object; Map map = (Map) object;
Iterator iterator = map.entrySet().iterator(); Iterator iterator = map.entrySet().iterator();
return get(iterator, index); return get(iterator, index);
} } else if (object instanceof List) {
else if(object instanceof List) { return ((List) object).get(index);
return ((List)object).get(index); } else if (object instanceof Object[]) {
} return ((Object[]) object)[index];
else if(object instanceof Object[]) { } else if (object instanceof Enumeration) {
return ((Object[])object)[index]; Enumeration it = (Enumeration) object;
} while (it.hasMoreElements()) {
else if(object instanceof Enumeration) {
Enumeration enum = (Enumeration)object;
while(enum.hasMoreElements()) {
index--; index--;
if(index == -1) { if (index == -1) {
return enum.nextElement(); return it.nextElement();
} else { } else {
enum.nextElement(); it.nextElement();
} }
} }
throw new IndexOutOfBoundsException("Entry does not exist."); throw new IndexOutOfBoundsException("Entry does not exist: " + index);
} } else if (object instanceof Iterator) {
else if(object instanceof Iterator) { Iterator it = (Iterator) object;
return get((Iterator)object, index); while (it.hasNext()) {
} index--;
else if(object instanceof Collection) { if (index == -1) {
Iterator iterator = ((Collection)object).iterator(); 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); return get(iterator, index);
} else { } 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.
* <p>
* This method can handles objects as follows
* <ul>
* <li>Collection - the collection size
* <li>Map - the map size
* <li>Object array - the array size
* <li>Iterator - the number of elements remaining in the iterator
* <li>Enumeration - the number of elements remaining in the enumeration
* </ul>
*
* @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. * Reverses the order of the given array.
* *
* @param array the array to reverse * @param array the array to reverse

View File

@ -46,8 +46,9 @@ import org.apache.commons.collections.collection.UnmodifiableCollection;
* @author Matthew Hawthorne * @author Matthew Hawthorne
* @author Stephen Colebourne * @author Stephen Colebourne
* @author Phil Steitz * @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 { 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() { private static Predicate EQUALS_TWO = new Predicate() {
public boolean evaluate(Object input) { public boolean evaluate(Object input) {
return (input.equals("Two")); return (input.equals("Two"));