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 449b735a10 OAK-11565 : added util to replace Iterables.get in
oak-commons (#2158)
449b735a10 is described below
commit 449b735a10b59655617df9ab05c1944407b51a85
Author: Rishabh Kumar <[email protected]>
AuthorDate: Mon Mar 10 16:35:17 2025 +0530
OAK-11565 : added util to replace Iterables.get in oak-commons (#2158)
* OAK-11565 : added util to replace Iterables.get in oak-commons
* Update
oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IterableUtils.java
Co-authored-by: Julian Reschke <[email protected]>
---------
Co-authored-by: Rishabh Kumar <[email protected]>
Co-authored-by: Julian Reschke <[email protected]>
---
.../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..14e0c28831 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, must 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);
+ }
}