ppkarwasz commented on code in PR #1411:
URL: https://github.com/apache/commons-lang/pull/1411#discussion_r2203170575


##########
src/main/java/org/apache/commons/lang3/ArrayUtils.java:
##########
@@ -8862,6 +8865,63 @@ public static <T> T[] 
toArray(@SuppressWarnings("unchecked") final T... items) {
         return items;
     }
 
+    /**
+     * Converts an {@link Iterator} into an array.
+     * <p>
+     * Returns {@code null} if the input iterator is {@code null}.
+     * If the iterator has no elements, an empty {@code Object[]} is returned.
+     * </p>
+     * <p>
+     * Note: The returned array has runtime type {@code Object[]}, and 
requires that all elements
+     * in the iterator are of the same type. If a type-safe array is needed, 
use
+     * {@link #iteratorToArray(Iterator, Class)} instead.
+     * </p>
+     *
+     * @param iterator the iterator to convert, may be {@code null}
+     * @param <T> the element type
+     * @return the array containing elements from the iterator,
+     *         or {@code null} if the input is {@code null}
+     * @since 3.18
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> T[] iteratorToArray(Iterator<T> iterator) {

Review Comment:
   This method causes heap pollution: the return type `T[]` can't be created 
safely due to type erasure.
   The returned type will always be `Object[]`, which will cause a 
`ClassCastException` at runtime, when used:
   
   ```java
   List<String> list = List.of("one", "two", "three");
   // Fails at runtime
   String[] array = iteratorToArray(list.iterator());
   ```
   



##########
src/main/java/org/apache/commons/lang3/ArrayUtils.java:
##########
@@ -8862,6 +8865,63 @@ public static <T> T[] 
toArray(@SuppressWarnings("unchecked") final T... items) {
         return items;
     }
 
+    /**
+     * Converts an {@link Iterator} into an array.
+     * <p>
+     * Returns {@code null} if the input iterator is {@code null}.
+     * If the iterator has no elements, an empty {@code Object[]} is returned.
+     * </p>
+     * <p>
+     * Note: The returned array has runtime type {@code Object[]}, and 
requires that all elements
+     * in the iterator are of the same type. If a type-safe array is needed, 
use
+     * {@link #iteratorToArray(Iterator, Class)} instead.
+     * </p>
+     *
+     * @param iterator the iterator to convert, may be {@code null}
+     * @param <T> the element type
+     * @return the array containing elements from the iterator,
+     *         or {@code null} if the input is {@code null}
+     * @since 3.18
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> T[] iteratorToArray(Iterator<T> iterator) {
+        if (iterator == null) {
+            return null;
+        }
+
+        if (!iterator.hasNext()) {
+            return (T[]) ArrayUtils.EMPTY_OBJECT_ARRAY;
+        }
+
+        return (T[]) Streams.of(iterator).toArray();
+    }
+
+    /**
+     * Converts an {@link Iterator} into a typed array.
+     * <p>
+     * Returns {@code null} if the input iterator or class is {@code null}.
+     * If the iterator has no elements, an empty array of the specified type 
is returned.
+     * </p>
+     *
+     * @param iterator the iterator to convert, may be {@code null}
+     * @param clazz    the class of the array component type, must not be 
{@code null}
+     * @param <T>      the element type
+     * @return the array containing elements from the iterator, or {@code 
null} if the input is {@code null}
+     * @since 3.18
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> T[] iteratorToArray(Iterator<T> iterator, Class<T> 
clazz) {
+        if (iterator == null || clazz == null) {
+            return null;
+        }
+
+        final List<T> list = new ArrayList<>();
+        iterator.forEachRemaining(list::add);
+
+        final T[] array = (T[]) Array.newInstance(clazz, list.size());
+        return list.toArray(array);
+    }

Review Comment:
   Some remarks:
   
   * While returning `null` when the `iterator` is `null` is consistent with 
other `ArrayUtils` methods, I would not accept a `null` `Class` argument.
   * The method can be safely generalized to accept an `Iterator<? extends T>` 
to support subtypes.
   * It is not necessary to copy elements into a temporary list first; the 
array can be constructed directly via `Stream`.
   
   ```suggestion
       public static <T> T[] iteratorToArray(Iterator<? extends T> iterator, 
Class<T> clazz) {
           Objects.requireNonNull(clazz, "clazz must not be null");
           return iterator == null
                   ? null
                    : Streams.of(iterator).toArray(size -> (T[]) 
Array.newInstance(clazz, size));
       }
   ```
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to