Skip to content

Commit 7decdab

Browse files
committed
Add chain methods support groupBy(), indexBy() and countBy().
1 parent f16ffc8 commit 7decdab

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

src/main/java/com/github/underscore/$.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,14 +1685,22 @@ public <F extends Comparable<? super F>> Chain<T> sortBy(final Function1<T, F> f
16851685
return new Chain<T>($.sortBy(list, func));
16861686
}
16871687

1688-
public <K, V> Chain<Map<K, V>> toMap() {
1689-
return new Chain<Map<K, V>>($.toMap((Iterable<Map.Entry<K, V>>) list));
1690-
}
1691-
16921688
public <K, V extends Comparable<? super V>> Chain<Map<K, V>> sortBy(final K key) {
16931689
return new Chain<Map<K, V>>($.sortBy((List<Map<K, V>>) list, key));
16941690
}
16951691

1692+
public <F> Chain<Map<F, List<T>>> groupBy(final Function1<T, F> func) {
1693+
return new Chain<Map<F, List<T>>>($.groupBy(list, func));
1694+
}
1695+
1696+
public Chain<Map<Object, List<T>>> indexBy(final String property) {
1697+
return new Chain<Map<Object, List<T>>>($.indexBy(list, property));
1698+
}
1699+
1700+
public <F> Chain<Map<F, Integer>> countBy(final Function1<T, F> func) {
1701+
return new Chain<Map<F, Integer>>($.countBy(list, func));
1702+
}
1703+
16961704
public Chain<T> shuffle() {
16971705
return new Chain<T>($.shuffle(list));
16981706
}
@@ -1750,6 +1758,10 @@ public <T> Chain<T> limit(final int size) {
17501758
return new Chain<T>((List<T>) list.subList(0, size));
17511759
}
17521760

1761+
public <K, V> Chain<Map<K, V>> toMap() {
1762+
return new Chain<Map<K, V>>($.toMap((Iterable<Map.Entry<K, V>>) list));
1763+
}
1764+
17531765
public T item() {
17541766
return item;
17551767
}

src/test/java/com/github/underscore/CollectionsTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,14 @@ public Integer apply(Integer item) {
785785
}
786786
});
787787
assertEquals("[5, 4, 6, 3, 1, 2]", resultObj.toString());
788+
final List<Integer> resultChain =
789+
$.chain(asList(1, 2, 3, 4, 5, 6)).sortBy(
790+
new Function1<Integer, Integer>() {
791+
public Integer apply(Integer item) {
792+
return Double.valueOf(Math.sin(item) * 1000).intValue();
793+
}
794+
}).value();
795+
assertEquals("[5, 4, 6, 3, 1, 2]", resultChain.toString());
788796
}
789797

790798
/*
@@ -836,6 +844,14 @@ public Double apply(Double num) {
836844
}
837845
});
838846
assertEquals("{1.0=[1.3], 2.0=[2.1, 2.4]}", resultObj.toString());
847+
final Map<Double, List<Double>> resultChain =
848+
(Map<Double, List<Double>>) $.chain(asList(1.3, 2.1, 2.4)).groupBy(
849+
new Function1<Double, Double>() {
850+
public Double apply(Double num) {
851+
return Math.floor(num);
852+
}
853+
}).item();
854+
assertEquals("{1.0=[1.3], 2.0=[2.1, 2.4]}", resultChain.toString());
839855
}
840856

841857
/*
@@ -866,6 +882,10 @@ public String toString() {
866882
final Map<String, List<Person>> resultObj =
867883
new $(asList(new Person("moe", 40), new Person("larry", 50), new Person("curly", 60))).indexBy("age");
868884
assertEquals("{40=[moe, 40], 50=[larry, 50], 60=[curly, 60]}", resultObj.toString());
885+
final Map<String, List<Person>> resultChain =
886+
(Map<String, List<Person>>) $.chain(asList(new Person("moe", 40), new Person("larry", 50),
887+
new Person("curly", 60))).indexBy("age").item();
888+
assertEquals("{40=[moe, 40], 50=[larry, 50], 60=[curly, 60]}", resultChain.toString());
869889
final Map<String, List<Person>> result2 =
870890
$.indexBy(asList(new Person("moe", 40), new Person("larry", 50), new Person("curly", 60)), "age2");
871891
assertEquals("{null=[moe, 40, larry, 50, curly, 60]}", result2.toString());
@@ -905,6 +925,15 @@ public String apply(Person person) {
905925
}
906926
});
907927
assertEquals("{moe=2, curly=1}", resultObj.toString());
928+
final Map<String, Integer> resultChain =
929+
(Map<String, Integer>) $.chain(asList(new Person("moe", 40), new Person("moe", 50),
930+
new Person("curly", 60))).countBy(
931+
new Function1<Person, String>() {
932+
public String apply(Person person) {
933+
return person.name;
934+
}
935+
}).item();
936+
assertEquals("{moe=2, curly=1}", resultChain.toString());
908937
}
909938

910939
/*

0 commit comments

Comments
 (0)