You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2025/03/16 02:40:28 UTC
(groovy) branch GROOVY-11486 created (now a907045a25)
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a change to branch GROOVY-11486
in repository https://gitbox.apache.org/repos/asf/groovy.git
at a907045a25 GROOVY-11486: rework `flatten` type parameters, similar to `collectMany`
This branch includes the following new commits:
new a907045a25 GROOVY-11486: rework `flatten` type parameters, similar to `collectMany`
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(groovy) 01/01: GROOVY-11486: rework `flatten` type parameters, similar to `collectMany`
Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY-11486
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit a907045a252acb7ee795b8684ec9ca4f82ab2f3e
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sat Mar 15 21:40:08 2025 -0500
GROOVY-11486: rework `flatten` type parameters, similar to `collectMany`
---
.../groovy/runtime/DefaultGroovyMethods.java | 52 +++++++++++-----------
1 file changed, 26 insertions(+), 26 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 298eca81d1..807545e911 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -5730,8 +5730,8 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
* @return a flattened Collection
* @since 1.6.0
*/
- public static <T> Collection<T> flatten(Collection<T> self) {
- return flatten(self, createSimilarCollection(self), true);
+ public static <T, E> Collection<T> flatten(Collection<E> self) {
+ return flatten(self, (Collection<T>) createSimilarCollection(self), true);
}
/**
@@ -5743,8 +5743,8 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
* @return a flattened Collection
* @since 1.6.0
*/
- public static <T> Collection<T> flatten(Iterable<T> self) {
- return flatten(self, createSimilarCollection(self), true);
+ public static <T, E> Collection<T> flatten(Iterable<E> self) {
+ return flatten(self, (Collection<T>) createSimilarCollection(self), true);
}
/**
@@ -5764,8 +5764,8 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
* @return a flattened Collection
* @since 5.0.0
*/
- public static <T> Collection<T> flatten(Iterable<T> self, boolean flattenOptionals) {
- return flatten(self, createSimilarCollection(self), flattenOptionals);
+ public static <T, E> Collection<T> flatten(Iterable<E> self, boolean flattenOptionals) {
+ return flatten(self, (Collection<T>) createSimilarCollection(self), flattenOptionals);
}
/**
@@ -5777,7 +5777,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
* @return a flattened List
* @since 2.4.0
*/
- public static <T> List<T> flatten(List<T> self) {
+ public static <T, E> List<T> flatten(List<E> self) {
return (List<T>) flatten(self, true);
}
@@ -5790,7 +5790,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
* @return a flattened Set
* @since 2.4.0
*/
- public static <T> Set<T> flatten(Set<T> self) {
+ public static <T, E> Set<T> flatten(Set<E> self) {
return (Set<T>) flatten(self, true);
}
@@ -5808,12 +5808,12 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
* @return a flattened SortedSet
* @since 2.4.0
*/
- public static <T> SortedSet<T> flatten(SortedSet<T> self) {
+ public static <T, E> SortedSet<T> flatten(SortedSet<E> self) {
return (SortedSet<T>) flatten(self, true);
}
- private static <T> Collection<T> flatten(Iterable<T> elements, Collection<T> addTo, boolean flattenOptionals) {
- for (T element : elements) {
+ private static <T, E> Collection<T> flatten(Iterable<E> elements, Collection<T> addTo, boolean flattenOptionals) {
+ for (E element : elements) {
flattenSingle(element, addTo, flattenOptionals);
}
return addTo;
@@ -5871,7 +5871,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
* @see #collectNested(Iterable, Closure)
* @since 1.6.0
*/
- public static <T, E> Collection<T> flatten(Iterable<E> self, Closure<?> flattenUsing) {
+ public static <T, E> Collection<T> flatten(Iterable<E> self, @ClosureParams(value=FromString.class, options="?") Closure<?> flattenUsing) {
return flatten(self, true, flattenUsing);
}
@@ -5904,7 +5904,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
* @return a flattened Collection
* @since 5.0.0
*/
- public static <T, E> Collection<T> flatten(Iterable<E> self, boolean flattenOptionals, Closure<?> flattenUsing) {
+ public static <T, E> Collection<T> flatten(Iterable<E> self, boolean flattenOptionals, @ClosureParams(value=FromString.class, options="?") Closure<?> flattenUsing) {
return flatten(self, flattenOptionals, (Collection<T>) createSimilarCollection(self), flattenUsing);
}
@@ -5920,20 +5920,20 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
* @return a flattened Optional
* @since 5.0.0
*/
- public static <T> Collection<T> flatten(Optional<T> self) {
+ public static <T, E> Collection<T> flatten(Optional<E> self) {
List<T> result = new ArrayList<>();
- self.ifPresent(result::add);
+ self.ifPresent(e -> flattenSingle(e, result, true));
return result;
}
private static <T, E> Collection<T> flatten(Iterable<E> elements, boolean flattenOptionals, Collection<T> addTo, Closure<?> flattenUsing) {
- for (Object element : elements) {
+ for (E element : elements) {
if (element instanceof Collection) {
- flatten((Collection<T>) element, flattenOptionals, addTo, flattenUsing);
+ flatten((Collection<?>) element, flattenOptionals, addTo, flattenUsing);
} else if (element != null && element.getClass().isArray()) {
- flatten(new ArrayIterable<>((Object[]) element), flattenOptionals, addTo, flattenUsing);
+ flatten(DefaultTypeTransformation.primitiveArrayToUnmodifiableList(element), flattenOptionals, addTo, flattenUsing);
} else if (element instanceof Optional && flattenOptionals) {
- flatten(flatten((Optional<T>) element), flattenOptionals, addTo, flattenUsing);
+ flatten(flatten((Optional<?>) element), flattenOptionals, addTo, flattenUsing);
} else {
flattenSingle(element, flattenOptionals, addTo, flattenUsing);
}
@@ -5943,15 +5943,15 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
private static <T> void flattenSingle(Object element, boolean flattenOptionals, Collection<T> addTo, Closure<?> flattenUsing) {
Object flattened = flattenUsing.call(new Object[]{element});
- boolean returnedSelf = flattened == element;
+ boolean returnedSelf = (flattened == element);
if (!returnedSelf && flattened instanceof Collection) {
- List list = toList((Iterable) flattened);
+ List<?> list = toList((Iterable<?>) flattened);
if (list.size() == 1 && list.get(0) == element) {
returnedSelf = true;
}
}
if (flattened instanceof Collection && !returnedSelf) {
- addTo.addAll(flatten((Collection) flattened, flattenOptionals));
+ addTo.addAll(flatten((Collection<?>) flattened, flattenOptionals));
} else {
addTo.add((T) flattened);
}
@@ -5986,18 +5986,18 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
* @see #collectMany(Iterable, Closure)
* @since 5.0.0
*/
- public static Collection<Object> flattenMany(Iterable<?> self, Closure<?> transform) {
- return flattenMany(self, (Collection<Object>) createSimilarCollection(self), transform);
+ public static <T> Collection<T> flattenMany(Iterable<?> self, Closure<?> transform) {
+ return flattenMany(self, (Collection<T>) createSimilarCollection(self), transform);
}
- private static Collection<Object> flattenMany(Iterable<?> self, Collection<Object> addTo, Closure<?> flattenUsing) {
+ private static <T> Collection<T> flattenMany(Iterable<?> self, Collection<T> addTo, Closure<?> flattenUsing) {
for (Object element : self) {
flattenSingle(element, addTo, flattenUsing);
}
return addTo;
}
- private static void flattenSingle(Object element, Collection<Object> addTo, Closure<?> transform) {
+ private static <T> void flattenSingle(Object element, Collection<T> addTo, Closure<?> transform) {
if (element instanceof Collection) {
addTo.addAll(flattenMany((Collection) element, addTo, transform));
} else if (element != null && element.getClass().isArray()) {