Add support for accessing primitive arrays in get(Object,int) and size(Object)

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131621 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2004-04-01 22:43:13 +00:00
parent 199716c53f
commit 5f9f0b259e
3 changed files with 70 additions and 26 deletions

View File

@ -38,6 +38,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>Functors - Add additional getInstance() methods for consistency [27856,27857]</li> <li>Functors - Add additional getInstance() methods for consistency [27856,27857]</li>
<li>CollectionUtils - get(Object,int) method now supports primitive arrays</li>
<li>CollectionUtils - Add size(Object) method to find the size of various collection-like objects [27909]</li> <li>CollectionUtils - Add size(Object) method to find the size of various collection-like objects [27909]</li>
</ul> </ul>

View File

@ -15,6 +15,7 @@
*/ */
package org.apache.commons.collections; package org.apache.commons.collections;
import java.lang.reflect.Array;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Enumeration; import java.util.Enumeration;
@ -37,7 +38,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.59 $ $Date: 2004/04/01 20:12:00 $ * @version $Revision: 1.60 $ $Date: 2004/04/01 22:43:13 $
* *
* @author Rodney Waldhoff * @author Rodney Waldhoff
* @author Paul Jack * @author Paul Jack
@ -784,8 +785,8 @@ public class CollectionUtils {
* <code>index</code> in the map's <code>entrySet</code> iterator, * <code>index</code> in the map's <code>entrySet</code> iterator,
* if there is such an entry.</li> * if there is such an entry.</li>
* <li> List -- this method is equivalent to the list's get method.</li> * <li> List -- this method is equivalent to the list's get method.</li>
* <li> Object Array -- the <code>index</code>-th array entry is returned, * <li> Array -- the <code>index</code>-th array entry is returned,
* if there is such an entry; otherwise an <code>ArrayIndexOutOfBoundsException</code> * if there is such an entry; otherwise an <code>IndexOutOfBoundsException</code>
* is thrown.</li> * is thrown.</li>
* <li> Collection -- the value returned is the <code>index</code>-th object * <li> Collection -- the value returned is the <code>index</code>-th object
* returned by the collection's default iterator, if there is such an element.</li> * returned by the collection's default iterator, if there is such an element.</li>
@ -799,8 +800,8 @@ public class CollectionUtils {
* @param object the object to get a value from * @param object the object to get a value from
* @param index the index to get * @param index the index to get
* @return the object at the specified index * @return the object at the specified index
* @throws IndexOutOfBoundsException * @throws IndexOutOfBoundsException if the index is invalid
* @throws IllegalArgumentException * @throws IllegalArgumentException if the object type is invalid
*/ */
public static Object get(Object object, int index) { public static Object get(Object object, int index) {
if (index < 0) { if (index < 0) {
@ -814,17 +815,6 @@ public class CollectionUtils {
return ((List) object).get(index); return ((List) object).get(index);
} else if (object instanceof Object[]) { } else if (object instanceof Object[]) {
return ((Object[]) object)[index]; return ((Object[]) object)[index];
} else if (object instanceof Enumeration) {
Enumeration it = (Enumeration) object;
while (it.hasMoreElements()) {
index--;
if (index == -1) {
return it.nextElement();
} else {
it.nextElement();
}
}
throw new IndexOutOfBoundsException("Entry does not exist: " + index);
} else if (object instanceof Iterator) { } else if (object instanceof Iterator) {
Iterator it = (Iterator) object; Iterator it = (Iterator) object;
while (it.hasNext()) { while (it.hasNext()) {
@ -839,9 +829,25 @@ public class CollectionUtils {
} else if (object instanceof Collection) { } else if (object instanceof Collection) {
Iterator iterator = ((Collection) object).iterator(); Iterator iterator = ((Collection) object).iterator();
return get(iterator, index); return get(iterator, index);
} else if (object instanceof Enumeration) {
Enumeration it = (Enumeration) object;
while (it.hasMoreElements()) {
index--;
if (index == -1) {
return it.nextElement();
} else { } else {
throw new IllegalArgumentException("Unsupported object type: " + it.nextElement();
(object == null ? "null" : object.getClass().getName())); }
}
throw new IndexOutOfBoundsException("Entry does not exist: " + index);
} else if (object == null) {
throw new IllegalArgumentException("Unsupported object type: null");
} else {
try {
return Array.get(object, index);
} catch (IllegalArgumentException ex) {
throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());
}
} }
} }
@ -852,7 +858,7 @@ public class CollectionUtils {
* <ul> * <ul>
* <li>Collection - the collection size * <li>Collection - the collection size
* <li>Map - the map size * <li>Map - the map size
* <li>Object array - the array size * <li>Array - the array size
* <li>Iterator - the number of elements remaining in the iterator * <li>Iterator - the number of elements remaining in the iterator
* <li>Enumeration - the number of elements remaining in the enumeration * <li>Enumeration - the number of elements remaining in the enumeration
* </ul> * </ul>
@ -882,9 +888,14 @@ public class CollectionUtils {
total++; total++;
it.nextElement(); it.nextElement();
} }
} else if (object == null) {
throw new IllegalArgumentException("Unsupported object type: null");
} else { } else {
throw new IllegalArgumentException("Unsupported object type: " + try {
(object == null ? "null" : object.getClass().getName())); total = Array.getLength(object);
} catch (IllegalArgumentException ex) {
throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());
}
} }
return total; return total;
} }

