This is an automated email from the ASF dual-hosted git repository.

daim pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 34e7fd65b2 OAK-11587 : added Iterators.asEnumeration replacement in 
oak-commons (#2236)
34e7fd65b2 is described below

commit 34e7fd65b2815b3c4944fe4dc96032dedadb65a3
Author: Rishabh Kumar <rishabhdaim1...@gmail.com>
AuthorDate: Thu Apr 17 20:54:51 2025 +0530

    OAK-11587 : added Iterators.asEnumeration replacement in oak-commons (#2236)
    
    Co-authored-by: Rishabh Kumar <d...@adobe.com>
---
 .../oak/commons/collections/IteratorUtils.java     | 21 +++++-
 .../oak/commons/collections/IteratorUtilsTest.java | 77 ++++++++++++++++++++++
 2 files changed, 97 insertions(+), 1 deletion(-)

diff --git 
a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtils.java
 
b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtils.java
index 41a3300311..ab756ce8b1 100644
--- 
a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtils.java
+++ 
b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtils.java
@@ -22,6 +22,7 @@ import 
org.apache.commons.collections4.iterators.PeekingIterator;
 import org.jetbrains.annotations.NotNull;
 
 import java.util.Comparator;
+import java.util.Enumeration;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 import java.util.Objects;
@@ -262,9 +263,27 @@ public class IteratorUtils {
      * @param type     the {@link Class} object representing the component 
type of the array, must not be null
      * @return an array containing all the elements from the iterator
      * @throws NullPointerException if the iterator or type is null
-     * @since 4.1
      */
     public static <T> T[] toArray(Iterator<? extends T> iterator, Class<T> 
type) {
         return org.apache.commons.collections4.IteratorUtils.toArray(iterator, 
type);
     }
+
+    /**
+     * Converts an iterator to an enumeration.
+     * <p>
+     * This method creates an {@link Enumeration} that will use the provided 
{@link Iterator}
+     * as its source of elements. The enumeration will iterate through the 
same elements
+     * as the iterator in the same order.
+     * <p>
+     * The enumeration's {@code hasMoreElements()} and {@code nextElement()} 
methods
+     * delegate to the iterator's {@code hasNext()} and {@code next()} methods 
respectively.
+     *
+     * @param <T> the type of elements in the iterator and enumeration
+     * @param iterator the iterator to convert to an enumeration, must not be 
null
+     * @return an enumeration that uses the provided iterator as its source
+     * @throws NullPointerException if the iterator is null
+     */
+    public static <T> Enumeration<T> asEnumeration(final Iterator<T> iterator) 
{
+        return 
org.apache.commons.collections4.IteratorUtils.asEnumeration(iterator);
+    }
 }
diff --git 
a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtilsTest.java
 
b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtilsTest.java
index 3fed8a21e4..0b96cb8df5 100644
--- 
a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtilsTest.java
+++ 
b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtilsTest.java
@@ -25,9 +25,11 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Comparator;
+import java.util.Enumeration;
 import java.util.Iterator;
 import java.util.List;
 import java.util.NoSuchElementException;
+import java.util.Objects;
 
 import static org.junit.Assert.fail;
 
@@ -512,4 +514,79 @@ public class IteratorUtilsTest {
         Assert.assertEquals("first", array[0].toString());
         Assert.assertEquals("second", array[1].toString());
     }
+
+    @Test
+    public void testAsEnumerationWithMultipleElements() {
+        Iterator<String> iterator = Arrays.asList("one", "two", 
"three").iterator();
+        Enumeration<String> enumeration = 
IteratorUtils.asEnumeration(iterator);
+
+        Assert.assertTrue(enumeration.hasMoreElements());
+        Assert.assertEquals("one", enumeration.nextElement());
+        Assert.assertTrue(enumeration.hasMoreElements());
+        Assert.assertEquals("two", enumeration.nextElement());
+        Assert.assertTrue(enumeration.hasMoreElements());
+        Assert.assertEquals("three", enumeration.nextElement());
+        Assert.assertFalse(enumeration.hasMoreElements());
+    }
+
+    @Test
+    public void testAsEnumerationWithEmptyIterator() {
+        Iterator<String> emptyIterator = Collections.emptyIterator();
+        Enumeration<String> enumeration = 
IteratorUtils.asEnumeration(emptyIterator);
+
+        Assert.assertFalse(enumeration.hasMoreElements());
+    }
+
+    @Test
+    public void testAsEnumerationWithSingleElement() {
+        Iterator<Integer> singleElementIterator = 
Collections.singleton(42).iterator();
+        Enumeration<Integer> enumeration = 
IteratorUtils.asEnumeration(singleElementIterator);
+
+        Assert.assertTrue(enumeration.hasMoreElements());
+        Assert.assertEquals(Integer.valueOf(42), enumeration.nextElement());
+        Assert.assertFalse(enumeration.hasMoreElements());
+    }
+
+    @Test(expected = NoSuchElementException.class)
+    public void testAsEnumerationNoMoreElements() {
+        Iterator<String> iterator = 
Collections.singletonList("single").iterator();
+        Enumeration<String> enumeration = 
IteratorUtils.asEnumeration(iterator);
+
+        enumeration.nextElement(); // First element
+        enumeration.nextElement(); // Should throw NoSuchElementException
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void testAsEnumerationWithNullIterator() {
+        IteratorUtils.asEnumeration(null);
+    }
+
+    @Test
+    public void testAsEnumerationWithCustomType() {
+        class CustomObject {
+            private final String value;
+
+            CustomObject(String value) {
+                this.value = value;
+            }
+
+            @Override
+            public boolean equals(Object obj) {
+                if (this == obj) return true;
+                if (obj == null || getClass() != obj.getClass()) return false;
+                CustomObject that = (CustomObject) obj;
+                return Objects.equals(value, that.value);
+            }
+        }
+
+        List<CustomObject> list = Arrays.asList(new CustomObject("first"), new 
CustomObject("second"));
+        Iterator<CustomObject> iterator = list.iterator();
+        Enumeration<CustomObject> enumeration = 
IteratorUtils.asEnumeration(iterator);
+
+        Assert.assertTrue(enumeration.hasMoreElements());
+        Assert.assertEquals(list.get(0), enumeration.nextElement());
+        Assert.assertTrue(enumeration.hasMoreElements());
+        Assert.assertEquals(list.get(1), enumeration.nextElement());
+        Assert.assertFalse(enumeration.hasMoreElements());
+    }
 }

Reply via email to