This is an automated email from the ASF dual-hosted git repository. daim pushed a commit to branch OAK-11563 in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
commit ea4190e034bfebce2ad583ca05c54b886a6a3f68 Author: Rishabh Kumar <[email protected]> AuthorDate: Fri Mar 7 15:17:54 2025 +0530 OAK-11563 : added util to replace Iterables.getFirst in oak-commons --- .../oak/commons/collections/IterableUtils.java | 18 +++++++++ .../oak/commons/collections/IterableUtilsTest.java | 47 ++++++++++++++++++++++ 2 files changed, 65 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 c3541b6066..3857a854d9 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 @@ -390,4 +390,22 @@ public class IterableUtils { public static String toString(final Iterable<?> iterable) { return org.apache.commons.collections4.IterableUtils.toString(iterable); } + + /** + * Returns the first element of the specified iterable, or the default value if the iterable is empty. + * <p> + * The iterable is only traversed enough to get the first element. If the iterable is empty, + * the default value is returned instead. + * + * @param <T> the type of elements in the iterable + * @param iterable the iterable to get the first element from, may be null + * @param defaultValue the value to return if the iterable is empty + * @return the first element in the iterable or the default value if the iterable is empty + * @throws NullPointerException if the iterable is null + */ + public static <T> T getFirst(final Iterable<T> iterable, final T defaultValue) { + Objects.requireNonNull(iterable, "Iterable must not be null."); + final Iterator<T> iterator = iterable.iterator(); + return iterator.hasNext() ? iterator.next() : defaultValue; + } } 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 916ffd6955..9b0637ca9a 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 @@ -863,4 +863,51 @@ public class IterableUtilsTest { String result = IterableUtils.toString(iterable); Assert.assertEquals("[a,b, c\"d, e\nf]", result); } + + @Test + public void testGetFirstWithNonEmptyIterable() { + List<String> list = Arrays.asList("a", "b", "c"); + String result = IterableUtils.getFirst(list, "default"); + Assert.assertEquals("a", result); + } + + @Test + public void testGetFirstWithEmptyIterable() { + List<String> list = Collections.emptyList(); + String result = IterableUtils.getFirst(list, "default"); + Assert.assertEquals("default", result); + } + + @Test + public void testGetFirstWithNullIterable() { + Assert.assertThrows(NullPointerException.class, () -> IterableUtils.getFirst(null, "default")); + } + + @Test + public void testGetFirstWithSingleElement() { + List<Integer> list = Collections.singletonList(42); + Integer result = IterableUtils.getFirst(list, 0); + Assert.assertEquals(Integer.valueOf(42), result); + } + + @Test + public void testGetFirstWithNullFirstElement() { + List<String> list = Arrays.asList(null, "b", "c"); + String result = IterableUtils.getFirst(list, "default"); + Assert.assertNull(result); + } + + @Test + public void testGetFirstWithNullDefaultValue() { + List<String> list = Collections.emptyList(); + String result = IterableUtils.getFirst(list, null); + Assert.assertNull(result); + } + + @Test + public void testGetFirstWithCustomIterable() { + Iterable<Integer> customIterable = () -> Arrays.asList(5, 10, 15).iterator(); + Integer result = IterableUtils.getFirst(customIterable, 0); + Assert.assertEquals(Integer.valueOf(5), result); + } }
