Skip to content

Commit 00a0811

Browse files
committed
- slightly improve GroupBy
- small fix on tests
1 parent bebcff8 commit 00a0811

File tree

2 files changed

+35
-34
lines changed

2 files changed

+35
-34
lines changed

src/main/java/br/com/zbra/androidlinq/GroupByStream.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
import br.com.zbra.androidlinq.delegate.Selector;
44

5-
import java.util.*;
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.Iterator;
8+
import java.util.List;
9+
10+
import static br.com.zbra.androidlinq.Linq.stream;
611

712

813
class GroupByStream<T, TKey, TElement> extends AbstractStream<Grouping<TKey, TElement>> {
@@ -28,45 +33,42 @@ protected Iterator<Grouping<TKey, TElement>> reverseIterator() {
2833
}
2934

3035
private Iterator<Grouping<TKey, TElement>> getGroupingIterator(Iterator<T> iterator) {
31-
HashMap<TKey, List<TElement>> map = new HashMap<>();
36+
HashMap<TKey, GroupingImpl<TKey, TElement>> map = new HashMap<>();
37+
List<Grouping<TKey, TElement>> groupings = new ArrayList<>();
3238

3339
while (iterator.hasNext()) {
3440
T t = iterator.next();
3541
TKey key = keySelector.select(t);
3642
TElement element = elementSelector.select(t);
3743

38-
List<TElement> elements = map.get(key);
39-
if (elements == null) {
40-
elements = new ArrayList<>();
41-
map.put(key, elements);
44+
GroupingImpl<TKey, TElement> grouping = map.get(key);
45+
if (grouping == null) {
46+
grouping = new GroupingImpl<>(key);
47+
map.put(key, grouping);
48+
groupings.add(grouping);
4249
}
4350

44-
elements.add(element);
45-
}
46-
47-
List<Grouping<TKey, TElement>> groupings = new ArrayList<>();
48-
for (Map.Entry<TKey, List<TElement>> entry : map.entrySet()) {
49-
groupings.add(new GroupingImpl<>(entry.getKey(), Linq.stream(entry.getValue())));
51+
grouping.source.add(element);
5052
}
5153

5254
return groupings.iterator();
5355
}
5456

5557
private static class GroupingImpl<TKey, TElement> implements Grouping<TKey, TElement> {
5658
private final TKey key;
57-
private final Stream<TElement> elements;
59+
private final List<TElement> source;
5860

59-
private GroupingImpl(TKey key, Stream<TElement> elements) {
61+
private GroupingImpl(TKey key) {
6062
this.key = key;
61-
this.elements = elements;
63+
this.source = new ArrayList<>();
6264
}
6365

6466
public TKey getKey() {
6567
return key;
6668
}
6769

6870
public Stream<TElement> getElements() {
69-
return elements;
71+
return stream(source);
7072
}
7173
}
7274
}

src/test/groovy/br/com/zbra/androidlinq/StreamTest.groovy

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class StreamTest extends GroovyTestCase {
9595
assert reverseResult.get(1).elements.toList() == [00, 20, 40, 60, 80]
9696
}
9797

98+
@SuppressWarnings("GroovyAssignabilityCheck")
9899
void testOrderBy() {
99100
def integers = 0..9
100101
def integersDescending = 9..0
@@ -116,7 +117,6 @@ class StreamTest extends GroovyTestCase {
116117
.orderBy({ n -> n }, numbersComparator)
117118
.toList()
118119

119-
120120
// orderBy count()
121121
assert shuffledItems.size() ==
122122
stream(shuffledItems)
@@ -142,7 +142,6 @@ class StreamTest extends GroovyTestCase {
142142
assert stream(shuffledItems).orderByDescending({ n -> n }).toList() ==
143143
stream(shuffledItems).orderBy({ n -> n }).reverse().toList()
144144

145-
146145
// thenBy & thenByDescending over orderBy
147146
def matrix = [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]]
148147
def shuffledMatrix = []
@@ -152,53 +151,53 @@ class StreamTest extends GroovyTestCase {
152151

153152
assert [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]] ==
154153
stream(shuffledMatrix)
155-
.orderBy({n -> n['0']})
156-
.thenBy({n -> n['1']})
154+
.orderBy({ n -> n[0] })
155+
.thenBy({ n -> n[1] })
157156
.toList()
158157

159158
assert [[1, 3], [1, 2], [1, 1], [2, 3], [2, 2], [2, 1], [3, 3], [3, 2], [3, 1]] ==
160159
stream(shuffledMatrix)
161-
.orderBy({n -> n['0']})
162-
.thenByDescending({ n -> n['1']})
160+
.orderBy({ n -> n[0] })
161+
.thenByDescending({ n -> n[1] })
163162
.toList()
164163

165164

166165
assert [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]] ==
167166
stream(shuffledMatrix)
168-
.orderBy({n -> n['0']})
169-
.thenBy({n -> n['1']}, numbersComparator)
167+
.orderBy({ n -> n[0] })
168+
.thenBy({ n -> n[1] }, numbersComparator)
170169
.toList()
171170

172171
assert [[1, 3], [1, 2], [1, 1], [2, 3], [2, 2], [2, 1], [3, 3], [3, 2], [3, 1]] ==
173172
stream(shuffledMatrix)
174-
.orderBy({n -> n['0']})
175-
.thenByDescending({ n -> n['1']}, numbersComparator)
173+
.orderBy({ n -> n[0] })
174+
.thenByDescending({ n -> n[1] }, numbersComparator)
176175
.toList()
177176

178177
// thenBy & thenByDescending over orderByDescending
179178
assert [[3, 1], [3, 2], [3, 3], [2, 1], [2, 2], [2, 3], [1, 1], [1, 2], [1, 3]] ==
180179
stream(shuffledMatrix)
181-
.orderByDescending({n -> n['0']})
182-
.thenBy({n -> n['1']})
180+
.orderByDescending({ n -> n[0] })
181+
.thenBy({ n -> n[1] })
183182
.toList()
184183

185184
assert [[3, 3], [3, 2], [3, 1], [2, 3], [2, 2], [2, 1], [1, 3], [1, 2], [1, 1]] ==
186185
stream(shuffledMatrix)
187-
.orderByDescending({n -> n['0']})
188-
.thenByDescending({ n -> n['1']})
186+
.orderByDescending({ n -> n[0] })
187+
.thenByDescending({ n -> n[1] })
189188
.toList()
190189

191190

192191
assert [[3, 1], [3, 2], [3, 3], [2, 1], [2, 2], [2, 3], [1, 1], [1, 2], [1, 3]] ==
193192
stream(shuffledMatrix)
194-
.orderByDescending({n -> n['0']})
195-
.thenBy({n -> n['1']}, numbersComparator)
193+
.orderByDescending({ n -> n[0] })
194+
.thenBy({ n -> n[1] }, numbersComparator)
196195
.toList()
197196

198197
assert [[3, 3], [3, 2], [3, 1], [2, 3], [2, 2], [2, 1], [1, 3], [1, 2], [1, 1]] ==
199198
stream(shuffledMatrix)
200-
.orderByDescending({n -> n['0']})
201-
.thenByDescending({ n -> n['1']}, numbersComparator)
199+
.orderByDescending({ n -> n[0] })
200+
.thenByDescending({ n -> n[1] }, numbersComparator)
202201
.toList()
203202
}
204203

0 commit comments

Comments
 (0)