diff --git a/src/changes/changes.xml b/src/changes/changes.xml index c2e2453a8..a3be7b579 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -108,6 +108,9 @@ Fix links to release notes and update contents for 4.4 #127. + + Add org.apache.commons.collections4.EnumerationUtils.asIterable(Enumeration). + diff --git a/src/main/java/org/apache/commons/collections4/EnumerationUtils.java b/src/main/java/org/apache/commons/collections4/EnumerationUtils.java index 224ab53c3..8918f67f6 100644 --- a/src/main/java/org/apache/commons/collections4/EnumerationUtils.java +++ b/src/main/java/org/apache/commons/collections4/EnumerationUtils.java @@ -22,6 +22,7 @@ import java.util.List; import java.util.StringTokenizer; import org.apache.commons.collections4.iterators.EnumerationIterator; +import org.apache.commons.collections4.iterators.IteratorIterable; /** * Provides utility methods for {@link Enumeration} instances. @@ -33,7 +34,22 @@ public class EnumerationUtils { /** * 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 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 Iterable asIterable(final Enumeration enumeration) { + return new IteratorIterable<>(new EnumerationIterator<>(enumeration)); + } /** * Returns the {@code index}-th value in the {@link Enumeration}, throwing @@ -51,8 +67,8 @@ public class EnumerationUtils { * @since 4.1 */ public static T get(final Enumeration e, final int index) { + CollectionUtils.checkIndexBounds(index); int i = index; - CollectionUtils.checkIndexBounds(i); while (e.hasMoreElements()) { i--; if (i == -1) { @@ -92,4 +108,5 @@ public class EnumerationUtils { } return result; } + } diff --git a/src/test/java/org/apache/commons/collections4/EnumerationUtilsTest.java b/src/test/java/org/apache/commons/collections4/EnumerationUtilsTest.java index c263c7980..d4e0577ef 100644 --- a/src/test/java/org/apache/commons/collections4/EnumerationUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/EnumerationUtilsTest.java @@ -18,11 +18,13 @@ package org.apache.commons.collections4; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.fail; import java.util.ArrayList; import java.util.Enumeration; import java.util.Hashtable; +import java.util.Iterator; import java.util.List; import java.util.StringTokenizer; import java.util.Vector; @@ -58,6 +60,30 @@ public class EnumerationUtilsTest { assertTrue(!en.hasMoreElements()); } + @Test + public void testAsIterableFor() { + final Vector vector = new Vector<>(); + vector.addElement("zero"); + vector.addElement("one"); + Enumeration en = vector.elements(); + final Iterator 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 public void testToListWithHashtable() { final Hashtable expected = new Hashtable<>();