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 a200ceb052 OAK-11548 : added util method to replace Guava's 
Iterables.limit() (#2138)
a200ceb052 is described below

commit a200ceb052d8deaa391b24808a356a146aa316c7
Author: Rishabh Kumar <[email protected]>
AuthorDate: Wed Mar 5 20:06:53 2025 +0530

    OAK-11548 : added util method to replace Guava's Iterables.limit() (#2138)
    
    Co-authored-by: Rishabh Kumar <[email protected]>
---
 .../oak/commons/collections/IterableUtils.java     | 18 ++++++
 .../oak/commons/collections/IterableUtilsTest.java | 67 ++++++++++++++++++++++
 2 files changed, 85 insertions(+)

diff --git 
a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IterableUtils.java
 
b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IterableUtils.java
index c88b7b1d23..370f66a7b6 100644
--- 
a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IterableUtils.java
+++ 
b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IterableUtils.java
@@ -357,4 +357,22 @@ public class IterableUtils {
         }
         return IteratorUtils.elementsEqual(itr1.iterator(), itr2.iterator());
     }
+
+    /**
+     * Creates an iterable limited to the specified number of elements.
+     * <p>
+     * The returned iterable's iterator will stop returning elements after the 
specified limit
+     * has been reached or when the source iterable's iterator is exhausted, 
whichever comes first.
+     * <p>
+     * The returned iterable's iterator supports {@code remove()} if the 
original iterator supports it.
+     *
+     * @param <T> the type of elements in the iterable
+     * @param iterable the iterable to limit, may be null
+     * @param limitSize the maximum number of elements to return
+     * @return a limited iterable
+     * @throws IllegalArgumentException if limitSize is negative
+     */
+    public static <T> Iterable<T> limit(final Iterable<T> iterable, final int 
limitSize) {
+        return 
org.apache.commons.collections4.IterableUtils.boundedIterable(iterable, 
limitSize);
+    }
 }
diff --git 
a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IterableUtilsTest.java
 
b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IterableUtilsTest.java
index 9c1aa94551..c9b0efe106 100644
--- 
a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IterableUtilsTest.java
+++ 
b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IterableUtilsTest.java
@@ -755,4 +755,71 @@ public class IterableUtilsTest {
 
         Assert.assertTrue(IterableUtils.elementsEqual(arrayList, linkedList));
     }
+
+    @Test
+    public void testLimitWithFewerElements() {
+        List<Integer> list = List.of(1, 2, 3, 4, 5);
+        Iterable<Integer> limited = IterableUtils.limit(list, 3);
+
+        List<Integer> result = new ArrayList<>();
+        limited.forEach(result::add);
+
+        Assert.assertEquals(List.of(1, 2, 3), result);
+    }
+
+    @Test
+    public void testLimitWithMoreElements() {
+        List<Integer> list = List.of(1, 2, 3);
+        Iterable<Integer> limited = IterableUtils.limit(list, 5);
+
+        List<Integer> result = new ArrayList<>();
+        limited.forEach(result::add);
+
+        Assert.assertEquals(List.of(1, 2, 3), result);
+    }
+
+    @Test
+    public void testLimitWithZero() {
+        List<Integer> list = List.of(1, 2, 3, 4, 5);
+        Iterable<Integer> limited = IterableUtils.limit(list, 0);
+
+        List<Integer> result = new ArrayList<>();
+        limited.forEach(result::add);
+
+        Assert.assertTrue(result.isEmpty());
+    }
+
+    @Test
+    public void testLimitWithEmptyIterable() {
+        List<Integer> list = Collections.emptyList();
+        Iterable<Integer> limited = IterableUtils.limit(list, 3);
+
+        Assert.assertFalse(limited.iterator().hasNext());
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void testLimitWithNullIterable() {
+        Iterable<Integer> limited = IterableUtils.limit(null, 3);
+
+        Assert.assertFalse(limited.iterator().hasNext());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testLimitWithNegativeSize() {
+        List<Integer> list = List.of(1, 2, 3);
+        IterableUtils.limit(list, -1);
+    }
+
+    @Test
+    public void testLimitWithRemove() {
+        List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
+        Iterable<Integer> limited = IterableUtils.limit(list, 2);
+
+        Iterator<Integer> iterator = limited.iterator();
+        iterator.next();  // 1
+        iterator.remove();
+        iterator.next();  // 2
+
+        Assert.assertEquals(Arrays.asList(2, 3, 4, 5), list);
+    }
 }

Reply via email to