Added get(object, index) method to CollectionUtils, deprecating index(-,-) methods.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131235 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
03f4174238
commit
4b2811d1c4
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/CollectionUtils.java,v 1.44 2003/09/22 02:20:56 psteitz Exp $
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/CollectionUtils.java,v 1.45 2003/10/05 19:48:00 psteitz Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
|
@ -84,7 +84,7 @@ import org.apache.commons.collections.observed.ObservableCollection;
|
|||
* Provides utility methods and decorators for {@link Collection} instances.
|
||||
*
|
||||
* @since Commons Collections 1.0
|
||||
* @version $Revision: 1.44 $ $Date: 2003/09/22 02:20:56 $
|
||||
* @version $Revision: 1.45 $ $Date: 2003/10/05 19:48:00 $
|
||||
*
|
||||
* @author Rodney Waldhoff
|
||||
* @author Paul Jack
|
||||
|
@ -730,6 +730,8 @@ public class CollectionUtils {
|
|||
* @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 @@ public class CollectionUtils {
|
|||
* @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) {
|
||||
|
@ -814,6 +818,86 @@ public class CollectionUtils {
|
|||
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.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an Iterator for the given object. Currently this method can handle
|
||||
* Iterator, Enumeration, Collection, Map, Object[] or array.
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
* $Id: TestCollectionUtils.java,v 1.23 2003/10/02 22:14:29 scolebourne Exp $
|
||||
* $Revision: 1.23 $
|
||||
* $Date: 2003/10/02 22:14:29 $
|
||||
* $Id: TestCollectionUtils.java,v 1.24 2003/10/05 19:48:00 psteitz Exp $
|
||||
* $Revision: 1.24 $
|
||||
* $Date: 2003/10/05 19:48:00 $
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
|
@ -93,7 +93,7 @@ import org.apache.commons.collections.decorators.UnmodifiableCollection;
|
|||
* @author Stephen Colebourne
|
||||
* @author Phil Steitz
|
||||
*
|
||||
* @version $Revision: 1.23 $ $Date: 2003/10/02 22:14:29 $
|
||||
* @version $Revision: 1.24 $ $Date: 2003/10/05 19:48:00 $
|
||||
*/
|
||||
public class TestCollectionUtils extends TestCase {
|
||||
public TestCollectionUtils(String testName) {
|
||||
|
@ -594,6 +594,136 @@ public class TestCollectionUtils extends TestCase {
|
|||
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() {
|
||||
public boolean evaluate(Object input) {
|
||||
|
|
Loading…
Reference in New Issue