[
https://issues.apache.org/jira/browse/GROOVY-10879?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17649640#comment-17649640
]
ASF GitHub Bot commented on GROOVY-10879:
-----------------------------------------
sonatype-lift[bot] commented on code in PR #1834:
URL: https://github.com/apache/groovy/pull/1834#discussion_r1053074864
##########
src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java:
##########
@@ -4005,6 +3998,405 @@ public static <K, V, E> Map<K, V>
collectEntries(Iterator<E> self, Map<K, V> col
return collector;
}
+ /**
+ * A variant of collectEntries for Iterators with separate functions for
transforming the keys and values.
+ * The supplied collector map is used as the destination for transformed
entries.
+ *
+ * @param self an Iterator
+ * @param collector the Map into which the transformed entries are put
+ * @param keyTransform a function for transforming Iterator elements
into keys
+ * @param valueTransform a function for transforming Iterator elements
into values
+ * @return the collector with all transformed values added to it
+ * @since 5.0.0
+ */
+ public static <K, V, E> Map<K, V> collectEntries(Iterator<E> self, Map<K,
V> collector, Function<? super E, K> keyTransform, Function<? super E, V>
valueTransform) {
+ while (self.hasNext()) {
+ E element = self.next();
+ addEntry(collector, Tuple2.tuple(keyTransform.apply(element),
valueTransform.apply(element)));
+ }
+ return collector;
+ }
+
+ /**
+ * A variant of collectEntries for Iterators with separate functions for
transforming the keys and values.
+ *
+ * @param self an Iterator
+ * @param keyTransform a function for transforming Iterator elements
into keys
+ * @param valueTransform a function for transforming Iterator elements
into values
+ * @return a Map of the transformed entries
+ * @since 5.0.0
+ */
+ public static <K, V, E> Map<K, V> collectEntries(Iterator<E> self,
Function<? super E, K> keyTransform, Function<? super E, V> valueTransform) {
+ return collectEntries(self, new LinkedHashMap<>(), keyTransform,
valueTransform);
+ }
+
+ /**
+ * A variant of collectEntries for Iterables with separate functions for
transforming the keys and values.
+ * The supplied collector map is used as the destination for transformed
entries.
+ * <pre class="groovyTestCase">
+ * def languages = ['Groovy', 'Java', 'Kotlin', 'Scala']
+ * assert languages.collectEntries([clojure:7], String::toLowerCase,
String::size) ==
+ * [clojure:7, groovy:6, java:4, kotlin:6, scala:5]
+ * </pre>
+ *
+ * @param self an Iterable
+ * @param collector the Map into which the transformed entries are put
+ * @param keyTransform a function for transforming Iterable elements
into keys
+ * @param valueTransform a function for transforming Iterable elements
into values
+ * @return the collector with all transformed values added to it
+ * @since 5.0.0
+ */
+ public static <K, V, E> Map<K, V> collectEntries(Iterable<E> self, Map<K,
V> collector, Function<? super E, K> keyTransform, Function<? super E, V>
valueTransform) {
+ return collectEntries(self.iterator(), collector, keyTransform,
valueTransform);
+ }
+
+ /**
+ * A variant of collectEntries for Iterables with separate functions for
transforming the keys and values.
+ * <pre class="groovyTestCase">
+ * def languages = ['Groovy', 'Java', 'Kotlin', 'Scala']
+ * assert languages.collectEntries(String::toLowerCase, String::size) ==
+ * [groovy:6, java:4, kotlin:6, scala:5]
+ * </pre>
+ *
+ * @param self an Iterable
+ * @param keyTransform a function for transforming Iterable elements
into keys
+ * @param valueTransform a function for transforming Iterable elements
into values
+ * @return a Map of the transformed entries
+ * @since 5.0.0
+ */
+ public static <K, V, E> Map<K, V> collectEntries(Iterable<E> self,
Function<? super E, K> keyTransform, Function<? super E, V> valueTransform) {
+ return collectEntries(self.iterator(), new LinkedHashMap<>(),
keyTransform, valueTransform);
+ }
+
+ /**
+ * A variant of collectEntries for arrays with separate functions for
transforming the keys and values.
+ * The supplied collector map is used as the destination for transformed
entries.
+ *
+ * @param self an array
+ * @param collector the Map into which the transformed entries are put
+ * @param keyTransform a function for transforming array elements into
keys
+ * @param valueTransform a function for transforming array elements into
values
+ * @return the collector with all transformed values added to it
+ * @since 5.0.0
+ */
+ public static <K, V, E> Map<K, V> collectEntries(E[] self, Map<K, V>
collector, Function<? super E, K> keyTransform, Function<? super E, V>
valueTransform) {
+ return collectEntries(new ArrayIterator<>(self), collector,
keyTransform, valueTransform);
+ }
+
+ /**
+ * A variant of collectEntries for arrays with separate functions for
transforming the keys and values.
+ * <pre class="groovyTestCase">
+ * String[] languages = ['Groovy', 'Java', 'Kotlin', 'Scala']
+ * def firstLetter = s -> s[0]
+ * assert languages.collectEntries(firstLetter, String::size) == [G:6,
J:4, K:6, S:5]
+ * </pre>
+ *
+ * @param self an array
+ * @param keyTransform a function for transforming array elements into
keys
+ * @param valueTransform a function for transforming array elements into
values
+ * @return a Map of the transformed entries
+ * @since 5.0.0
+ */
+ public static <K, V, E> Map<K, V> collectEntries(E[] self, Function<?
super E, K> keyTransform, Function<? super E, V> valueTransform) {
+ return collectEntries(new ArrayIterator<>(self), new
LinkedHashMap<>(), keyTransform, valueTransform);
+ }
+
+ /**
+ * A variant of collectEntries for Maps with separate functions for
transforming the keys and values.
+ * The supplied collector map is used as the destination for transformed
entries.
+ *
+ * @param self a Map
+ * @param collector the Map into which the transformed entries are put
+ * @param keyTransform a function for transforming Map keys
+ * @param valueTransform a function for transforming Map values
+ * @return the collector with all transformed values added to it
+ * @since 5.0.0
+ */
+ public static <K, V, X, Y> Map<K, V> collectEntries(Map<X, Y> self, Map<K,
V> collector, Function<? super X, K> keyTransform, Function<? super Y, V>
valueTransform) {
+ for (Map.Entry<X, Y> entry : self.entrySet()) {
+ addEntry(collector,
Tuple2.tuple(keyTransform.apply(entry.getKey()),
valueTransform.apply(entry.getValue())));
+ }
+ return collector;
+ }
+
+ /**
+ * A variant of collectEntries for Maps with separate functions for
transforming the keys and values.
+ *
+ * @param self a Map
+ * @param keyTransform a function for transforming Map keys
+ * @param valueTransform a function for transforming Map values
+ * @return a Map of the transformed entries
+ * @since 5.0.0
+ */
+ public static <K, V, X, Y> Map<K, V> collectEntries(Map<X, Y> self,
Function<? super X, K> keyTransform, Function<? super Y, V> valueTransform) {
+ return collectEntries(self, new LinkedHashMap<>(), keyTransform,
valueTransform);
+ }
+
+ /**
+ * A variant of withCollectedValues for Iterators.
+ *
+ * @param keys an Iterator
+ * @param collector the Map into which the transformed entries are put
+ * @param valueTransform a function for transforming Iterator elements
into values
+ * @return the collector with all transformed values added to it
+ * @see #withCollectedValues(Iterable, Map, Function)
+ * @since 5.0.0
+ */
+ public static <K, V> Map<K, V> withCollectedValues(Iterator<K> keys,
Map<K, V> collector, Function<? super K, V> valueTransform) {
+ return collectEntries(keys, collector, Function.identity(),
valueTransform);
+ }
+
+ /**
+ * A variant of withCollectedValues for Iterators.
+ *
+ * @param keys an Iterator
+ * @param valueTransform a function for transforming Iterator elements
into values
+ * @return a Map of the transformed entries
+ * @see #withCollectedValues(Iterable, Function)
+ * @since 5.0.0
+ */
+ public static <K, V> Map<K, V> withCollectedValues(Iterator<K> keys,
Function<? super K, V> valueTransform) {
+ return withCollectedValues(keys, new LinkedHashMap<>(),
valueTransform);
+ }
+
+ /**
+ * Transform Iterable elements into Map entries with keys unchanged
+ * and values transformed using the supplied function.
+ * The supplied collector map is used as the destination for transformed
entries.
+ * <pre class="groovyTestCase">
+ * def languages = ['Groovy', 'Java', 'Kotlin', 'Scala']
+ * assert languages.withCollectedValues([Clojure:7], String::size) ==
+ * [Clojure:7, Groovy:6, Java:4, Kotlin:6, Scala:5]
+ * </pre>
+ *
+ * @param keys an Iterable
+ * @param collector the Map into which the transformed entries are put
+ * @param valueTransform a function for transforming Iterable elements
into values
+ * @return the collector with all transformed values added to it
+ * @since 5.0.0
+ */
+ public static <K, V> Map<K, V> withCollectedValues(Iterable<K> keys,
Map<K, V> collector, Function<? super K, V> valueTransform) {
+ return collectEntries(keys.iterator(), collector, Function.identity(),
valueTransform);
+ }
+
+ /**
+ * Transform Iterable elements into Map entries with keys unchanged
+ * and values transformed using the supplied function.
+ * <pre class="groovyTestCase">
+ * def languages = ['Groovy', 'Java', 'Kotlin', 'Scala']
+ * assert languages.withCollectedValues(String::size) ==
+ * [Groovy:6, Java:4, Kotlin:6, Scala:5]
+ * </pre>
+ *
+ * @param keys an Iterable
+ * @param valueTransform a function for transforming Iterable elements
into values
+ * @return a Map of the transformed entries
+ * @since 5.0.0
+ */
+ public static <K, V> Map<K, V> withCollectedValues(Iterable<K> keys,
Function<? super K, V> valueTransform) {
+ return withCollectedValues(keys.iterator(), new LinkedHashMap<>(),
valueTransform);
+ }
+
+ /**
+ * A variant of withCollectedValues for arrays.
+ *
+ * @param keys an array
+ * @param collector the Map into which the transformed entries are put
+ * @param valueTransform a function for transforming array elements into
values
+ * @return the collector with all transformed values added to it
+ * @since 5.0.0
+ */
+ public static <K, V> Map<K, V> withCollectedValues(K[] keys, Map<K, V>
collector, Function<? super K, V> valueTransform) {
+ return collectEntries(new ArrayIterator<>(keys), collector,
Function.identity(), valueTransform);
+ }
+
+ /**
+ * A variant of withCollectedValues for arrays.
+ *
+ * @param keys an array
+ * @param valueTransform a function for transforming array elements into
values
+ * @return a Map of the transformed entries
+ * @since 5.0.0
+ */
+ public static <K, V> Map<K, V> withCollectedValues(K[] keys, Function<?
super K, V> valueTransform) {
+ return withCollectedValues(new ArrayIterator<>(keys), new
LinkedHashMap<>(), valueTransform);
+ }
+
+ /**
+ * Transform a Maps' values leaving the keys unchanged.
+ * Similar to {@link #withCollectedValues(Iterable, Map, Function)} but
for Maps.
+ * <pre class="groovyTestCase">
+ * def lengths = [Groovy:6, Java:4, Kotlin:6, Scala:5]
+ * def squared = e -> e ** 2
+ * assert lengths.collectValues([Clojure:49], squared) ==
+ * [Clojure:49, Groovy:36, Java:16, Kotlin:36, Scala:25]
+ * </pre>
+ *
+ * @param keys a Map
+ * @param collector the Map into which the transformed entries are put
+ * @param valueTransform a function for transforming Map values
+ * @return the collector with all transformed values added to it
+ * @since 5.0.0
+ */
+ public static <K, V> Map<K, V> collectValues(Map<K, V> keys, Map<K, V>
collector, Function<? super V, V> valueTransform) {
+ for (Map.Entry<K, V> entry : keys.entrySet()) {
+ addEntry(collector, Tuple2.tuple(entry.getKey(),
valueTransform.apply(entry.getValue())));
+ }
+ return collector;
+ }
+
+ /**
+ * Transform a Maps' values leaving the keys unchanged.
+ * Similar to {@link #withCollectedValues(Iterable, Function)} but for
Maps.
+ * <pre class="groovyTestCase">
+ * def lengths = [Groovy:6, Java:4, Kotlin:6, Scala:5]
+ * def squared = e -> e ** 2
+ * assert lengths.collectValues(squared) == [Groovy:36, Java:16,
Kotlin:36, Scala:25]
+ * </pre>
+ *
+ * @param keys a Map
+ * @param valueTransform a function for transforming Map values
+ * @return a Map of the transformed entries
+ * @since 5.0.0
+ */
+ public static <K, V> Map<K, V> collectValues(Map<K, V> keys, Function<?
super V, V> valueTransform) {
+ return collectValues(keys, new LinkedHashMap<>(), valueTransform);
+ }
+
+ /**
+ * A variant of withCollectedKeys for Iterators.
+ *
+ * @param values an Iterator
+ * @param collector the Map into which the transformed entries are put
+ * @param keyTransform a function for transforming Iterator elements into
values
+ * @return the collector with all transformed values added to it
+ * @see #withCollectedKeys(Iterable, Map, Function)
+ * @since 5.0.0
+ */
+ public static <K, V> Map<K, V> withCollectedKeys(Iterator<V> values,
Map<K, V> collector, Function<? super V, K> keyTransform) {
+ return collectEntries(values, collector, keyTransform,
Function.identity());
+ }
+
+ /**
+ * A variant of withCollectedKeys for Iterators.
+ *
+ * @param values an Iterator
+ * @param keyTransform a function for transforming Iterator elements into
values
+ * @return a Map of the transformed entries
+ * @see #withCollectedKeys(Iterable, Function)
+ * @since 5.0.0
+ */
+ public static <K, V> Map<K, V> withCollectedKeys(Iterator<V> values,
Function<? super V, K> keyTransform) {
+ return withCollectedKeys(values, new LinkedHashMap<>(), keyTransform);
+ }
+
+ /**
+ * Transform Iterable elements into Map entries with values unchanged
+ * and keys transformed using the supplied function.
+ * The supplied map is used as the destination for transformed entries.
+ * <pre class="groovyTestCase">
+ * def languages = ['Groovy', 'Java', 'Kotlin', 'Scala']
+ * def firstLetter = s -> s[0]
Review Comment:
💬 6 similar findings have been found in this PR
---
*[UnescapedEntity](https://errorprone.info/bugpattern/UnescapedEntity):*
This HTML entity is invalid. Enclosing the code in this <pre>/<code> tag with a
{@code } block will force Javadoc to interpret HTML literally.
---
```suggestion
* <pre class="groovyTestCase">{@code
```
---
<details><summary><b>🔎 Expand here to view all instances of this
finding</b></summary><br/>
<div align=\"center\">
| **File Path** | **Line Number** |
| -----------
> Potential additional DGM collectEntries variants
> ------------------------------------------------
>
> Key: GROOVY-10879
> URL: https://issues.apache.org/jira/browse/GROOVY-10879
> Project: Groovy
> Issue Type: Improvement
> Reporter: Paul King
> Assignee: Paul King
> Priority: Major
>
> As discussed in the mailing list:
> https://lists.apache.org/thread/7rd6wdblnh7bmdf6socjnbf21crg37g0
> Existing variants:
> {code}
> var languages = ['Kotlin', 'Groovy', 'Java', 'Clojure']
> assert languages.collectEntries{ [it.toLowerCase(), it.size()] } ==
> [kotlin:6, groovy:6, java:4, clojure:7]
> assert languages.collectEntries{ [it.toLowerCase(), it] } ==
> [kotlin:'Kotlin', groovy:'Groovy', java:'Java', clojure:'Clojure']
> assert languages.collectEntries(Scala:5){ [it, it.size()] } ==
> [Scala:5, Kotlin:6, Groovy:6, Java:4, Clojure:7]
> {code}
> Proposed new variants:
> {code}
> assert languages.collectEntries(String::toLowerCase, String::size) ==
> [kotlin:6, groovy:6, java:4, clojure:7]
> assert languages.collectByValue(String::toLowerCase) == [kotlin:'Kotlin',
> groovy:'Groovy', java:'Java', clojure:'Clojure']
> assert languages.collectByKey([Scala:5], String::size) == [Scala:5, Kotlin:6,
> Groovy:6, Java:4, Clojure:7]
> def squared = e -> e ** 2
> assert [Scala:5, Kotlin:6, Groovy:6, Java:4,
> Clojure:7].collectEntries(String::toLowerCase, squared) ==
> [scala:25, kotlin:36, groovy:36, java:16, clojure:49]
> {code}
--
This message was sent by Atlassian Jira
(v8.20.10#820010)