You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/01/12 15:32:06 UTC
[commons-collections] 03/03: [COLLECTIONS-780] Add
org.apache.commons.collections4.EnumerationUtils.asIterable(Enumeration).
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-collections.git
commit 22560c3077cf042f546428db6462c371bf9a2fa7
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jan 12 10:32:00 2020 -0500
[COLLECTIONS-780] Add
org.apache.commons.collections4.EnumerationUtils.asIterable(Enumeration).
---
src/changes/changes.xml | 3 +++
.../commons/collections4/EnumerationUtils.java | 21 +++++++++++++++--
.../commons/collections4/EnumerationUtilsTest.java | 26 ++++++++++++++++++++++
3 files changed, 48 insertions(+), 2 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index c2e2453..a3be7b5 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -108,6 +108,9 @@
<action dev="ggregory" type="fix" due-to="Dominik Stadler">
Fix links to release notes and update contents for 4.4 #127.
</action>
+ <action issue="COLLECTIONS-780" dev="ggregory" type="add" due-to="Gary Gregory">
+ Add org.apache.commons.collections4.EnumerationUtils.asIterable(Enumeration).
+ </action>
</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">
diff --git a/src/main/java/org/apache/commons/collections4/EnumerationUtils.java b/src/main/java/org/apache/commons/collections4/EnumerationUtils.java
index 224ab53..8918f67 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 <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
@@ -51,8 +67,8 @@ public class EnumerationUtils {
* @since 4.1
*/
public static <T> T get(final Enumeration<T> 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 c263c79..d4e0577 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;
@@ -59,6 +61,30 @@ public class EnumerationUtilsTest {
}
@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
public void testToListWithHashtable() {
final Hashtable<String, Integer> expected = new Hashtable<>();
expected.put("one", Integer.valueOf(1));