Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 49297 invoked from network); 5 Oct 2003 19:48:05 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 5 Oct 2003 19:48:05 -0000 Received: (qmail 34733 invoked by uid 500); 5 Oct 2003 19:47:52 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 34507 invoked by uid 500); 5 Oct 2003 19:47:50 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 34494 invoked by uid 500); 5 Oct 2003 19:47:50 -0000 Received: (qmail 34490 invoked from network); 5 Oct 2003 19:47:50 -0000 Received: from unknown (HELO minotaur.apache.org) (209.237.227.194) by daedalus.apache.org with SMTP; 5 Oct 2003 19:47:50 -0000 Received: (qmail 49218 invoked by uid 1718); 5 Oct 2003 19:48:00 -0000 Date: 5 Oct 2003 19:48:00 -0000 Message-ID: <20031005194800.49217.qmail@minotaur.apache.org> From: psteitz@apache.org To: jakarta-commons-cvs@apache.org Subject: cvs commit: jakarta-commons/collections/src/test/org/apache/commons/collections TestCollectionUtils.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N 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 index-th value in object, throwing + * IndexOutOfBoundsException if there is no such element or + * IllegalArgumentException if object is not an + * instance of one of the supported types. + *

+ * The supported types, and associated semantics are: + *

    + *
  • Map -- the value returned is the Map.Entry in position + * index in the map's entrySet iterator, + * if there is such an entry.
  • + *
  • List -- this method is equivalent to the list's get method.
  • + *
  • Object Array -- the index-th array entry is returned, + * if there is such an entry; otherwise an ArrayIndexOutOfBoundsException + * is thrown.
  • + *
  • Collection -- the value returned is the index-th object + * returned by the collection's default iterator, if there is such an element.
  • + *
  • Iterator or Enumeration -- the value returned is the + * index-th object in the Iterator/Enumeration, if there + * is such an element. The Iterator/Enumeration is advanced to + * index (or to the end, if index exceeds the + * number of entries) as a side effect of this method.
  • + *
+ * + * @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