Skip to content

Commit 60cd759

Browse files
authored
OAK-11573: add size api in IteratorUtils (#2159)
1 parent 3f5167c commit 60cd759

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,17 @@ public static boolean elementsEqual(final Iterator<?> iterator1, final Iterator<
177177
// return true if both the iterators have same number of elements
178178
return !iterator1.hasNext() && !iterator2.hasNext();
179179
}
180+
181+
/**
182+
* Returns the number of elements in the given iterator.
183+
* <p>
184+
* This method consumes the iterator to count the elements.
185+
* A null or empty iterator returns 0.
186+
*
187+
* @param iterator the iterator whose size is to be determined
188+
* @return the number of elements in the iterator
189+
*/
190+
public static int size(Iterator<?> iterator) {
191+
return org.apache.commons.collections4.IteratorUtils.size(iterator);
192+
}
180193
}

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,59 @@ public void testLargeIterators() {
231231
list2.set(9999, -1);
232232
Assert.assertFalse(IteratorUtils.elementsEqual(list1.iterator(), list2.iterator()));
233233
}
234+
235+
@Test
236+
public void testSizeWithMultipleElements() {
237+
List<String> list = Arrays.asList("one", "two", "three", "four", "five");
238+
Iterator<String> iterator = list.iterator();
239+
Assert.assertEquals(5, IteratorUtils.size(iterator));
240+
Assert.assertFalse("Iterator should be consumed after size operation", iterator.hasNext());
241+
}
242+
243+
@Test
244+
public void testSizeWithEmptyIterator() {
245+
Assert.assertEquals(0, IteratorUtils.size(Collections.emptyIterator()));
246+
}
247+
248+
@Test
249+
public void testSizeWithNullIterator() {
250+
Assert.assertEquals(0,IteratorUtils.size(null));
251+
}
252+
253+
@Test
254+
public void testSizeConsumesIterator() {
255+
List<String> list = Arrays.asList("one", "two", "three");
256+
Iterator<String> iterator = list.iterator();
257+
258+
Assert.assertEquals(3, IteratorUtils.size(iterator));
259+
Assert.assertFalse("Iterator should be consumed after size operation", iterator.hasNext());
260+
}
261+
262+
@Test
263+
public void testSizeWithSingleElement() {
264+
List<String> singletonList = Collections.singletonList("single");
265+
Assert.assertEquals(1, IteratorUtils.size(singletonList.iterator()));
266+
}
267+
268+
@Test
269+
public void testSizeWithCustomIterator() {
270+
Iterator<Integer> customIterator = new Iterator<>() {
271+
private int count = 0;
272+
273+
@Override
274+
public boolean hasNext() {
275+
return count < 10;
276+
}
277+
278+
@Override
279+
public Integer next() {
280+
if (!hasNext()) {
281+
throw new NoSuchElementException();
282+
}
283+
return count++;
284+
}
285+
};
286+
Assert.assertEquals(10, IteratorUtils.size(customIterator));
287+
Assert.assertFalse("Iterator should be consumed after size operation", customIterator.hasNext());
288+
}
234289
}

0 commit comments

Comments
 (0)