scolebourne 2004/04/01 14:43:13
Modified: collections RELEASE-NOTES.html
collections/src/test/org/apache/commons/collections
TestCollectionUtils.java
collections/src/java/org/apache/commons/collections
CollectionUtils.java
Log:
Add support for accessing primitive arrays in get(Object,int) and size(Object)
Revision Changes Path
1.24 +1 -0 jakarta-commons/collections/RELEASE-NOTES.html
Index: RELEASE-NOTES.html
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/RELEASE-NOTES.html,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -r1.23 -r1.24
--- RELEASE-NOTES.html 1 Apr 2004 22:18:11 -0000 1.23
+++ RELEASE-NOTES.html 1 Apr 2004 22:43:12 -0000 1.24
@@ -38,6 +38,7 @@
<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 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>
</ul>
1.37 +37 -5
jakarta-commons/collections/src/test/org/apache/commons/collections/TestCollectionUtils.java
Index: TestCollectionUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestCollectionUtils.java,v
retrieving revision 1.36
retrieving revision 1.37
diff -u -r1.36 -r1.37
--- TestCollectionUtils.java 31 Mar 2004 21:43:27 -0000 1.36
+++ TestCollectionUtils.java 1 Apr 2004 22:43:12 -0000 1.37
@@ -33,6 +33,8 @@
import junit.framework.TestCase;
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.PredicatedCollection;
import org.apache.commons.collections.collection.SynchronizedCollection;
@@ -676,6 +678,23 @@
}
{
+ // 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
Object obj = new Object();
try {
@@ -684,6 +703,12 @@
} catch (IllegalArgumentException e) {
// expected
}
+ try {
+ CollectionUtils.get(null, 0);
+ fail("Expecting IllegalArgumentException.");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
}
}
@@ -714,6 +739,17 @@
stringArray[2] = "c";
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() {
Vector list = new Vector();
assertEquals(0, CollectionUtils.size(list.elements()));
@@ -737,10 +773,6 @@
} 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) {}
}
1.60 +32 -21
jakarta-commons/collections/src/java/org/apache/commons/collections/CollectionUtils.java
Index: CollectionUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/CollectionUtils.java,v
retrieving revision 1.59
retrieving revision 1.60
diff -u -r1.59 -r1.60
--- CollectionUtils.java 1 Apr 2004 20:12:00 -0000 1.59
+++ CollectionUtils.java 1 Apr 2004 22:43:13 -0000 1.60
@@ -15,6 +15,7 @@
*/
package org.apache.commons.collections;
+import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
@@ -784,8 +785,8 @@
* <code>index</code> in the map's <code>entrySet</code> iterator,
* if there is such an entry.</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,
- * if there is such an entry; otherwise an
<code>ArrayIndexOutOfBoundsException</code>
+ * <li> Array -- the <code>index</code>-th array entry is returned,
+ * if there is such an entry; otherwise an
<code>IndexOutOfBoundsException</code>
* is thrown.</li>
* <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>
@@ -799,8 +800,8 @@
* @param object the object to get a value from
* @param index the index to get
* @return the object at the specified index
- * @throws IndexOutOfBoundsException
- * @throws IllegalArgumentException
+ * @throws IndexOutOfBoundsException if the index is invalid
+ * @throws IllegalArgumentException if the object type is invalid
*/
public static Object get(Object object, int index) {
if (index < 0) {
@@ -814,17 +815,6 @@
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 it.nextElement();
- } else {
- it.nextElement();
- }
- }
- throw new IndexOutOfBoundsException("Entry does not exist: " + index);
} else if (object instanceof Iterator) {
Iterator it = (Iterator) object;
while (it.hasNext()) {
@@ -839,9 +829,25 @@
} else if (object instanceof Collection) {
Iterator iterator = ((Collection) object).iterator();
return get(iterator, 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 == null) {
+ throw new IllegalArgumentException("Unsupported object type: null");
} else {
- throw new IllegalArgumentException("Unsupported object type: " +
- (object == null ? "null" : object.getClass().getName()));
+ try {
+ return Array.get(object, index);
+ } catch (IllegalArgumentException ex) {
+ throw new IllegalArgumentException("Unsupported object type: " +
object.getClass().getName());
+ }
}
}
@@ -852,7 +858,7 @@
* <ul>
* <li>Collection - the collection 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>Enumeration - the number of elements remaining in the enumeration
* </ul>
@@ -882,9 +888,14 @@
total++;
it.nextElement();
}
+ } else if (object == null) {
+ throw new IllegalArgumentException("Unsupported object type: null");
} else {
- throw new IllegalArgumentException("Unsupported object type: " +
- (object == null ? "null" : object.getClass().getName()));
+ try {
+ total = Array.getLength(object);
+ } catch (IllegalArgumentException ex) {
+ throw new IllegalArgumentException("Unsupported object type: " +
object.getClass().getName());
+ }
}
return total;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]