Skip to content

Commit 8512acc

Browse files
rishabhdaimRishabh Kumar
andauthored
OAK-11513 : added util method in oak-commons to replace Iterables.toArray (#2105)
Co-authored-by: Rishabh Kumar <diam@adobe.com>
1 parent 14c29ca commit 8512acc

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IterableUtils.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020

2121
import org.apache.commons.collections4.Predicate;
2222
import org.apache.commons.collections4.iterators.LazyIteratorChain;
23+
import org.jetbrains.annotations.NotNull;
2324

25+
import java.lang.reflect.Array;
26+
import java.util.Collection;
2427
import java.util.Iterator;
2528
import java.util.Objects;
2629

@@ -190,4 +193,23 @@ public static <E> boolean matchesAll(final Iterable<E> itr, final Predicate<? su
190193
public static boolean isEmpty(final Iterable<?> itr) {
191194
return org.apache.commons.collections4.IterableUtils.isEmpty(itr);
192195
}
196+
197+
/**
198+
* Converts an Iterable to an array of the specified type.
199+
*
200+
* @param <T> the type of elements in the itr
201+
* @param itr the itr to convert, may be null
202+
* @param type the class of the type of elements in the array, may not be null
203+
* @return an array containing the elements of the itr
204+
* @throws NullPointerException if the itr or type is null
205+
*/
206+
@NotNull
207+
@SuppressWarnings("unchecked")
208+
public static <T> T[] toArray(final Iterable<T> itr, final Class<T> type) {
209+
210+
final T[] t = (T[]) Array.newInstance(type, 0);
211+
212+
final Collection<T> collection = itr instanceof Collection ? (Collection<T>) itr : ListUtils.toList(itr);
213+
return collection.toArray(t);
214+
}
193215
}

oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IterableUtilsTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,40 @@ public void testIsEmptyWithSingleElement() {
277277
public void testIsEmptyWithNullIterable() {
278278
Assert.assertTrue(IterableUtils.isEmpty(null));
279279
}
280+
281+
@Test
282+
public void testToArrayWithNonEmptyIterable() {
283+
Iterable<String> itr = Arrays.asList("a", "b", "c");
284+
String[] array = IterableUtils.toArray(itr, String.class);
285+
Assert.assertArrayEquals(new String[]{"a", "b", "c"}, array);
286+
}
287+
288+
@Test
289+
public void testToArrayWithEmptyIterable() {
290+
Iterable<String> itr = Collections.emptyList();
291+
String[] array = IterableUtils.toArray(itr, String.class);
292+
Assert.assertArrayEquals(new String[]{}, array);
293+
}
294+
295+
@Test
296+
public void testToArrayWithSingleElement() {
297+
Iterable<String> itr = Collections.singletonList("a");
298+
String[] array = IterableUtils.toArray(itr, String.class);
299+
Assert.assertArrayEquals(new String[]{"a"}, array);
300+
}
301+
302+
@Test
303+
public void testToArrayWithNullIterable() {
304+
Assert.assertThrows(NullPointerException.class, () -> {
305+
IterableUtils.toArray(null, String.class);
306+
});
307+
}
308+
309+
@Test
310+
public void testToArrayWithNullType() {
311+
Iterable<String> itr = Arrays.asList("a", "b", "c");
312+
Assert.assertThrows(NullPointerException.class, () -> {
313+
IterableUtils.toArray(itr, null);
314+
});
315+
}
280316
}

0 commit comments

Comments
 (0)