You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ps...@apache.org on 2003/10/05 21:48:00 UTC
cvs commit: jakarta-commons/collections/src/test/org/apache/commons/collections TestCollectionUtils.java
psteitz 2003/10/05 12:48:00
Modified: collections/src/java/org/apache/commons/collections
CollectionUtils.java
collections/src/test/org/apache/commons/collections
TestCollectionUtils.java
Log:
Added get(object, index) method to CollectionUtils, deprecating index(-,-) methods.
Revision Changes Path
1.45 +86 -2 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.44
retrieving revision 1.45
diff -u -r1.44 -r1.45
--- CollectionUtils.java 22 Sep 2003 02:20:56 -0000 1.44
+++ CollectionUtils.java 5 Oct 2003 19:48:00 -0000 1.45
@@ -730,6 +730,8 @@
* @param idx the index to get
* @throws IndexOutOfBoundsException
* @throws ArrayIndexOutOfBoundsException
+ *
+ * @deprecated use {@link #get(Object, int)} instead
*/
public static Object index(Object obj, int idx) {
return index(obj, new Integer(idx));
@@ -755,6 +757,8 @@
* @return the object at the specified index
* @throws IndexOutOfBoundsException
* @throws ArrayIndexOutOfBoundsException
+ *
+ * @deprecated use {@link #get(Object, int)} instead
*/
public static Object index(Object obj, Object index) {
if(obj instanceof Map) {
@@ -812,6 +816,86 @@
}
}
return iterator;
+ }
+
+ /**
+ * Returns the <code>index</code>-th value in <code>object</code>, throwing
+ * <code>IndexOutOfBoundsException</code> if there is no such element or
+ * <code>IllegalArgumentException</code> if <code>object</code> is not an
+ * instance of one of the supported types.
+ * <p>
+ * The supported types, and associated semantics are:
+ * <ul>
+ * <li> Map -- the value returned is the <code>Map.Entry</code> in position
+ * <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>
+ * 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>
+ * <li> Iterator or Enumeration -- the value returned is the
+ * <code>index</code>-th object in the Iterator/Enumeration, if there
+ * is such an element. The Iterator/Enumeration is advanced to
+ * <code>index</code> (or to the end, if <code>index</code> exceeds the
+ * number of entries) as a side effect of this method.</li>
+ * </ul>
+ *
+ * @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
+ */
+ public static Object get(Object object, int index) {
+ if (index < 0) {
+ throw new IndexOutOfBoundsException("Index cannot be negative.");
+ }
+ 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()) {
+ index--;
+ if(index == -1) {
+ return enum.nextElement();
+ } else {
+ enum.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();
+ return get(iterator, index);
+ } else {
+ throw new IllegalArgumentException("Unsupported object type.");
+ }
+ }
+
+ 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.");
}
/**
1.24 +134 -4 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.23
retrieving revision 1.24
diff -u -r1.23 -r1.24
--- TestCollectionUtils.java 2 Oct 2003 22:14:29 -0000 1.23
+++ TestCollectionUtils.java 5 Oct 2003 19:48:00 -0000 1.24
@@ -593,6 +593,136 @@
test = CollectionUtils.index(obj, obj);
assertTrue(test.equals(obj));
}
+
+ public void testGet() {
+ // Unordered map, entries exist
+ Map map = new HashMap();
+ map.put("zeroKey", "zero");
+ map.put("oneKey", "one");
+ Object test = CollectionUtils.get(map, 0);
+ assertTrue(((Map.Entry) test).getKey().equals("zeroKey"));
+ assertTrue(((Map.Entry) test).getValue().equals("zero"));
+ test = CollectionUtils.get(map, 1);
+ assertTrue(((Map.Entry) test).getKey().equals("oneKey"));
+ assertTrue(((Map.Entry) test).getValue().equals("one"));
+
+ // Map index out of range
+ try {
+ test = CollectionUtils.get(map, 2);
+ fail("Expecting IndexOutOfBoundsException.");
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ test = CollectionUtils.get(map, -2);
+ fail("Expecting IndexOutOfBoundsException.");
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+
+ // Sorted map, entries exist, should respect order
+ SortedMap map2 = new TreeMap();
+ map2.put("zeroKey", "zero");
+ map2.put("oneKey", "one");
+ test = CollectionUtils.get(map2, 1);
+ assertTrue(((Map.Entry) test).getKey().equals("zeroKey"));
+ assertTrue(((Map.Entry) test).getValue().equals("zero"));
+ test = CollectionUtils.get(map2, 0);
+ assertTrue(((Map.Entry) test).getKey().equals("oneKey"));
+ assertTrue(((Map.Entry) test).getValue().equals("one"));
+
+ // List, entry exists
+ List list = new ArrayList();
+ list.add("zero");
+ list.add("one");
+ test = CollectionUtils.get(list, 0);
+ assertTrue(test.equals("zero"));
+ test = CollectionUtils.get(list, 1);
+ assertTrue(test.equals("one"));
+
+ // list, non-existent entry -- IndexOutOfBoundsException
+ try {
+ test = CollectionUtils.index(list, 2);
+ fail("Expecting IndexOutOfBoundsException");
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+
+ // Iterator, entry exists
+ Iterator iterator = list.iterator();
+ test = CollectionUtils.get(iterator,0);
+ assertTrue(test.equals("zero"));
+ iterator = list.iterator();
+ test = CollectionUtils.get(iterator,1);
+ assertTrue(test.equals("one"));
+
+ // Iterator, non-existent entry
+ try {
+ test = CollectionUtils.get(iterator,3);
+ fail("Expecting IndexOutOfBoundsException.");
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ assertTrue(!iterator.hasNext());
+
+ // Enumeration, entry exists
+ Vector vector = new Vector(list);
+ Enumeration enum = vector.elements();
+ test = CollectionUtils.get(enum,0);
+ assertTrue(test.equals("zero"));
+ enum = vector.elements();
+ test = CollectionUtils.get(enum,1);
+ assertTrue(test.equals("one"));
+
+ // Enumerator, non-existent entry
+ try {
+ test = CollectionUtils.get(enum,3);
+ fail("Expecting IndexOutOfBoundsException.");
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ assertTrue(!enum.hasMoreElements());
+
+ // Collection, entry exists
+ Bag bag = new HashBag();
+ bag.add("element", 1);
+ test = CollectionUtils.get(bag, 0);
+ assertTrue(test.equals("element"));
+
+ // Collection, non-existent entry
+ try {
+ test = CollectionUtils.get(bag, 1);
+ fail("Expceting IndexOutOfBoundsException.");
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+
+ // Object array, entry exists
+ Object[] objArray = new Object[2];
+ objArray[0] = "zero";
+ objArray[1] = "one";
+ test = CollectionUtils.get(objArray,0);
+ assertTrue(test.equals("zero"));
+ test = CollectionUtils.get(objArray,1);
+ assertTrue(test.equals("one"));
+
+ // Object array, non-existent entry -- ArrayIndexOutOfBoundsException
+ try {
+ test = CollectionUtils.get(objArray,2);
+ fail("Expecting ArrayIndexOutOfBoundsException.");
+ } catch (ArrayIndexOutOfBoundsException ex) {
+ // expected
+ }
+
+ // Invalid object
+ Object obj = new Object();
+ try {
+ test = CollectionUtils.get(obj, 0);
+ fail("Expecting IllegalArgumentException.");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ }
private static Predicate EQUALS_TWO = new Predicate() {
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org