Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GROOVY-6992: Added support for collecting over sets #498

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -2631,6 +2631,50 @@ public static <S,T> List<T> collect(Collection<S> self, @ClosureParams(FirstPara
return (List<T>) collect(self, new ArrayList<T>(self.size()), transform);
}

/**
* Iterates through this Set transforming each value into a new value using the <code>transform</code> closure
* and adding it to the supplied <code>collector</code>.
* <pre class="groovyTestCase">assert [1,2,3] as HashSet == [2,4,5,6].collect { (int)(it / 2) }</pre>
*
* @param self a set
* @param collector the Set to which the transformed values are added
* @param transform the closure used to transform each item of the collection
* @return the collector with all transformed values added to it
*/
public static <T,E> Set<T> collect(Set<E> self, Set<T> collector, @ClosureParams(FirstParam.FirstGenericType.class) Closure<? extends T> transform) {
return collectSet(self, collector, transform);
}

/**
* Iterates through this set transforming each entry into a new value using the <code>transform</code> closure
* returning a list of transformed values.
* <pre class="groovyTestCase">assert [1,2,3] as Set == [2,4,5,6].collect { (int) (it / 2) }</pre>
*
* @param self a set
* @param transform the closure used to transform each item of the collection
* @return a List of the transformed values
*/
public static <T,E> Set<T> collect(Set<E> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure<? extends T> transform) {
return collectSet(self, DefaultGroovyMethods.<T>defaultSet(), transform);
}

/**
* Iterates through this set transforming each entry into a new value using Closure.IDENTITY
* as a transformer, basically returning a set of items copied from the original collection.
* <pre class="groovyTestCase">assert [1,2,3] as Set == ([1,2,3] as Set).collect()</pre>
*
* @param self a set
* @return a Set of the transformed values
* @see Closure#IDENTITY
*/
public static <T,E> Set<T> collect(Set<E> self) {
return collectSet(self, DefaultGroovyMethods.<T>defaultSet(), Closure.IDENTITY);
}

private static <A> Set<A> defaultSet() {
return new LinkedHashSet<A>();
}

/**
* Iterates through this collection transforming each entry into a new value using Closure.IDENTITY
* as a transformer, basically returning a list of items copied from the original collection.
Expand Down Expand Up @@ -4063,7 +4107,7 @@ public static <T> Set<List<T>> permutations(List<T> self) {
* @since 2.2.0
*/
public static <T,V> List<V> permutations(Iterable<T> self, Closure<V> function) {
return collect(permutations(self),function);
return collect((Collection) permutations(self),function);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
*/
package org.codehaus.groovy.runtime;

import groovy.lang.Closure;
import groovy.lang.EmptyRange;
import groovy.lang.IntRange;
import groovy.lang.Range;
import groovy.transform.stc.ClosureParams;
import groovy.transform.stc.FirstParam;
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;

import java.io.Closeable;
Expand Down Expand Up @@ -127,6 +130,10 @@ private static Object cloneObject(Object orig) {
return null;
}

protected static <T,E> Set<T> collectSet(Set<E> self, Set<T> collector, @ClosureParams(FirstParam.FirstGenericType.class) Closure<? extends T> transform) {
return (Set<T>) DefaultGroovyMethods.collect((Collection) self, collector, transform);
}

protected static Collection createSimilarOrDefaultCollection(Object object) {
if (object instanceof Collection) {
return createSimilarCollection((Collection<?>) object);
Expand Down
4 changes: 2 additions & 2 deletions src/test/groovy/MapTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class MapTest extends GroovyTestCase {
assert map1 == control

map1 = [a:1, b:2]
map1.putAll(map3.entrySet().collect{ [it.value, it.key] as MapEntry })
map1.putAll(map3.entrySet().toList().collect{ [it.value, it.key] as MapEntry })
assert map1 == control

map1 = [a:1, b:2]
Expand All @@ -137,7 +137,7 @@ class MapTest extends GroovyTestCase {
assert control == map1 + map2.entrySet()
assert map1 == [a:1, b:2]

assert control == map1 + map3.entrySet().collect{ [it.value, it.key] as MapEntry }
assert control == map1 + map3.entrySet().toList().collect{ [it.value, it.key] as MapEntry }
assert map1 == [a:1, b:2]

map1 = [a:1, b:2]
Expand Down
16 changes: 16 additions & 0 deletions src/test/groovy/SetTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ class SetTest extends GroovyTestCase {
assert flat == [3, 4, 5, 6, 7, 46, 78, "erer"] as Set
}

void testCollect() {
def s1 = [1, 1, 2] as Set
assert(s1 instanceof Set)
def s2 = s1.collect {
(it + 1).toString()
}
assert(s2 == ["2", "3"] as Set)
def s3 = s1.collect {
s1
}
assert(s3 == [[1, 2] as Set] as Set)
s3.each {
assert(it instanceof Set)
}
}

void testFlattenSetOfMapsWithClosure() {
Set orig = [[a:1, b:2], [c:3, d:4]] as Set
def flat = orig.flatten{ it instanceof Map ? it.values() : it }
Expand Down