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 b11c1b3c2c OAK-11518 : added utils method to replace 
Iterables.partition (#2111)
b11c1b3c2c is described below

commit b11c1b3c2c46ce7b290686232cf54c47988acc27
Author: Rishabh Kumar <[email protected]>
AuthorDate: Mon Feb 24 19:40:55 2025 +0530

    OAK-11518 : added utils method to replace Iterables.partition (#2111)
    
    * OAK-11518 : added utils method to replace Iterables.partition
    
    * OAK-11518 : updated return type to iterable
    
    * OAK-11518 : added requireNotNull and checkargument check
    
    * OAK-11518 : throws an exception if no element is present while calling 
Iterable.next()
    
    * Update IterableUtilsTest.java
    
    ---------
    
    Co-authored-by: Rishabh Kumar <[email protected]>
    Co-authored-by: mbaedke <[email protected]>
---
 .../oak/commons/collections/IterableUtils.java     | 48 +++++++++++++
 .../oak/commons/collections/IterableUtilsTest.java | 80 ++++++++++++++++++++++
 2 files changed, 128 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..bea321d4b0 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
@@ -20,11 +20,15 @@ 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.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.NoSuchElementException;
 import java.util.Objects;
 
 /**
@@ -212,4 +216,48 @@ 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> Iterable<List<T>> partition(final Iterable<T> itr, final 
int size) {
+
+        Objects.requireNonNull(itr, "Iterable must not be null.");
+        Validate.checkArgument(size > 0, "Size must be greater than 0.");
+
+        return new Iterable<>() {
+            @Override
+            public @NotNull Iterator<List<T>> iterator() {
+                return new Iterator<>() {
+                    private final Iterator<T> iterator = itr.iterator();
+
+                    @Override
+                    public boolean hasNext() {
+                        return iterator.hasNext();
+                    }
+
+                    @Override
+                    public List<T> next() {
+                        // check if there are elements left, throw an 
exception if not
+                        if (!hasNext()) {
+                            throw new NoSuchElementException();
+                        }
+
+                        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..1f30edca6b 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
@@ -26,6 +26,7 @@ import java.util.Arrays;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
+import java.util.NoSuchElementException;
 
 /**
  * Unit tests for the {@link IterableUtils} class.
@@ -313,4 +314,83 @@ 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).iterator();
+        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).iterator();
+        Assert.assertFalse(partitions.hasNext());
+    }
+
+    @Test
+    public void testPartitionWithNotSupportedRemoveIterable() {
+        Iterable<Integer> iterable = Collections.emptyList();
+        Iterator<List<Integer>> partitions = IterableUtils.partition(iterable, 
3).iterator();
+        Assert.assertThrows(UnsupportedOperationException.class, 
partitions::remove);
+    }
+
+    @Test
+    public void testPartitionWithSingleElement() {
+        Iterable<Integer> iterable = Collections.singletonList(1);
+        Iterator<List<Integer>> partitions = IterableUtils.partition(iterable, 
3).iterator();
+        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).iterator();
+        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);
+        });
+    }
+
+    @Test
+    public void testPartitionWithEmptyIterableAndSizeOne() {
+        Iterable<List<Integer>> partition = 
IterableUtils.partition(Collections.emptyList(), 1);
+        Iterator<List<Integer>> iterator = partition.iterator();
+        Assert.assertFalse(iterator.hasNext());
+        Assert.assertThrows(NoSuchElementException.class, iterator::next);
+    }
 }

Reply via email to