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

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

commit 9c99d7648c744309da5104fae0f373655bbf03cc
Author: Rishabh Kumar <[email protected]>
AuthorDate: Mon Mar 10 11:34:09 2025 +0530

    OAK-11565 : added util to replace Iterables.get in oak-commons
---
 .../oak/commons/collections/IterableUtils.java     | 20 ++++++++
 .../oak/commons/collections/IterableUtilsTest.java | 59 ++++++++++++++++++++++
 2 files changed, 79 insertions(+)

diff --git 
a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IterableUtils.java
 
b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IterableUtils.java
index 3857a854d9..6668f186df 100644
--- 
a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IterableUtils.java
+++ 
b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IterableUtils.java
@@ -408,4 +408,24 @@ public class IterableUtils {
         final Iterator<T> iterator = iterable.iterator();
         return iterator.hasNext() ? iterator.next() : defaultValue;
     }
+
+    /**
+     * Returns the element at the specified index in the specified iterable.
+     * <p>
+     * The iterable is traversed until the specified index is reached. If the
+     * position is greater than the number of elements in the iterable, an
+     * IndexOutOfBoundsException is thrown.
+     *
+     * @param <T> the type of elements in the iterable
+     * @param iterable the iterable to get the element from, may not be null
+     * @param index the index of the element to retrieve, must be non-negative
+     * @return the element at the specified index
+     * @throws NullPointerException if the iterable is null
+     * @throws IndexOutOfBoundsException if the position is negative or 
greater than
+     *         the number of elements in the iterable
+     */
+    public static <T> T get(final Iterable<T> iterable, final int index) {
+        Objects.requireNonNull(iterable, "Iterable must not be null.");
+        return org.apache.commons.collections4.IterableUtils.get(iterable, 
index);
+    }
 }
diff --git 
a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IterableUtilsTest.java
 
b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IterableUtilsTest.java
index 9b0637ca9a..55ad5ad59a 100644
--- 
a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IterableUtilsTest.java
+++ 
b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IterableUtilsTest.java
@@ -910,4 +910,63 @@ public class IterableUtilsTest {
         Integer result = IterableUtils.getFirst(customIterable, 0);
         Assert.assertEquals(Integer.valueOf(5), result);
     }
+
+    @Test
+    public void testGetWithValidPosition() {
+        List<String> list = Arrays.asList("a", "b", "c", "d", "e");
+        String result = IterableUtils.get(list, 2);
+        Assert.assertEquals("c", result);
+    }
+
+    @Test
+    public void testGetFirstElement() {
+        List<String> list = Arrays.asList("a", "b", "c");
+        String result = IterableUtils.get(list, 0);
+        Assert.assertEquals("a", result);
+    }
+
+    @Test
+    public void testGetLastElement() {
+        List<String> list = Arrays.asList("a", "b", "c");
+        String result = IterableUtils.get(list, 2);
+        Assert.assertEquals("c", result);
+    }
+
+    @Test(expected = IndexOutOfBoundsException.class)
+    public void testGetWithNegativePosition() {
+        List<String> list = Arrays.asList("a", "b", "c");
+        IterableUtils.get(list, -1);
+    }
+
+    @Test(expected = IndexOutOfBoundsException.class)
+    public void testGetWithPositionTooLarge() {
+        List<String> list = Arrays.asList("a", "b", "c");
+        IterableUtils.get(list, 3);
+    }
+
+    @Test(expected = IndexOutOfBoundsException.class)
+    public void testGetWithEmptyIterable() {
+        List<String> list = Collections.emptyList();
+        IterableUtils.get(list, 0);
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void testGetWithNullIterable() {
+        IterableUtils.get(null, 0);
+    }
+
+    @Test
+    public void testGetWithCustomIterable() {
+        // Custom iterable implementation
+        Iterable<Integer> customIterable = () -> Arrays.asList(5, 10, 15, 20, 
25).iterator();
+        Integer result = IterableUtils.get(customIterable, 3);
+        Assert.assertEquals(Integer.valueOf(20), result);
+    }
+
+    @Test
+    public void testGetWithSingleElementIterable() {
+        List<String> list = Collections.singletonList("only");
+        String result = IterableUtils.get(list, 0);
+        Assert.assertEquals("only", result);
+    }
 }

Reply via email to