Skip to content

Commit

Permalink
- slightly improve GroupBy
Browse files Browse the repository at this point in the history
- small fix on tests
  • Loading branch information
brunovinicius committed Jun 9, 2015
1 parent bebcff8 commit 00a0811
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 34 deletions.
34 changes: 18 additions & 16 deletions src/main/java/br/com/zbra/androidlinq/GroupByStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

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

import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import static br.com.zbra.androidlinq.Linq.stream;


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

private Iterator<Grouping<TKey, TElement>> getGroupingIterator(Iterator<T> iterator) {
HashMap<TKey, List<TElement>> map = new HashMap<>();
HashMap<TKey, GroupingImpl<TKey, TElement>> map = new HashMap<>();
List<Grouping<TKey, TElement>> groupings = new ArrayList<>();

while (iterator.hasNext()) {
T t = iterator.next();
TKey key = keySelector.select(t);
TElement element = elementSelector.select(t);

List<TElement> elements = map.get(key);
if (elements == null) {
elements = new ArrayList<>();
map.put(key, elements);
GroupingImpl<TKey, TElement> grouping = map.get(key);
if (grouping == null) {
grouping = new GroupingImpl<>(key);
map.put(key, grouping);
groupings.add(grouping);
}

elements.add(element);
}

List<Grouping<TKey, TElement>> groupings = new ArrayList<>();
for (Map.Entry<TKey, List<TElement>> entry : map.entrySet()) {
groupings.add(new GroupingImpl<>(entry.getKey(), Linq.stream(entry.getValue())));
grouping.source.add(element);
}

return groupings.iterator();
}

private static class GroupingImpl<TKey, TElement> implements Grouping<TKey, TElement> {
private final TKey key;
private final Stream<TElement> elements;
private final List<TElement> source;

private GroupingImpl(TKey key, Stream<TElement> elements) {
private GroupingImpl(TKey key) {
this.key = key;
this.elements = elements;
this.source = new ArrayList<>();
}

public TKey getKey() {
return key;
}

public Stream<TElement> getElements() {
return elements;
return stream(source);
}
}
}
35 changes: 17 additions & 18 deletions src/test/groovy/br/com/zbra/androidlinq/StreamTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class StreamTest extends GroovyTestCase {
assert reverseResult.get(1).elements.toList() == [00, 20, 40, 60, 80]
}

@SuppressWarnings("GroovyAssignabilityCheck")
void testOrderBy() {
def integers = 0..9
def integersDescending = 9..0
Expand All @@ -116,7 +117,6 @@ class StreamTest extends GroovyTestCase {
.orderBy({ n -> n }, numbersComparator)
.toList()


// orderBy count()
assert shuffledItems.size() ==
stream(shuffledItems)
Expand All @@ -142,7 +142,6 @@ class StreamTest extends GroovyTestCase {
assert stream(shuffledItems).orderByDescending({ n -> n }).toList() ==
stream(shuffledItems).orderBy({ n -> n }).reverse().toList()


// thenBy & thenByDescending over orderBy
def matrix = [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]]
def shuffledMatrix = []
Expand All @@ -152,53 +151,53 @@ class StreamTest extends GroovyTestCase {

assert [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]] ==
stream(shuffledMatrix)
.orderBy({n -> n['0']})
.thenBy({n -> n['1']})
.orderBy({ n -> n[0] })
.thenBy({ n -> n[1] })
.toList()

assert [[1, 3], [1, 2], [1, 1], [2, 3], [2, 2], [2, 1], [3, 3], [3, 2], [3, 1]] ==
stream(shuffledMatrix)
.orderBy({n -> n['0']})
.thenByDescending({ n -> n['1']})
.orderBy({ n -> n[0] })
.thenByDescending({ n -> n[1] })
.toList()


assert [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]] ==
stream(shuffledMatrix)
.orderBy({n -> n['0']})
.thenBy({n -> n['1']}, numbersComparator)
.orderBy({ n -> n[0] })
.thenBy({ n -> n[1] }, numbersComparator)
.toList()

assert [[1, 3], [1, 2], [1, 1], [2, 3], [2, 2], [2, 1], [3, 3], [3, 2], [3, 1]] ==
stream(shuffledMatrix)
.orderBy({n -> n['0']})
.thenByDescending({ n -> n['1']}, numbersComparator)
.orderBy({ n -> n[0] })
.thenByDescending({ n -> n[1] }, numbersComparator)
.toList()

// thenBy & thenByDescending over orderByDescending
assert [[3, 1], [3, 2], [3, 3], [2, 1], [2, 2], [2, 3], [1, 1], [1, 2], [1, 3]] ==
stream(shuffledMatrix)
.orderByDescending({n -> n['0']})
.thenBy({n -> n['1']})
.orderByDescending({ n -> n[0] })
.thenBy({ n -> n[1] })
.toList()

assert [[3, 3], [3, 2], [3, 1], [2, 3], [2, 2], [2, 1], [1, 3], [1, 2], [1, 1]] ==
stream(shuffledMatrix)
.orderByDescending({n -> n['0']})
.thenByDescending({ n -> n['1']})
.orderByDescending({ n -> n[0] })
.thenByDescending({ n -> n[1] })
.toList()


assert [[3, 1], [3, 2], [3, 3], [2, 1], [2, 2], [2, 3], [1, 1], [1, 2], [1, 3]] ==
stream(shuffledMatrix)
.orderByDescending({n -> n['0']})
.thenBy({n -> n['1']}, numbersComparator)
.orderByDescending({ n -> n[0] })
.thenBy({ n -> n[1] }, numbersComparator)
.toList()

assert [[3, 3], [3, 2], [3, 1], [2, 3], [2, 2], [2, 1], [1, 3], [1, 2], [1, 1]] ==
stream(shuffledMatrix)
.orderByDescending({n -> n['0']})
.thenByDescending({ n -> n['1']}, numbersComparator)
.orderByDescending({ n -> n[0] })
.thenByDescending({ n -> n[1] }, numbersComparator)
.toList()
}

Expand Down

0 comments on commit 00a0811

Please sign in to comment.