Skip to content

Commit 449b735

Browse files
rishabhdaimRishabh Kumarreschke
authored
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 <reschke@apache.org> --------- Co-authored-by: Rishabh Kumar <diam@adobe.com> Co-authored-by: Julian Reschke <reschke@apache.org>
1 parent 0d391f6 commit 449b735

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,4 +408,24 @@ public static <T> T getFirst(final Iterable<T> iterable, final T defaultValue)
408408
final Iterator<T> iterator = iterable.iterator();
409409
return iterator.hasNext() ? iterator.next() : defaultValue;
410410
}
411+
412+
/**
413+
* Returns the element at the specified index in the specified iterable.
414+
* <p>
415+
* The iterable is traversed until the specified index is reached. If the
416+
* position is greater than the number of elements in the iterable, an
417+
* IndexOutOfBoundsException is thrown.
418+
*
419+
* @param <T> the type of elements in the iterable
420+
* @param iterable the iterable to get the element from, must not be null
421+
* @param index the index of the element to retrieve, must be non-negative
422+
* @return the element at the specified index
423+
* @throws NullPointerException if the iterable is null
424+
* @throws IndexOutOfBoundsException if the position is negative or greater than
425+
* the number of elements in the iterable
426+
*/
427+
public static <T> T get(final Iterable<T> iterable, final int index) {
428+
Objects.requireNonNull(iterable, "Iterable must not be null.");
429+
return org.apache.commons.collections4.IterableUtils.get(iterable, index);
430+
}
411431
}

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,4 +910,63 @@ public void testGetFirstWithCustomIterable() {
910910
Integer result = IterableUtils.getFirst(customIterable, 0);
911911
Assert.assertEquals(Integer.valueOf(5), result);
912912
}
913+
914+
@Test
915+
public void testGetWithValidPosition() {
916+
List<String> list = Arrays.asList("a", "b", "c", "d", "e");
917+
String result = IterableUtils.get(list, 2);
918+
Assert.assertEquals("c", result);
919+
}
920+
921+
@Test
922+
public void testGetFirstElement() {
923+
List<String> list = Arrays.asList("a", "b", "c");
924+
String result = IterableUtils.get(list, 0);
925+
Assert.assertEquals("a", result);
926+
}
927+
928+
@Test
929+
public void testGetLastElement() {
930+
List<String> list = Arrays.asList("a", "b", "c");
931+
String result = IterableUtils.get(list, 2);
932+
Assert.assertEquals("c", result);
933+
}
934+
935+
@Test(expected = IndexOutOfBoundsException.class)
936+
public void testGetWithNegativePosition() {
937+
List<String> list = Arrays.asList("a", "b", "c");
938+
IterableUtils.get(list, -1);
939+
}
940+
941+
@Test(expected = IndexOutOfBoundsException.class)
942+
public void testGetWithPositionTooLarge() {
943+
List<String> list = Arrays.asList("a", "b", "c");
944+
IterableUtils.get(list, 3);
945+
}
946+
947+
@Test(expected = IndexOutOfBoundsException.class)
948+
public void testGetWithEmptyIterable() {
949+
List<String> list = Collections.emptyList();
950+
IterableUtils.get(list, 0);
951+
}
952+
953+
@Test(expected = NullPointerException.class)
954+
public void testGetWithNullIterable() {
955+
IterableUtils.get(null, 0);
956+
}
957+
958+
@Test
959+
public void testGetWithCustomIterable() {
960+
// Custom iterable implementation
961+
Iterable<Integer> customIterable = () -> Arrays.asList(5, 10, 15, 20, 25).iterator();
962+
Integer result = IterableUtils.get(customIterable, 3);
963+
Assert.assertEquals(Integer.valueOf(20), result);
964+
}
965+
966+
@Test
967+
public void testGetWithSingleElementIterable() {
968+
List<String> list = Collections.singletonList("only");
969+
String result = IterableUtils.get(list, 0);
970+
Assert.assertEquals("only", result);
971+
}
913972
}

0 commit comments

Comments
 (0)