This is an automated email from the ASF dual-hosted git repository.
daim pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
The following commit(s) were added to refs/heads/trunk by this push:
new 60cd7597d5 OAK-11573: add size api in IteratorUtils (#2159)
60cd7597d5 is described below
commit 60cd7597d5eba9694fbf42aa25b474eb9a6def70
Author: Tushar <[email protected]>
AuthorDate: Tue Mar 11 13:19:01 2025 +0530
OAK-11573: add size api in IteratorUtils (#2159)
---
.../oak/commons/collections/IteratorUtils.java | 13 +++++
.../oak/commons/collections/IteratorUtilsTest.java | 55 ++++++++++++++++++++++
2 files changed, 68 insertions(+)
diff --git
a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtils.java
b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtils.java
index 1da3dfa3b1..dafe7f96cb 100644
---
a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtils.java
+++
b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtils.java
@@ -177,4 +177,17 @@ public class IteratorUtils {
// return true if both the iterators have same number of elements
return !iterator1.hasNext() && !iterator2.hasNext();
}
+
+ /**
+ * Returns the number of elements in the given iterator.
+ * <p>
+ * This method consumes the iterator to count the elements.
+ * A null or empty iterator returns 0.
+ *
+ * @param iterator the iterator whose size is to be determined
+ * @return the number of elements in the iterator
+ */
+ public static int size(Iterator<?> iterator) {
+ return org.apache.commons.collections4.IteratorUtils.size(iterator);
+ }
}
diff --git
a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtilsTest.java
b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtilsTest.java
index 5986534bc2..6b1258ce0e 100644
---
a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtilsTest.java
+++
b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtilsTest.java
@@ -231,4 +231,59 @@ public class IteratorUtilsTest {
list2.set(9999, -1);
Assert.assertFalse(IteratorUtils.elementsEqual(list1.iterator(),
list2.iterator()));
}
+
+ @Test
+ public void testSizeWithMultipleElements() {
+ List<String> list = Arrays.asList("one", "two", "three", "four",
"five");
+ Iterator<String> iterator = list.iterator();
+ Assert.assertEquals(5, IteratorUtils.size(iterator));
+ Assert.assertFalse("Iterator should be consumed after size operation",
iterator.hasNext());
+ }
+
+ @Test
+ public void testSizeWithEmptyIterator() {
+ Assert.assertEquals(0,
IteratorUtils.size(Collections.emptyIterator()));
+ }
+
+ @Test
+ public void testSizeWithNullIterator() {
+ Assert.assertEquals(0,IteratorUtils.size(null));
+ }
+
+ @Test
+ public void testSizeConsumesIterator() {
+ List<String> list = Arrays.asList("one", "two", "three");
+ Iterator<String> iterator = list.iterator();
+
+ Assert.assertEquals(3, IteratorUtils.size(iterator));
+ Assert.assertFalse("Iterator should be consumed after size operation",
iterator.hasNext());
+ }
+
+ @Test
+ public void testSizeWithSingleElement() {
+ List<String> singletonList = Collections.singletonList("single");
+ Assert.assertEquals(1, IteratorUtils.size(singletonList.iterator()));
+ }
+
+ @Test
+ public void testSizeWithCustomIterator() {
+ Iterator<Integer> customIterator = new Iterator<>() {
+ private int count = 0;
+
+ @Override
+ public boolean hasNext() {
+ return count < 10;
+ }
+
+ @Override
+ public Integer next() {
+ if (!hasNext()) {
+ throw new NoSuchElementException();
+ }
+ return count++;
+ }
+ };
+ Assert.assertEquals(10, IteratorUtils.size(customIterator));
+ Assert.assertFalse("Iterator should be consumed after size operation",
customIterator.hasNext());
+ }
}