Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

rided CollectionUtils.sort(Map, Comparator) because Map#sort(Comparat…

…or) has been supported since 1.7.2
  • Loading branch information...
commit 38eb0773800ba24690aa03c23c05ccc4bca5c232 1 parent d5972b3
@nobeans nobeans authored
View
15 src/main/groovy/org/jggug/kobo/commons/lang/CollectionUtils.groovy
@@ -20,11 +20,6 @@ class CollectionUtils {
static void extendMetaClass() {
ExpandoMetaClass.enableGlobally()
extend(List, Object[], Iterator, Map)
-
- // because there isn't sort(Comparator) only for Map
- Map.metaClass.sort << { Comparator comparator ->
- return CollectionUtils.sort(delegate, comparator)
- }
}
private static extend(Class... classes) {
@@ -39,20 +34,14 @@ class CollectionUtils {
}
static sort(def self, Closure... closures) {
+ println "sort(self, closures as List): ${self.class}: ${self}"
return sort(self, closures as List)
}
static sort(def self, List<Closure> closures) {
+ println "self.sort(new OrderBy(closures): ${self.class}: ${self}"
return self.sort(new OrderBy(closures))
}
- // why isn't there this method in org.codehaus.groovy.runtime.DefaultGroovyMethods ??
- static Map sort(Map self, Comparator comparator) {
- Map result = new LinkedHashMap()
- self.entrySet().asList().sort(comparator).each { entry ->
- result.put(entry.key, entry.value)
- }
- return result
- }
}
View
17 src/test/groovy/org/jggug/kobo/commons/lang/CollectionUtilsTest.groovy
@@ -72,12 +72,11 @@ class CollectionUtilsTest extends GroovyTestCase {
assert CollectionUtils.sort(data.iterator(), [{it.key}, {it.value}]) in Iterator
}
- void testSort_Util_Map() {
+ void testSort_Util_Map() { // you can use only key. 'it' means key.
def data = [d:2, e:1, b:1, aa:2, c:2]
- assert "[aa:2, b:1, c:2, d:2, e:1]" == CollectionUtils.sort(data, [{it.key}, {it.value}]).toString()
- assert "[b:1, e:1, aa:2, c:2, d:2]" == CollectionUtils.sort(data, [{it.value}, {it.key}]).toString()
- assert "[b:1, e:1, aa:2, c:2, d:2]" == CollectionUtils.sort(data, new OrderBy([{it.value}, {it.key}])).toString()
- assert CollectionUtils.sort(data, [{it.key}, {it.value}]) in Map
+ assert "[aa:2, b:1, c:2, d:2, e:1]" == CollectionUtils.sort(data, [{it}, {data[it]}]).toString()
+ assert "[b:1, e:1, aa:2, c:2, d:2]" == CollectionUtils.sort(data, [{data[it]}, {it}]).toString()
+ assert CollectionUtils.sort(data, [{it}, {data[it]}]) in Map
}
void testSort_Util_aVarietyOfNotifications() {
@@ -130,11 +129,11 @@ class CollectionUtilsTest extends GroovyTestCase {
assert data.iterator().sort([{it.key}, {it.value}]) in Iterator
}
- void testSort_MOP_Map() {
+ void testSort_MOP_Map() { // you can use only key. 'it' means key.
def data = [d:2, e:1, b:1, aa:2, c:2]
- assert "[aa:2, b:1, c:2, d:2, e:1]" == data.sort([{it.key}, {it.value}]).toString()
- assert "[b:1, e:1, aa:2, c:2, d:2]" == data.sort([{it.value}, {it.key}]).toString()
- assert data.sort([{it.key}, {it.value}]) in Map
+ assert "[aa:2, b:1, c:2, d:2, e:1]" == data.sort([{it}, {data[it]}]).toString()
+ assert "[b:1, e:1, aa:2, c:2, d:2]" == data.sort([{data[it]}, {it}]).toString()
+ assert data.sort([{it}, {data[it]}]) in Map
}
void testSort_MOP_aVarietyOfNotifications() {
Please sign in to comment.
Something went wrong with that request. Please try again.