[
https://issues.apache.org/jira/browse/GROOVY-12218?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18100396#comment-18100396
]
ASF GitHub Bot commented on GROOVY-12218:
-----------------------------------------
Copilot commented on code in PR #2752:
URL: https://github.com/apache/groovy/pull/2752#discussion_r3682276449
##########
src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java:
##########
@@ -997,6 +1137,29 @@ public static <K, V, E> Map<K, V> collectEntries(E[]
self, Map<K, V> collector,
return DefaultGroovyMethods.collectEntries(new ArrayIterator<>(self),
collector, transform);
}
+ /**
+ * Iterates through an array transforming each element into a map entry
using the
+ * transform function and adding it to the collector. A "fat-free" variant
of
+ * {@link #collectEntries(Object[], Map, Closure)} accepting a {@link
Function}.
+ * <pre class="language-groovy groovyTestCase">
+ * String[] words = ['bb', 'ccc']
+ * assert words.collectEntries([a: 1], w -> new MapEntry(w, w.size()))
== [a: 1, bb: 2, ccc: 3]
+ * </pre>
+ *
+ * @param self an array
+ * @param collector an initial map to add the collected entries to
+ * @param transform the transform function applied to each element,
returning a map entry
+ * @return the collector with the collected entries added to it
+ * @since 6.0.0
+ */
+ public static <K, V, E> Map<K, V> collectEntries(E[] self, Map<K, V>
collector, Function<? super E, ? extends Map.Entry<K, V>> transform) {
+ for (E item : self) {
+ Map.Entry<K, V> entry = transform.apply(item);
+ collector.put(entry.getKey(), entry.getValue());
+ }
+ return collector;
+ }
Review Comment:
`collectEntries(E[] self, Map collector, Function transform)` currently
dereferences `entry.getKey()/getValue()` unconditionally. If the transform
returns `null`, this will throw an NPE, whereas
`DefaultGroovyMethods.collectEntries(..., Closure)` (via `addEntry`) treats a
`null` entrySpec as “insert nothing” (see GROOVY-10893 handling). The array
overload should mirror that behavior by skipping null entries.
> Complete fat-free (functional-interface) twins for the array (`T[]`) forms of
> the GROOVY-12054 DGM methods
> ----------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12218
> URL: https://issues.apache.org/jira/browse/GROOVY-12218
> Project: Groovy
> Issue Type: Sub-task
> Reporter: Paul King
> Priority: Major
>
> h3. Summary
> GROOVY-12054 added {{java.util.function}} twins beside the Closure overloads
> of the core higher-order DGM methods, but only in {{DefaultGroovyMethods}} —
> the array-receiver forms in {{ArrayGroovyMethods}} were left with only their
> {{Closure}} overloads. This completes the object-array ({{T[]}}) twins so
> array receivers reach parity with the collection receivers. Each twin simply
> delegates to the corresponding DGM {{Iterable}}/{{Iterator}} twin (already
> shipped in GROOVY-12054) via {{ArrayIterable}}/{{ArrayIterator}}, following
> the array pattern established in GROOVY-12215 / GROOVY-12216.
> Already covered (not in scope): {{each}} on primitive arrays already has
> {{Consumer}}/{{IntConsumer}}/etc. twins; {{collectEntries}} already has the
> two-{{Function}} {{(keyTransform, valueTransform)}} forms; and the array
> forms of
> {{groupBy}}/{{collectMany}}/{{countBy}}/{{split}}/{{takeWhile}}/{{dropWhile}}
> (GROOVY-12215) and {{inject}}/{{injectAll}} (GROOVY-12216) are done.
> h3. Object-array twins to add
> ||Method (array form)||Count||Functional type||
> |{{each(T[])}}|1|{{Consumer<? super T>}}|
> |{{eachWithIndex(T[])}}|1|{{ObjIntConsumer<? super T>}}|
> |{{collect(T[])}}, {{collect(T[], Collection collector)}}|2|{{Function<?
> super E, ? extends T>}}|
> |{{collectEntries(T[])}}, {{collectEntries(T[], Map
> collector)}}|2|{{Function<? super E, ? extends Map.Entry<K, V>>}}|
> |{{any(T[])}}|1|{{Predicate<? super T>}}|
> |{{every(T[])}}|1|{{Predicate<? super T>}}|
> |{{find(T[])}}|1|{{Predicate<? super T>}}|
> |{{findAll(T[])}}|1|{{Predicate<? super T>}}|
> |{{count(T[])}}|1|{{Predicate<? super T>}}|
> Total: 11 object-array twins.
> h3. Primitive-array predicates (decision needed)
> {{any}}/{{every}}/{{count}} on primitive arrays currently take only a
> {{Closure}}. The JDK provides
> {{IntPredicate}}/{{LongPredicate}}/{{DoublePredicate}} but no
> {{Byte}}/{{Char}}/{{Short}}/{{Float}} predicate, so only
> {{int[]}}/{{long[]}}/{{double[]}} can be covered cleanly (3 receivers x 3
> methods = 9 twins); the other four primitive types would be inconsistent
> (widening to {{IntPredicate}} is ugly). *Options:* (a) add the 9
> well-supported primitive twins; (b) skip primitives for predicates and cover
> only {{T[]}}. Recommendation: match how {{each}}'s primitive twins already
> exist only where a JDK SAM fits — i.e. option (a) if primitive predicate
> ergonomics are wanted, else (b).
> h3. Exclusions
> * {{eachWithIndex}} on primitive arrays — no JDK {{(primitive, int)}} SAM, so
> only the {{T[]}} form ({{ObjIntConsumer}}) is clean.
> * The {{@Incubating}} right-curry {{(BiXxx, param)}} array forms —
> GROOVY-12054 added these for {{Iterable}}/{{Iterator}} but not arrays;
> deferred as a separate item.
> h3. Conventions (per GROOVY-12054)
> PECS wildcards; {{@since 6.0.0}}; javadoc {{<pre class="language-groovy
> groovyTestCase">}} example cross-linking the {{Closure}} counterpart via
> {{@link}}; plain SAM overloads (not {{@Incubating}}); bodies delegate to the
> existing DGM twins.
> h3. Compatibility
> Existing closure/lambda call sites are unaffected; method references and
> typed functional values select the new twins under {{@CompileStatic}}
> (GROOVY-12214). The only check is an existing {{@CompileStatic}} call passing
> a literal {{null}} to a currently-single-overload array method — rare and
> grep-checkable, resolved with a cast if found.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)