View File

@ -33,6 +33,8 @@ import junit.framework.Test;
import junit.framework.TestCase; import junit.framework.TestCase;
import junit.framework.TestSuite; import junit.framework.TestSuite;
import org.apache.commons.collections.bag.HashBag;
import org.apache.commons.collections.buffer.BoundedFifoBuffer;
import org.apache.commons.collections.collection.AbstractTestCollection; import org.apache.commons.collections.collection.AbstractTestCollection;
import org.apache.commons.collections.collection.PredicatedCollection; import org.apache.commons.collections.collection.PredicatedCollection;
import org.apache.commons.collections.collection.SynchronizedCollection; import org.apache.commons.collections.collection.SynchronizedCollection;
@ -48,7 +50,7 @@ import org.apache.commons.collections.collection.UnmodifiableCollection;
* @author Phil Steitz * @author Phil Steitz
* @author Steven Melzer * @author Steven Melzer
* *
* @version $Revision: 1.36 $ $Date: 2004/03/31 21:43:27 $ * @version $Revision: 1.37 $ $Date: 2004/04/01 22:43:12 $
*/ */
public class TestCollectionUtils extends TestCase { public class TestCollectionUtils extends TestCase {
@ -675,6 +677,23 @@ public class TestCollectionUtils extends TestCase {
} }
} }
{
// Primitive array, entry exists
int[] array = new int[2];
array[0] = 10;
array[1] = 20;
assertEquals(new Integer(10), CollectionUtils.get(array,0));
assertEquals(new Integer(20), CollectionUtils.get(array,1));
// Object array, non-existent entry -- ArrayIndexOutOfBoundsException
try {
CollectionUtils.get(array,2);
fail("Expecting IndexOutOfBoundsException.");
} catch (IndexOutOfBoundsException ex) {
// expected
}
}
{ {
// Invalid object // Invalid object
Object obj = new Object(); Object obj = new Object();
@ -684,6 +703,12 @@ public class TestCollectionUtils extends TestCase {
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
} }
try {
CollectionUtils.get(null, 0);
fail("Expecting IllegalArgumentException.");
} catch (IllegalArgumentException e) {
// expected
}
} }
} }
@ -714,6 +739,17 @@ public class TestCollectionUtils extends TestCase {
stringArray[2] = "c"; stringArray[2] = "c";
assertEquals(3, CollectionUtils.size(stringArray)); assertEquals(3, CollectionUtils.size(stringArray));
} }
public void testSize_PrimitiveArray() {
int[] intArray = new int[0];
assertEquals(0, CollectionUtils.size(intArray));
double[] doubleArray = new double[3];
assertEquals(3, CollectionUtils.size(doubleArray));
doubleArray[0] = 0.0d;
doubleArray[1] = 1.0d;
doubleArray[2] = 2.5d;
assertEquals(3, CollectionUtils.size(doubleArray));
}
public void testSize_Enumeration() { public void testSize_Enumeration() {
Vector list = new Vector(); Vector list = new Vector();
assertEquals(0, CollectionUtils.size(list.elements())); assertEquals(0, CollectionUtils.size(list.elements()));
@ -739,10 +775,6 @@ public class TestCollectionUtils extends TestCase {
CollectionUtils.size("not a list"); CollectionUtils.size("not a list");
fail("Expecting IllegalArgumentException"); fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException e) {} } catch (IllegalArgumentException e) {}
try {
CollectionUtils.size(new int[0]);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException e) {}
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------