Skip to content

Commit 9762770

Browse files
rishabhdaimRishabh Kumar
andauthored
OAK-11563 : added util to replace Iterables.getFirst in oak-commons (#2150)
Co-authored-by: Rishabh Kumar <[email protected]>
1 parent b3cb44e commit 9762770

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IterableUtils.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,4 +390,22 @@ public static <T> Iterable<T> limit(final Iterable<T> iterable, final int limitS
390390
public static String toString(final Iterable<?> iterable) {
391391
return org.apache.commons.collections4.IterableUtils.toString(iterable);
392392
}
393+
394+
/**
395+
* Returns the first element of the specified iterable, or the default value if the iterable is empty.
396+
* <p>
397+
* The iterable is only traversed enough to get the first element. If the iterable is empty,
398+
* the default value is returned instead.
399+
*
400+
* @param <T> the type of elements in the iterable
401+
* @param iterable the iterable to get the first element from, may be null
402+
* @param defaultValue the value to return if the iterable is empty
403+
* @return the first element in the iterable or the default value if the iterable is empty
404+
* @throws NullPointerException if the iterable is null
405+
*/
406+
public static <T> T getFirst(final Iterable<T> iterable, final T defaultValue) {
407+
Objects.requireNonNull(iterable, "Iterable must not be null.");
408+
final Iterator<T> iterator = iterable.iterator();
409+
return iterator.hasNext() ? iterator.next() : defaultValue;
410+
}
393411
}

oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IterableUtilsTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,4 +863,51 @@ public void testToStringWithSpecialCharacters() {
863863
String result = IterableUtils.toString(iterable);
864864
Assert.assertEquals("[a,b, c\"d, e\nf]", result);
865865
}
866+
867+
@Test
868+
public void testGetFirstWithNonEmptyIterable() {
869+
List<String> list = Arrays.asList("a", "b", "c");
870+
String result = IterableUtils.getFirst(list, "default");
871+
Assert.assertEquals("a", result);
872+
}
873+
874+
@Test
875+
public void testGetFirstWithEmptyIterable() {
876+
List<String> list = Collections.emptyList();
877+
String result = IterableUtils.getFirst(list, "default");
878+
Assert.assertEquals("default", result);
879+
}
880+
881+
@Test
882+
public void testGetFirstWithNullIterable() {
883+
Assert.assertThrows(NullPointerException.class, () -> IterableUtils.getFirst(null, "default"));
884+
}
885+
886+
@Test
887+
public void testGetFirstWithSingleElement() {
888+
List<Integer> list = Collections.singletonList(42);
889+
Integer result = IterableUtils.getFirst(list, 0);
890+
Assert.assertEquals(Integer.valueOf(42), result);
891+
}
892+
893+
@Test
894+
public void testGetFirstWithNullFirstElement() {
895+
List<String> list = Arrays.asList(null, "b", "c");
896+
String result = IterableUtils.getFirst(list, "default");
897+
Assert.assertNull(result);
898+
}
899+
900+
@Test
901+
public void testGetFirstWithNullDefaultValue() {
902+
List<String> list = Collections.emptyList();
903+
String result = IterableUtils.getFirst(list, null);
904+
Assert.assertNull(result);
905+
}
906+
907+
@Test
908+
public void testGetFirstWithCustomIterable() {
909+
Iterable<Integer> customIterable = () -> Arrays.asList(5, 10, 15).iterator();
910+
Integer result = IterableUtils.getFirst(customIterable, 0);
911+
Assert.assertEquals(Integer.valueOf(5), result);
912+
}
866913
}

0 commit comments

Comments
 (0)