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 9762770de4 OAK-11563 : added util to replace Iterables.getFirst in
oak-commons (#2150)
9762770de4 is described below
commit 9762770de4d60268446ef1922c4f05c390065baf
Author: Rishabh Kumar <[email protected]>
AuthorDate: Fri Mar 7 17:31:04 2025 +0530
OAK-11563 : added util to replace Iterables.getFirst in oak-commons (#2150)
Co-authored-by: Rishabh Kumar <[email protected]>
---
.../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);
+ }
}