Abyss-lord commented on code in PR #1411: URL: https://github.com/apache/commons-lang/pull/1411#discussion_r2203235059
########## 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: Thank you for the suggestions—really helpful! Also, if I may ask a quick question: what formatting tool does Commons Lang use for code style enforcement? I’ve been using `Spotless` in other projects, but I’d like to make sure my contributions follow the correct convention here. Thanks again! -- 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: issues-unsubscr...@commons.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org