Skip to content

Commit 2e2311c

Browse files
authored
OAK-11584 : add getLast() method in IteratorUtils (#2188)
* OAK-11584: add getLast() method in IteratorUtils * OAK-11584: add test cases for getLast()
1 parent 6a2ae9a commit 2e2311c

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,26 @@ public static <T> T get(Iterator<T> iterator, int index) {
208208
Objects.requireNonNull(iterator, "Iterator must not be null");
209209
return org.apache.commons.collections4.IteratorUtils.get(iterator, index);
210210
}
211+
212+
/**
213+
* Returns the last element in the given iterator.
214+
* <p>
215+
* This method consumes the entire iterator to find the last element.
216+
* <p>
217+
*
218+
* @param <T> the type of elements in the iterator
219+
* @param iterator the iterator to get the last element from, must not be null
220+
* @return the last element in the iterator
221+
* @throws NullPointerException if the iterator is null
222+
* @throws NoSuchElementException if the iterator is empty
223+
*/
224+
public static <T> T getLast(Iterator<T> iterator) {
225+
Objects.requireNonNull(iterator, "Iterator must not be null");
226+
while (true) {
227+
T currentElement = iterator.next();
228+
if (!iterator.hasNext()) {
229+
return currentElement;
230+
}
231+
}
232+
}
211233
}

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,53 @@ public String toString() {
365365
TestObject result = IteratorUtils.get(data.iterator(), 1);
366366
Assert.assertEquals("obj2", result.toString());
367367
}
368+
369+
@Test
370+
public void testGetLastWithMultipleElements() {
371+
Iterator<String> iterator = Arrays.asList("one", "two", "three").iterator();
372+
Assert.assertEquals("three", IteratorUtils.getLast(iterator));
373+
Assert.assertFalse(iterator.hasNext());
374+
}
375+
376+
@Test
377+
public void testGetLastWithSingleElement() {
378+
Iterator<Integer> intIterator = List.of(1).iterator();
379+
Assert.assertEquals(Integer.valueOf(1), IteratorUtils.getLast(intIterator));
380+
Assert.assertFalse(intIterator.hasNext());
381+
}
382+
383+
@Test(expected = NullPointerException.class)
384+
public void testGetLastWithNullIterator() {
385+
IteratorUtils.getLast(null);
386+
}
387+
388+
@Test(expected = NoSuchElementException.class)
389+
public void testGetLastWithEmptyIterator() {
390+
IteratorUtils.getLast(Collections.emptyIterator());
391+
}
392+
393+
@Test
394+
public void testGetLastWithDifferentTypes() {
395+
class TestObject {
396+
private final String value;
397+
398+
TestObject(String value) {
399+
this.value = value;
400+
}
401+
402+
@Override
403+
public boolean equals(Object o) {
404+
if (this == o) return true;
405+
if (o == null || getClass() != o.getClass()) return false;
406+
TestObject that = (TestObject) o;
407+
return value.equals(that.value);
408+
}
409+
}
410+
411+
TestObject obj1 = new TestObject("first");
412+
TestObject obj2 = new TestObject("last");
413+
Iterator<TestObject> objectIterator = Arrays.asList(obj1, obj2).iterator();
414+
Assert.assertEquals(obj2, IteratorUtils.getLast(objectIterator));
415+
Assert.assertFalse(objectIterator.hasNext());
416+
}
368417
}

0 commit comments

Comments
 (0)