[COLLECTIONS-780] Add

org.apache.commons.collections4.EnumerationUtils.asIterable(Enumeration).
This commit is contained in:
Gary Gregory 2020-01-12 10:32:00 -05:00
parent 45eee00385
commit 22560c3077
3 changed files with 48 additions and 2 deletions

View File

@ -108,6 +108,9 @@
<action dev="ggregory" type="fix" due-to="Dominik Stadler"> <action dev="ggregory" type="fix" due-to="Dominik Stadler">
Fix links to release notes and update contents for 4.4 #127. Fix links to release notes and update contents for 4.4 #127.
</action> </action>
<action issue="COLLECTIONS-780" dev="ggregory" type="add" due-to="Gary Gregory">
Add org.apache.commons.collections4.EnumerationUtils.asIterable(Enumeration).
</action>
</release> </release>
<release version="4.4" date="2019-07-05" description="Maintenance release."> <release version="4.4" date="2019-07-05" description="Maintenance release.">
<action issue="COLLECTIONS-710" dev="ggregory" type="fix" due-to="Yu Shi, Gary Gregory"> <action issue="COLLECTIONS-710" dev="ggregory" type="fix" due-to="Yu Shi, Gary Gregory">

View File

@ -22,6 +22,7 @@ import java.util.List;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import org.apache.commons.collections4.iterators.EnumerationIterator; import org.apache.commons.collections4.iterators.EnumerationIterator;
import org.apache.commons.collections4.iterators.IteratorIterable;
/** /**
* Provides utility methods for {@link Enumeration} instances. * Provides utility methods for {@link Enumeration} instances.
@ -33,7 +34,22 @@ public class EnumerationUtils {
/** /**
* EnumerationUtils is not normally instantiated. * EnumerationUtils is not normally instantiated.
*/ */
private EnumerationUtils() {} private EnumerationUtils() {
// no instances.
}
/**
* Creates an {@link Iterable} that wraps an {@link Enumeration}. The returned {@link Iterable} can be used for a
* single iteration.
*
* @param <T> the element type
* @param enumeration the enumeration to use, may not be null
* @return a new, single use {@link Iterable}
* @since 4.5
*/
public static <T> Iterable<T> asIterable(final Enumeration<T> enumeration) {
return new IteratorIterable<>(new EnumerationIterator<>(enumeration));
}
/** /**
* Returns the {@code index}-th value in the {@link Enumeration}, throwing * Returns the {@code index}-th value in the {@link Enumeration}, throwing
@ -51,8 +67,8 @@ public class EnumerationUtils {
* @since 4.1 * @since 4.1
*/ */
public static <T> T get(final Enumeration<T> e, final int index) { public static <T> T get(final Enumeration<T> e, final int index) {
CollectionUtils.checkIndexBounds(index);
int i = index; int i = index;
CollectionUtils.checkIndexBounds(i);
while (e.hasMoreElements()) { while (e.hasMoreElements()) {
i--; i--;
if (i == -1) { if (i == -1) {
@ -92,4 +108,5 @@ public class EnumerationUtils {
} }
return result; return result;
} }
} }

View File

@ -18,11 +18,13 @@ package org.apache.commons.collections4;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.Vector; import java.util.Vector;
@ -58,6 +60,30 @@ public class EnumerationUtilsTest {
assertTrue(!en.hasMoreElements()); assertTrue(!en.hasMoreElements());
} }
@Test
public void testAsIterableFor() {
final Vector<String> vector = new Vector<>();
vector.addElement("zero");
vector.addElement("one");
Enumeration<String> en = vector.elements();
final Iterator<String> iterator = EnumerationUtils.asIterable(en).iterator();
assertTrue(iterator.hasNext());
assertEquals("zero", iterator.next());
assertTrue(iterator.hasNext());
assertEquals("one", iterator.next());
assertFalse(iterator.hasNext());
}
@Test
public void testAsIterableForNull() {
try {
EnumerationUtils.asIterable((Enumeration) null).iterator().next();
fail("Expecting NullPointerException");
} catch (final NullPointerException ex) {
// success
}
}
@Test @Test
public void testToListWithHashtable() { public void testToListWithHashtable() {
final Hashtable<String, Integer> expected = new Hashtable<>(); final Hashtable<String, Integer> expected = new Hashtable<>();