Skip to content

Commit ac0f242

Browse files
t-ranaDaniel Iancu
authored andcommitted
OAK-11583: add get api in IteratorUtils (apache#2171)
1 parent 17570fd commit ac0f242

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,22 @@ public static boolean elementsEqual(final Iterator<?> iterator1, final Iterator<
190190
public static int size(Iterator<?> iterator) {
191191
return org.apache.commons.collections4.IteratorUtils.size(iterator);
192192
}
193+
194+
/**
195+
* Returns the element at the specified position in the iterator.
196+
* <p>
197+
* This method will consume the iterator up to the specified position.
198+
* <p>
199+
* @param <T> the type of elements in the iterator
200+
* @param iterator the iterator to get the element from, must not be null
201+
* @param index the position of the element to return, zero-based
202+
* @return the element at the specified position
203+
* @throws NullPointerException if the iterator is null
204+
* @throws IndexOutOfBoundsException if the iterator is empty or index is negative or greater than the number
205+
* of elements in the iterator
206+
*/
207+
public static <T> T get(Iterator<T> iterator, int index) {
208+
Objects.requireNonNull(iterator, "Iterator must not be null");
209+
return org.apache.commons.collections4.IteratorUtils.get(iterator, index);
210+
}
193211
}

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,83 @@ public Integer next() {
286286
Assert.assertEquals(10, IteratorUtils.size(customIterator));
287287
Assert.assertFalse("Iterator should be consumed after size operation", customIterator.hasNext());
288288
}
289+
290+
@Test
291+
public void testGetFirstElement() {
292+
Iterator<String> iterator = Arrays.asList("a", "b", "c").iterator();
293+
Assert.assertEquals("a", IteratorUtils.get(iterator, 0));
294+
// iterator should be at position 1
295+
Assert.assertEquals("b", iterator.next());
296+
297+
}
298+
299+
@Test
300+
public void testGetMiddleElement() {
301+
Iterator<Integer> iterator = Arrays.asList(1, 2, 3).iterator();
302+
Assert.assertEquals(Integer.valueOf(2), IteratorUtils.get(iterator, 1));
303+
// iterator should be at position 2
304+
Assert.assertEquals(Integer.valueOf(3), iterator.next());
305+
}
306+
307+
@Test
308+
public void testGetLastElement() {
309+
Iterator<String> iterator = Arrays.asList("a", "b", "c").iterator();
310+
Assert.assertEquals("c", IteratorUtils.get(iterator, 2));
311+
Assert.assertFalse(iterator.hasNext());
312+
}
313+
314+
@Test
315+
public void testGetWithNullIterator() {
316+
Assert.assertThrows(NullPointerException.class, () -> {
317+
IteratorUtils.get(null, 0);
318+
});
319+
}
320+
321+
@Test
322+
public void testGetWithNegativeIndex() {
323+
List<String> data = Arrays.asList("a", "b", "c");
324+
Assert.assertThrows(IndexOutOfBoundsException.class, () -> {
325+
IteratorUtils.get(data.iterator(), -1);
326+
});
327+
}
328+
329+
@Test
330+
public void testWithIndexGreaterThanSizeOfIterator() {
331+
List<String> data = Arrays.asList("a", "b", "c");
332+
Assert.assertThrows(IndexOutOfBoundsException.class, () -> {
333+
IteratorUtils.get(data.iterator(), 3);
334+
});
335+
}
336+
337+
@Test
338+
public void testGetWithEmptyIterator() {
339+
Assert.assertThrows(IndexOutOfBoundsException.class, () -> {
340+
IteratorUtils.get(Collections.emptyIterator(), 0);
341+
});
342+
}
343+
344+
@Test
345+
public void testGetWithCustomObject() {
346+
class TestObject {
347+
private final String value;
348+
349+
TestObject(String value) {
350+
this.value = value;
351+
}
352+
353+
@Override
354+
public String toString() {
355+
return value;
356+
}
357+
}
358+
359+
List<TestObject> data = Arrays.asList(
360+
new TestObject("obj1"),
361+
new TestObject("obj2"),
362+
new TestObject("obj3")
363+
);
364+
365+
TestObject result = IteratorUtils.get(data.iterator(), 1);
366+
Assert.assertEquals("obj2", result.toString());
367+
}
289368
}

0 commit comments

Comments
 (0)