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


The following commit(s) were added to refs/heads/master by this push:
     new 3e22618df Add EnumerationUtils.toSet(Enumeration)
3e22618df is described below

commit 3e22618df9583fa3e8716768d76dc738b8710564
Author: Gary D. Gregory <[email protected]>
AuthorDate: Sun Mar 16 15:36:19 2025 -0400

    Add EnumerationUtils.toSet(Enumeration)
---
 src/changes/changes.xml                            |  1 +
 .../commons/collections4/EnumerationUtils.java     | 17 +++++++++++
 .../commons/collections4/EnumerationUtilsTest.java | 33 ++++++++++++++++++++++
 3 files changed, 51 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 05df0d206..9bf82e908 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -29,6 +29,7 @@
     <!-- ADD -->
     <action type="add" dev="ggregory" due-to="Gary Gregory">Add 
IteratorUtils.toSet(Iterator).</action>
     <action type="add" dev="ggregory" due-to="Gary Gregory">Add 
IteratorUtils.toSet(Iterator, int).</action>
+    <action type="add" dev="ggregory" due-to="Gary Gregory">Add 
EnumerationUtils.toSet(Enumeration).</action>
     <!-- UPDATE -->
     <action type="update" dev="ggregory" due-to="Gary Gregory">Bump 
commons-codec:commons-codec from 1.17.1 to 1.18.0 #591.</action>
     <action type="update" dev="ggregory" due-to="Gary Gregory, 
Dependabot">Bump org.apache.commons:commons-parent from 78 to 81 #2296.</action>
diff --git 
a/src/main/java/org/apache/commons/collections4/EnumerationUtils.java 
b/src/main/java/org/apache/commons/collections4/EnumerationUtils.java
index ca872776f..2d8365982 100644
--- a/src/main/java/org/apache/commons/collections4/EnumerationUtils.java
+++ b/src/main/java/org/apache/commons/collections4/EnumerationUtils.java
@@ -19,6 +19,7 @@ package org.apache.commons.collections4;
 import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.List;
+import java.util.Set;
 import java.util.StringTokenizer;
 
 import org.apache.commons.collections4.iterators.EnumerationIterator;
@@ -103,6 +104,22 @@ public class EnumerationUtils {
         return result;
     }
 
+    /**
+     * Creates a set based on an enumeration.
+     *
+     * <p>As the enumeration is traversed, an HashSet of its values is
+     * created. The new set is returned.</p>
+     *
+     * @param <E> the element type
+     * @param enumeration  the enumeration to traverse, which should not be 
{@code null}.
+     * @return a set containing all elements of the given enumeration.
+     * @throws NullPointerException if the enumeration parameter is {@code 
null}.
+     * @since 4.5.0-M4
+     */
+    public static <E> Set<E> toSet(final Enumeration<? extends E> enumeration) 
{
+        return IteratorUtils.toSet(new EnumerationIterator<>(enumeration));
+    }
+
     /**
      * Don't allow instances.
      */
diff --git 
a/src/test/java/org/apache/commons/collections4/EnumerationUtilsTest.java 
b/src/test/java/org/apache/commons/collections4/EnumerationUtilsTest.java
index 00d28520f..137af1f9e 100644
--- a/src/test/java/org/apache/commons/collections4/EnumerationUtilsTest.java
+++ b/src/test/java/org/apache/commons/collections4/EnumerationUtilsTest.java
@@ -23,9 +23,11 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.util.ArrayList;
 import java.util.Enumeration;
+import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Set;
 import java.util.StringTokenizer;
 import java.util.Vector;
 
@@ -124,4 +126,35 @@ public class EnumerationUtilsTest {
         assertEquals(expectedList2, actualList);
     }
 
+    @Test
+    public void testToSetWithHashtable() {
+        final Hashtable<String, Integer> expected = new Hashtable<>();
+        expected.put("one", Integer.valueOf(1));
+        expected.put("two", Integer.valueOf(2));
+        expected.put("three", Integer.valueOf(3));
+        // validate elements.
+        final Set<Integer> actualEltSet = 
EnumerationUtils.toSet(expected.elements());
+        assertEquals(expected.size(), actualEltSet.size());
+        assertTrue(actualEltSet.contains(Integer.valueOf(1)));
+        assertTrue(actualEltSet.contains(Integer.valueOf(2)));
+        assertTrue(actualEltSet.contains(Integer.valueOf(3)));
+        final Set<Integer> expectedEltList = new HashSet<>();
+        expectedEltList.add(Integer.valueOf(1));
+        expectedEltList.add(Integer.valueOf(2));
+        expectedEltList.add(Integer.valueOf(3));
+        assertTrue(actualEltSet.containsAll(expectedEltList));
+
+        // validate keys.
+        final Set<String> actualKeySet = 
EnumerationUtils.toSet(expected.keys());
+        assertEquals(expected.size(), actualEltSet.size());
+        assertTrue(actualKeySet.contains("one"));
+        assertTrue(actualKeySet.contains("two"));
+        assertTrue(actualKeySet.contains("three"));
+        final Set<String> expectedKeySet = new HashSet<>();
+        expectedKeySet.add("one");
+        expectedKeySet.add("two");
+        expectedKeySet.add("three");
+        assertTrue(actualKeySet.containsAll(expectedKeySet));
+    }
+
 }

Reply via email to