|
13 | 13 | */ |
14 | 14 | package com.querydsl.core.group; |
15 | 15 |
|
16 | | -import com.mysema.commons.lang.CloseableIterator; |
17 | 16 | import com.querydsl.core.FetchableQuery; |
18 | | -import com.querydsl.core.Tuple; |
19 | 17 | import com.querydsl.core.types.Expression; |
20 | | -import com.querydsl.core.types.FactoryExpression; |
21 | | -import com.querydsl.core.types.FactoryExpressionUtils; |
22 | | -import com.querydsl.core.types.Projections; |
23 | 18 | import java.util.ArrayList; |
| 19 | +import java.util.LinkedHashMap; |
24 | 20 | import java.util.List; |
25 | | -import java.util.Objects; |
| 21 | +import java.util.Map; |
26 | 22 |
|
27 | 23 | /** |
28 | 24 | * Provides aggregated results as a list |
|
33 | 29 | */ |
34 | 30 | public class GroupByList<K, V> extends AbstractGroupByTransformer<K, List<V>> { |
35 | 31 |
|
| 32 | + private final GroupByMap<K, V> mapTransformer; |
| 33 | + |
36 | 34 | GroupByList(Expression<K> key, Expression<?>... expressions) { |
37 | 35 | super(key, expressions); |
| 36 | + mapTransformer = |
| 37 | + new GroupByMap<K, V>(key, expressions) { |
| 38 | + @Override |
| 39 | + protected Map<K, V> transform(Map<K, Group> groups) { |
| 40 | + Map<K, V> results = |
| 41 | + new LinkedHashMap<K, V>((int) Math.ceil(groups.size() / 0.75), 0.75f); |
| 42 | + for (Map.Entry<K, Group> entry : groups.entrySet()) { |
| 43 | + results.put(entry.getKey(), GroupByList.this.transform(entry.getValue())); |
| 44 | + } |
| 45 | + return results; |
| 46 | + } |
| 47 | + }; |
38 | 48 | } |
39 | 49 |
|
40 | 50 | @Override |
41 | 51 | public List<V> transform(FetchableQuery<?, ?> query) { |
42 | | - // create groups |
43 | | - FactoryExpression<Tuple> expr = FactoryExpressionUtils.wrap(Projections.tuple(expressions)); |
44 | | - boolean hasGroups = false; |
45 | | - for (Expression<?> e : expr.getArgs()) { |
46 | | - hasGroups |= e instanceof GroupExpression; |
47 | | - } |
48 | | - if (hasGroups) { |
49 | | - expr = withoutGroupExpressions(expr); |
50 | | - } |
51 | | - final CloseableIterator<Tuple> iter = query.select(expr).iterate(); |
52 | | - |
53 | | - List<V> list = new ArrayList<>(); |
54 | | - GroupImpl group = null; |
55 | | - K groupId = null; |
56 | | - while (iter.hasNext()) { |
57 | | - @SuppressWarnings("unchecked") // This type is mandated by the key type |
58 | | - K[] row = (K[]) iter.next().toArray(); |
59 | | - if (group == null) { |
60 | | - group = new GroupImpl(groupExpressions, maps); |
61 | | - groupId = row[0]; |
62 | | - } else if (!Objects.equals(groupId, row[0])) { |
63 | | - list.add(transform(group)); |
64 | | - group = new GroupImpl(groupExpressions, maps); |
65 | | - groupId = row[0]; |
66 | | - } |
67 | | - group.add(row); |
68 | | - } |
69 | | - if (group != null) { |
70 | | - list.add(transform(group)); |
71 | | - } |
72 | | - iter.close(); |
73 | | - return list; |
| 52 | + Map<K, V> result = mapTransformer.transform(query); |
| 53 | + return new ArrayList<V>(result.values()); |
74 | 54 | } |
75 | 55 |
|
76 | 56 | @SuppressWarnings("unchecked") |
|
0 commit comments