This is an automated email from the ASF dual-hosted git repository. daim pushed a commit to branch OAK-11548 in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
commit 9de2d19b65a0754dfaf82311cb3a02864d4e5183 Author: Rishabh Kumar <[email protected]> AuthorDate: Wed Mar 5 17:28:57 2025 +0530 OAK-11548 : added util method to replace Guava's Iterables.limit() --- .../oak/commons/collections/IterableUtils.java | 19 ++++++ .../oak/commons/collections/IterableUtilsTest.java | 67 ++++++++++++++++++++++ 2 files changed, 86 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..c1673e7e9f 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 @@ -21,6 +21,7 @@ package org.apache.jackrabbit.oak.commons.collections; import org.apache.commons.collections4.Predicate; import org.apache.commons.collections4.iterators.LazyIteratorChain; import org.apache.jackrabbit.oak.commons.conditions.Validate; +import org.checkerframework.checker.nullness.qual.Nullable; import org.jetbrains.annotations.NotNull; import java.lang.reflect.Array; @@ -357,4 +358,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); + } }
