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 308965d9a0 OAK-11583: add get api in IteratorUtils (#2171)
308965d9a0 is described below

commit 308965d9a01c8b5435d951d29c63f9b29afd08e0
Author: Tushar <[email protected]>
AuthorDate: Wed Mar 12 16:23:02 2025 +0530

    OAK-11583: add get api in IteratorUtils (#2171)
---
 .../oak/commons/collections/IteratorUtils.java     | 18 +++++
 .../oak/commons/collections/IteratorUtilsTest.java | 79 ++++++++++++++++++++++
 2 files changed, 97 insertions(+)

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 dafe7f96cb..6e65feff65 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
@@ -190,4 +190,22 @@ public class IteratorUtils {
     public static int size(Iterator<?> iterator) {
         return org.apache.commons.collections4.IteratorUtils.size(iterator);
     }
+
+    /**
+     * Returns the element at the specified position in the iterator.
+     * <p>
+     * This method will consume the iterator up to the specified position.
+     * <p>
+     * @param <T> the type of elements in the iterator
+     * @param iterator the iterator to get the element from, must not be null
+     * @param index the position of the element to return, zero-based
+     * @return the element at the specified position
+     * @throws NullPointerException if the iterator is null
+     * @throws IndexOutOfBoundsException if the iterator is empty or index is 
negative or greater than the number
+     * of elements in the iterator
+     */
+    public static <T> T get(Iterator<T> iterator, int index) {
+        Objects.requireNonNull(iterator, "Iterator must not be null");
+        return org.apache.commons.collections4.IteratorUtils.get(iterator, 
index);
+    }
 }
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 6b1258ce0e..4fb8ad3a83 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
@@ -286,4 +286,83 @@ public class IteratorUtilsTest {
         Assert.assertEquals(10, IteratorUtils.size(customIterator));
         Assert.assertFalse("Iterator should be consumed after size operation", 
customIterator.hasNext());
     }
+
+    @Test
+    public void testGetFirstElement() {
+        Iterator<String> iterator = Arrays.asList("a", "b", "c").iterator();
+        Assert.assertEquals("a", IteratorUtils.get(iterator, 0));
+        // iterator should be at position 1
+        Assert.assertEquals("b", iterator.next());
+
+    }
+
+    @Test
+    public void testGetMiddleElement() {
+        Iterator<Integer> iterator = Arrays.asList(1, 2, 3).iterator();
+        Assert.assertEquals(Integer.valueOf(2), IteratorUtils.get(iterator, 
1));
+        // iterator should be at position 2
+        Assert.assertEquals(Integer.valueOf(3), iterator.next());
+    }
+
+    @Test
+    public void testGetLastElement() {
+        Iterator<String> iterator = Arrays.asList("a", "b", "c").iterator();
+        Assert.assertEquals("c", IteratorUtils.get(iterator, 2));
+        Assert.assertFalse(iterator.hasNext());
+    }
+
+    @Test
+    public void testGetWithNullIterator() {
+        Assert.assertThrows(NullPointerException.class, () -> {
+            IteratorUtils.get(null, 0);
+        });
+    }
+
+    @Test
+    public void testGetWithNegativeIndex() {
+        List<String> data = Arrays.asList("a", "b", "c");
+        Assert.assertThrows(IndexOutOfBoundsException.class, () -> {
+            IteratorUtils.get(data.iterator(), -1);
+        });
+    }
+
+    @Test
+    public void testWithIndexGreaterThanSizeOfIterator() {
+        List<String> data = Arrays.asList("a", "b", "c");
+        Assert.assertThrows(IndexOutOfBoundsException.class, () -> {
+            IteratorUtils.get(data.iterator(), 3);
+        });
+    }
+
+    @Test
+    public void testGetWithEmptyIterator() {
+        Assert.assertThrows(IndexOutOfBoundsException.class, () -> {
+            IteratorUtils.get(Collections.emptyIterator(), 0);
+        });
+    }
+
+    @Test
+    public void testGetWithCustomObject() {
+        class TestObject {
+            private final String value;
+
+            TestObject(String value) {
+                this.value = value;
+            }
+
+            @Override
+            public String toString() {
+                return value;
+            }
+        }
+
+        List<TestObject> data = Arrays.asList(
+                new TestObject("obj1"),
+                new TestObject("obj2"),
+                new TestObject("obj3")
+        );
+
+        TestObject result = IteratorUtils.get(data.iterator(), 1);
+        Assert.assertEquals("obj2", result.toString());
+    }
 }

Reply via email to