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 8512acc1c5 OAK-11513 : added util method in oak-commons to replace 
Iterables.toArray (#2105)
8512acc1c5 is described below

commit 8512acc1c564df8d7e467f37ea39e01035b410e9
Author: Rishabh Kumar <[email protected]>
AuthorDate: Fri Feb 21 16:48:27 2025 +0530

    OAK-11513 : added util method in oak-commons to replace Iterables.toArray 
(#2105)
    
    Co-authored-by: Rishabh Kumar <[email protected]>
---
 .../oak/commons/collections/IterableUtils.java     | 22 +++++++++++++
 .../oak/commons/collections/IterableUtilsTest.java | 36 ++++++++++++++++++++++
 2 files changed, 58 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 9a05516269..e2107aea1a 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,7 +20,10 @@ package org.apache.jackrabbit.oak.commons.collections;
 
 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.Collection;
 import java.util.Iterator;
 import java.util.Objects;
 
@@ -190,4 +193,23 @@ public class IterableUtils {
     public static boolean isEmpty(final Iterable<?> itr) {
         return org.apache.commons.collections4.IterableUtils.isEmpty(itr);
     }
+
+    /**
+     * Converts an Iterable to an array of the specified type.
+     *
+     * @param <T> the type of elements in the itr
+     * @param itr the itr to convert, may be null
+     * @param type the class of the type of elements in the array, may not be 
null
+     * @return an array containing the elements of the itr
+     * @throws NullPointerException if the itr or type is null
+     */
+    @NotNull
+    @SuppressWarnings("unchecked")
+    public static <T> T[] toArray(final Iterable<T> itr, final Class<T> type) {
+
+        final T[] t = (T[]) Array.newInstance(type, 0);
+
+        final Collection<T> collection = itr instanceof Collection ? 
(Collection<T>) itr : ListUtils.toList(itr);
+        return collection.toArray(t);
+    }
 }
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 39dd3a7472..88cd57a22b 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
@@ -277,4 +277,40 @@ public class IterableUtilsTest {
     public void testIsEmptyWithNullIterable() {
         Assert.assertTrue(IterableUtils.isEmpty(null));
     }
+
+    @Test
+    public void testToArrayWithNonEmptyIterable() {
+        Iterable<String> itr = Arrays.asList("a", "b", "c");
+        String[] array = IterableUtils.toArray(itr, String.class);
+        Assert.assertArrayEquals(new String[]{"a", "b", "c"}, array);
+    }
+
+    @Test
+    public void testToArrayWithEmptyIterable() {
+        Iterable<String> itr = Collections.emptyList();
+        String[] array = IterableUtils.toArray(itr, String.class);
+        Assert.assertArrayEquals(new String[]{}, array);
+    }
+
+    @Test
+    public void testToArrayWithSingleElement() {
+        Iterable<String> itr = Collections.singletonList("a");
+        String[] array = IterableUtils.toArray(itr, String.class);
+        Assert.assertArrayEquals(new String[]{"a"}, array);
+    }
+
+    @Test
+    public void testToArrayWithNullIterable() {
+        Assert.assertThrows(NullPointerException.class, () -> {
+            IterableUtils.toArray(null, String.class);
+        });
+    }
+
+    @Test
+    public void testToArrayWithNullType() {
+        Iterable<String> itr = Arrays.asList("a", "b", "c");
+        Assert.assertThrows(NullPointerException.class, () -> {
+            IterableUtils.toArray(itr, null);
+        });
+    }
 }

Reply via email to