This is an automated email from the ASF dual-hosted git repository. daim pushed a commit to branch OAK-11518 in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
commit 7c69f1e749833a5b063f66f0b4edd5a272909c1d Author: Rishabh Kumar <[email protected]> AuthorDate: Fri Feb 21 23:34:36 2025 +0530 OAK-11518 : added utils method to replace Iterables.partition --- .../oak/commons/collections/IterableUtils.java | 40 ++++++++++++ .../oak/commons/collections/IterableUtilsTest.java | 71 ++++++++++++++++++++++ 2 files changed, 111 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 e2107aea1a..7772aa9a04 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 @@ -18,13 +18,16 @@ */ package org.apache.jackrabbit.oak.commons.collections; +import org.apache.commons.collections4.IteratorUtils; import org.apache.commons.collections4.Predicate; import org.apache.commons.collections4.iterators.LazyIteratorChain; import org.jetbrains.annotations.NotNull; import java.lang.reflect.Array; +import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; +import java.util.List; import java.util.Objects; /** @@ -212,4 +215,41 @@ public class IterableUtils { final Collection<T> collection = itr instanceof Collection ? (Collection<T>) itr : ListUtils.toList(itr); return collection.toArray(t); } + + /** + * Splits an Iterable into an Iterator of sub-iterators, each of the specified size. + * + * @param <T> the type of elements in the itr + * @param itr the itr to split, may not be null + * @param size the size of each sub-iterator, must be greater than 0 + * @return an iterator of sub-iterators, each of the specified size + * @throws NullPointerException if the itr is null + * @throws IllegalArgumentException if size is less than or equal to 0 + */ + public static <T> Iterator<List<T>> partition(final Iterable<T> itr, final int size) { + if (itr == null) { + throw new NullPointerException("Iterable must not be null."); + } + if (size <= 0) { + throw new IllegalArgumentException("Size must be greater than 0."); + } + + return IteratorUtils.unmodifiableIterator(new Iterator<>() { + private final Iterator<T> iterator = itr.iterator(); + + @Override + public boolean hasNext() { + return iterator.hasNext(); + } + + @Override + public List<T> next() { + List<T> currentPartition = new ArrayList<>(size); + for (int i = 0; i < size && iterator.hasNext(); i++) { + currentPartition.add(iterator.next()); + } + return currentPartition; + } + }); + } } 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 88cd57a22b..f3f9a66e11 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 @@ -313,4 +313,75 @@ public class IterableUtilsTest { IterableUtils.toArray(itr, null); }); } + + @Test + public void testPartitionWithNonEmptyIterable() { + Iterable<Integer> iterable = Arrays.asList(1, 2, 3, 4, 5, 6, 7); + Iterator<List<Integer>> partitions = IterableUtils.partition(iterable, 3); + Assert.assertTrue(partitions.hasNext()); + Assert.assertEquals(Arrays.asList(1, 2, 3), partitions.next()); + Assert.assertTrue(partitions.hasNext()); + Assert.assertEquals(Arrays.asList(4, 5, 6), partitions.next()); + Assert.assertTrue(partitions.hasNext()); + Assert.assertEquals(Collections.singletonList(7), partitions.next()); + Assert.assertFalse(partitions.hasNext()); + } + + @Test + public void testPartitionWithEmptyIterable() { + Iterable<Integer> iterable = Collections.emptyList(); + Iterator<List<Integer>> partitions = IterableUtils.partition(iterable, 3); + Assert.assertFalse(partitions.hasNext()); + } + + @Test + public void testPartitionWithNotSupportedRemoveIterable() { + Iterable<Integer> iterable = Collections.emptyList(); + Iterator<List<Integer>> partitions = IterableUtils.partition(iterable, 3); + Assert.assertThrows(UnsupportedOperationException.class, partitions::remove); + } + + @Test + public void testPartitionWithSingleElement() { + Iterable<Integer> iterable = Collections.singletonList(1); + Iterator<List<Integer>> partitions = IterableUtils.partition(iterable, 3); + Assert.assertTrue(partitions.hasNext()); + Assert.assertEquals(Collections.singletonList(1), partitions.next()); + Assert.assertFalse(partitions.hasNext()); + } + + @Test + public void testPartitionWithSizeOne() { + Iterable<Integer> iterable = Arrays.asList(1, 2, 3, 4, 5); + Iterator<List<Integer>> partitions = IterableUtils.partition(iterable, 1); + Assert.assertTrue(partitions.hasNext()); + Assert.assertEquals(Collections.singletonList(1), partitions.next()); + Assert.assertTrue(partitions.hasNext()); + Assert.assertEquals(Collections.singletonList(2), partitions.next()); + Assert.assertTrue(partitions.hasNext()); + Assert.assertEquals(Collections.singletonList(3), partitions.next()); + Assert.assertTrue(partitions.hasNext()); + Assert.assertEquals(Collections.singletonList(4), partitions.next()); + Assert.assertTrue(partitions.hasNext()); + Assert.assertEquals(Collections.singletonList(5), partitions.next()); + Assert.assertFalse(partitions.hasNext()); + } + + @Test + public void testPartitionWithNullIterable() { + Assert.assertThrows(NullPointerException.class, () -> { + IterableUtils.partition(null, 3); + }); + } + + @Test + public void testPartitionWithInvalidSize() { + Iterable<Integer> iterable = Arrays.asList(1, 2, 3); + Assert.assertThrows(IllegalArgumentException.class, () -> { + IterableUtils.partition(iterable, 0); + }); + Assert.assertThrows(IllegalArgumentException.class, () -> { + IterableUtils.partition(iterable, -1); + }); + } }
