mirror of
https://github.com/apache/commons-collections.git
synced 2025-02-17 23:44:48 +00:00
[COLLECTIONS-540] Added CollectionUtils#get(Enumeration, int), simplified code of CollectionUtils#get(Object, int). Thanks to Daniel Stewart, Issam El Atif. This closes #6.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1647955 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f0b18d27f0
commit
45a0337e1d
@ -22,6 +22,10 @@
|
||||
<body>
|
||||
|
||||
<release version="4.1" date="TBD" description="">
|
||||
<action issue="COLLECTIONS-540" dev="tn" type="fix" due-to="Daniel Stewart, Issam El Atif">
|
||||
Added overloaded method "CollectionUtils#get(Enumeration, int)" and simplified
|
||||
code for "CollectionUtils#get(Object, int)".
|
||||
</action>
|
||||
<action issue="COLLECTIONS-518" dev="tn" type="fix" due-to="Dipanjan Laha">
|
||||
The abstract decorator "AbstractIterableGetMapDecorator" was not declared
|
||||
abstract.
|
||||
|
@ -1198,6 +1198,35 @@ public class CollectionUtils {
|
||||
throw new IndexOutOfBoundsException("Entry does not exist: " + i);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the <code>index</code>-th value in the {@link Enumeration}, throwing
|
||||
* <code>IndexOutOfBoundsException</code> if there is no such element.
|
||||
* <p>
|
||||
* The 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.
|
||||
*
|
||||
* @param e the enumeration to get a value from
|
||||
* @param index the index to get
|
||||
* @param <T> the type of object in the {@link Enumeration}
|
||||
* @return the object at the specified index
|
||||
* @throws IndexOutOfBoundsException if the index is invalid
|
||||
* @throws IllegalArgumentException if the object type is invalid
|
||||
* @since 4.1
|
||||
*/
|
||||
public static <T> T get(final Enumeration<T> e, final int index) {
|
||||
int i = index;
|
||||
checkIndexBounds(i);
|
||||
while (e.hasMoreElements()) {
|
||||
i--;
|
||||
if (i == -1) {
|
||||
return e.nextElement();
|
||||
} else {
|
||||
e.nextElement();
|
||||
}
|
||||
}
|
||||
throw new IndexOutOfBoundsException("Entry does not exist: " + i);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures an index is not negative.
|
||||
* @param index the index to check.
|
||||
@ -1272,28 +1301,13 @@ public class CollectionUtils {
|
||||
return ((Object[]) object)[i];
|
||||
} else if (object instanceof Iterator<?>) {
|
||||
final Iterator<?> it = (Iterator<?>) object;
|
||||
while (it.hasNext()) {
|
||||
i--;
|
||||
if (i == -1) {
|
||||
return it.next();
|
||||
}
|
||||
it.next();
|
||||
}
|
||||
throw new IndexOutOfBoundsException("Entry does not exist: " + i);
|
||||
return get(it, i);
|
||||
} else if (object instanceof Collection<?>) {
|
||||
final Iterator<?> iterator = ((Collection<?>) object).iterator();
|
||||
return get(iterator, i);
|
||||
} else if (object instanceof Enumeration<?>) {
|
||||
final Enumeration<?> it = (Enumeration<?>) object;
|
||||
while (it.hasMoreElements()) {
|
||||
i--;
|
||||
if (i == -1) {
|
||||
return it.nextElement();
|
||||
} else {
|
||||
it.nextElement();
|
||||
}
|
||||
}
|
||||
throw new IndexOutOfBoundsException("Entry does not exist: " + i);
|
||||
return get(it, i);
|
||||
} else if (object == null) {
|
||||
throw new IllegalArgumentException("Unsupported object type: null");
|
||||
} else {
|
||||
|
@ -1600,8 +1600,26 @@ public class CollectionUtilsTest extends MockTestCase {
|
||||
assertEquals(2, CollectionUtils.get((Object)collectionA, 2));
|
||||
assertEquals(2, CollectionUtils.get((Object)collectionA.iterator(), 2));
|
||||
final Map<Integer, Integer> map = CollectionUtils.getCardinalityMap(collectionA);
|
||||
assertEquals(map.entrySet().iterator().next(), CollectionUtils.get(
|
||||
(Object)map, 0));
|
||||
assertEquals(map.entrySet().iterator().next(), CollectionUtils.get((Object)map, 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getIterator() {
|
||||
final Iterator<Integer> it = collectionA.iterator();
|
||||
assertEquals(Integer.valueOf(2), CollectionUtils.get(it, 2));
|
||||
assertTrue(it.hasNext());
|
||||
assertEquals(Integer.valueOf(4), CollectionUtils.get(it, 6));
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getEnumeration() {
|
||||
final Vector<Integer> vectorA = new Vector<Integer>(collectionA);
|
||||
final Enumeration<Integer> e = vectorA.elements();
|
||||
assertEquals(Integer.valueOf(2), CollectionUtils.get(e, 2));
|
||||
assertTrue(e.hasMoreElements());
|
||||
assertEquals(Integer.valueOf(4), CollectionUtils.get(e, 6));
|
||||
assertFalse(e.hasMoreElements());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
x
Reference in New Issue
Block a user