This is an automated email from the ASF dual-hosted git repository. daim pushed a commit to branch OAK-11588 in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
commit b980c3cf47453f671628e1d70a5240328ebf1e93 Author: Rishabh Kumar <[email protected]> AuthorDate: Thu Apr 17 09:02:38 2025 +0530 OAK-11588 : added Iterators.toArray replacement in oak-commons --- .../oak/commons/collections/IteratorUtils.java | 17 +++++++ .../oak/commons/collections/IteratorUtilsTest.java | 54 ++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtils.java b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtils.java index 9701d92632..41a3300311 100644 --- a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtils.java +++ b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtils.java @@ -250,4 +250,21 @@ public class IteratorUtils { Objects.requireNonNull(iterator, "Iterator must not be null"); return org.apache.commons.collections4.IteratorUtils.contains(iterator, element); } + + /** + * Converts an iterator to an array of a specific type. + * <p> + * This method consumes the iterator and returns an array containing all of its elements. + * The type of the array is determined by the provided {@code type} parameter. + * + * @param <T> the component type of the resulting array + * @param iterator the iterator to convert, must not be null + * @param type the {@link Class} object representing the component type of the array, must not be null + * @return an array containing all the elements from the iterator + * @throws NullPointerException if the iterator or type is null + * @since 4.1 + */ + public static <T> T[] toArray(Iterator<? extends T> iterator, Class<T> type) { + return org.apache.commons.collections4.IteratorUtils.toArray(iterator, type); + } } diff --git a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtilsTest.java b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtilsTest.java index 6883df39b1..3fed8a21e4 100644 --- a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtilsTest.java +++ b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtilsTest.java @@ -458,4 +458,58 @@ public class IteratorUtilsTest { Assert.assertTrue(iterator.hasNext()); Assert.assertEquals("b", iterator.next()); } + + @Test + public void testToArrayWithMultipleElements() { + Iterator<String> iterator = Arrays.asList("one", "two", "three").iterator(); + String[] array = IteratorUtils.toArray(iterator, String.class); + Assert.assertArrayEquals(new String[]{"one", "two", "three"}, array); + } + + @Test + public void testToArrayWithEmptyIterator() { + Iterator<String> iterator = Collections.emptyIterator(); + String[] array = IteratorUtils.toArray(iterator, String.class); + Assert.assertArrayEquals(new String[0], array); + } + + @Test(expected = NullPointerException.class) + public void testToArrayWithNullIterator() { + IteratorUtils.toArray(null, String.class); + } + + @Test(expected = NullPointerException.class) + public void testToArrayWithNullType() { + Iterator<String> iterator = Arrays.asList("one").iterator(); + IteratorUtils.toArray(iterator, null); + } + + @Test + public void testToArrayWithSingleElement() { + Iterator<Integer> iterator = Collections.singletonList(10).iterator(); + Integer[] array = IteratorUtils.toArray(iterator, Integer.class); + Assert.assertArrayEquals(new Integer[]{10}, array); + } + + @Test + public void testToArrayWithCustomType() { + class CustomObject { + private final String value; + + CustomObject(String value) { + this.value = value; + } + + @Override + public String toString() { + return value; + } + } + + List<CustomObject> list = Arrays.asList(new CustomObject("first"), new CustomObject("second")); + Iterator<CustomObject> iterator = list.iterator(); + CustomObject[] array = IteratorUtils.toArray(iterator, CustomObject.class); + Assert.assertEquals("first", array[0].toString()); + Assert.assertEquals("second", array[1].toString()); + } }
