Skip to content

Commit

Permalink
Delete dependency on elements order from list transformation (#516)
Browse files Browse the repository at this point in the history
Co-authored-by: kholysz <[email protected]>
  • Loading branch information
kamilkrzywanski and aasyspl authored Aug 2, 2024
1 parent 02a3818 commit 7ccfbfc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,12 @@
*/
package com.querydsl.core.group;

import com.mysema.commons.lang.CloseableIterator;
import com.querydsl.core.FetchableQuery;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.FactoryExpression;
import com.querydsl.core.types.FactoryExpressionUtils;
import com.querydsl.core.types.Projections;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;
import java.util.Map;

/**
* Provides aggregated results as a list
Expand All @@ -33,44 +29,28 @@
*/
public class GroupByList<K, V> extends AbstractGroupByTransformer<K, List<V>> {

private final GroupByMap<K, V> mapTransformer;

GroupByList(Expression<K> key, Expression<?>... expressions) {
super(key, expressions);
mapTransformer =
new GroupByMap<K, V>(key, expressions) {
@Override
protected Map<K, V> transform(Map<K, Group> groups) {
Map<K, V> results =
new LinkedHashMap<K, V>((int) Math.ceil(groups.size() / 0.75), 0.75f);
for (Map.Entry<K, Group> entry : groups.entrySet()) {
results.put(entry.getKey(), GroupByList.this.transform(entry.getValue()));
}
return results;
}
};
}

@Override
public List<V> transform(FetchableQuery<?, ?> query) {
// create groups
FactoryExpression<Tuple> expr = FactoryExpressionUtils.wrap(Projections.tuple(expressions));
boolean hasGroups = false;
for (Expression<?> e : expr.getArgs()) {
hasGroups |= e instanceof GroupExpression;
}
if (hasGroups) {
expr = withoutGroupExpressions(expr);
}
final CloseableIterator<Tuple> iter = query.select(expr).iterate();

List<V> list = new ArrayList<>();
GroupImpl group = null;
K groupId = null;
while (iter.hasNext()) {
@SuppressWarnings("unchecked") // This type is mandated by the key type
K[] row = (K[]) iter.next().toArray();
if (group == null) {
group = new GroupImpl(groupExpressions, maps);
groupId = row[0];
} else if (!Objects.equals(groupId, row[0])) {
list.add(transform(group));
group = new GroupImpl(groupExpressions, maps);
groupId = row[0];
}
group.add(row);
}
if (group != null) {
list.add(transform(group));
}
iter.close();
return list;
Map<K, V> result = mapTransformer.transform(query);
return new ArrayList<V>(result.values());
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,35 @@ public void map3() {
Object[] array = tuple.toArray();

if (posts == null || !(postId == array[0] || postId != null && postId.equals(array[0]))) {
posts = new LinkedHashMap<Integer, Map<Integer, String>>();
expected.add(posts);
posts = findPostsById(expected, array[0]);
if (posts == null) {
posts = new LinkedHashMap<Integer, Map<Integer, String>>();
expected.add(posts);
}
}
postId = array[0];
@SuppressWarnings("unchecked")
Pair<Integer, Pair<Integer, String>> pair = (Pair<Integer, Pair<Integer, String>>) array[1];
Integer first = pair.getFirst();
Map<Integer, String> comments =
posts.computeIfAbsent(first, k -> new LinkedHashMap<Integer, String>());
Map<Integer, String> comments = posts.get(first);
if (comments == null) {
comments = new LinkedHashMap<Integer, String>();
posts.put(first, comments);
}
Pair<Integer, String> second = pair.getSecond();
comments.put(second.getFirst(), second.getSecond());
}
assertEquals(expected.toString(), actual.toString());
}

private Map<Integer, Map<Integer, String>> findPostsById(
List<Map<Integer, Map<Integer, String>>> allPosts, Object postId) {
for (Map<Integer, Map<Integer, String>> posts : allPosts) {
if (posts.containsKey(postId)) return posts;
}
return null;
}

@Test
public void map4() {
CloseableIterator<Map<Map<Integer, String>, String>> results =
Expand Down

0 comments on commit 7ccfbfc

Please sign in to comment.