You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by da...@apache.org on 2025/02/21 09:28:35 UTC
(jackrabbit-oak) branch OAK-11513 created (now 40c3bc6aca)
This is an automated email from the ASF dual-hosted git repository.
daim pushed a change to branch OAK-11513
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
at 40c3bc6aca OAK-11513 : added util method in oak-commons to replace Iterables.toArray
This branch includes the following new commits:
new 40c3bc6aca OAK-11513 : added util method in oak-commons to replace Iterables.toArray
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(jackrabbit-oak) 01/01: OAK-11513 : added util method in oak-commons to replace Iterables.toArray
Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
daim pushed a commit to branch OAK-11513
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
commit 40c3bc6acad73a2b4856af96b822d1dbb92fa225
Author: Rishabh Kumar <di...@adobe.com>
AuthorDate: Fri Feb 21 14:57:58 2025 +0530
OAK-11513 : added util method in oak-commons to replace Iterables.toArray
---
.../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);
+ });
+ }
}