Add GroupBy.sortedSet and sortedMap

This commit is contained in:
Timo Westkämper 2015-02-05 23:24:04 +02:00
parent cc27741cf3
commit 82c97d948b
3 changed files with 90 additions and 10 deletions

View File

@ -16,6 +16,7 @@ package com.querydsl.core.group;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
import com.mysema.commons.lang.Pair;
@ -23,7 +24,7 @@ import com.mysema.commons.lang.Pair;
* @param <K>
* @param <V>
*/
class GMap<K, V> extends AbstractGroupExpression<Pair<K, V>, Map<K, V>> {
abstract class GMap<K, V> extends AbstractGroupExpression<Pair<K, V>, Map<K, V>> {
private static final long serialVersionUID = 7106389414200843920L;
@ -31,11 +32,31 @@ class GMap<K, V> extends AbstractGroupExpression<Pair<K, V>, Map<K, V>> {
super(Map.class, qpair);
}
protected abstract Map<K, V> createMap();
public static <T, U> GMap<T, U> createLinked(QPair<T, U> expr) {
return new GMap<T, U>(expr) {
@Override
protected Map<T, U> createMap() {
return new LinkedHashMap<T, U>();
}
};
}
public static <T, U> GMap<T, U> createSorted(QPair<T, U> expr) {
return new GMap<T, U>(expr) {
@Override
protected Map<T, U> createMap() {
return new TreeMap<T, U>();
}
};
}
@Override
public GroupCollector<Pair<K,V>, Map<K, V>> createGroupCollector() {
return new GroupCollector<Pair<K,V>, Map<K, V>>() {
private final Map<K, V> map = new LinkedHashMap<K, V>();
private final Map<K, V> map = createMap();
@Override
public void add(Pair<K,V> pair) {

View File

@ -15,6 +15,7 @@ package com.querydsl.core.group;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;
import com.querydsl.core.types.Expression;
@ -23,19 +24,39 @@ import com.querydsl.core.types.Expression;
*
* @param <T>
*/
class GSet<T> extends AbstractGroupExpression<T, Set<T>> {
abstract class GSet<T> extends AbstractGroupExpression<T, Set<T>> {
private static final long serialVersionUID = -1575808026237160843L;
public static <U> GSet<U> createLinked(Expression<U> expr) {
return new GSet<U>(expr) {
@Override
protected Set<U> createSet() {
return new LinkedHashSet<U>();
}
};
}
public static <U> GSet<U> createSorted(Expression<U> expr) {
return new GSet<U>(expr) {
@Override
protected Set<U> createSet() {
return new TreeSet<U>();
}
};
}
public GSet(Expression<T> expr) {
super(Set.class, expr);
}
protected abstract Set<T> createSet();
@Override
public GroupCollector<T,Set<T>> createGroupCollector() {
return new GroupCollector<T,Set<T>>() {
private final Set<T> set = new LinkedHashSet<T>();
private final Set<T> set = createSet();
@Override
public void add(T o) {

View File

@ -106,21 +106,35 @@ public final class GroupBy {
}
/**
* Create a new aggregating set expression
* Create a new aggregating set expression using a backing LinkedHashMap
*
* @param expression
* @return
*/
public static <E> AbstractGroupExpression<E, Set<E>> set(Expression<E> expression) {
return new GSet<E>(expression);
return GSet.createLinked(expression);
}
public static <E, F> GroupExpression<E, Set<F>> set(GroupExpression<E, F> groupExpression) {
return new MixinGroupExpression<E, F, Set<F>>(groupExpression, new GSet<F>(groupExpression));
return new MixinGroupExpression<E, F, Set<F>>(groupExpression, GSet.createLinked(groupExpression));
}
/**
* Create a new aggregating map expression
* Create a new aggregating set expression using a backing TreeSet
*
* @param expression
* @return
*/
public static <E> AbstractGroupExpression<E, Set<E>> sortedSet(Expression<E> expression) {
return GSet.createSorted(expression);
}
public static <E, F> GroupExpression<E, Set<F>> sortedSet(GroupExpression<E, F> groupExpression) {
return new MixinGroupExpression<E, F, Set<F>>(groupExpression, GSet.createSorted(groupExpression));
}
/**
* Create a new aggregating map expression using a backing LinkedHashMap
*
* @param key
* @param value
@ -128,7 +142,7 @@ public final class GroupBy {
*/
@WithBridgeMethods(value=Expression.class,castRequired=true)
public static <K, V> AbstractGroupExpression<Pair<K, V>,Map<K, V>> map(Expression<K> key, Expression<V> value) {
return new GMap<K, V>(QPair.create(key, value));
return GMap.createLinked(QPair.create(key, value));
}
public static <K, V, T> AbstractGroupExpression<Pair<K, V>, Map<T, V>> map(GroupExpression<K, T> key, Expression<V> value) {
@ -140,7 +154,31 @@ public final class GroupBy {
}
public static <K, V, T, U> AbstractGroupExpression<Pair<K, V>, Map<T, U>> map(GroupExpression<K, T> key, GroupExpression<V, U> value) {
return new GMap.Mixin<K, V, T, U, Map<T, U>>(key, value, new GMap<T, U>(QPair.create(key, value)));
return new GMap.Mixin<K, V, T, U, Map<T, U>>(key, value, GMap.createLinked(QPair.create(key, value)));
}
/**
* Create a new aggregating map expression using a backing TreeMap
*
* @param key
* @param value
* @return
*/
@WithBridgeMethods(value=Expression.class,castRequired=true)
public static <K, V> AbstractGroupExpression<Pair<K, V>,Map<K, V>> sortedMap(Expression<K> key, Expression<V> value) {
return GMap.createSorted(QPair.create(key, value));
}
public static <K, V, T> AbstractGroupExpression<Pair<K, V>, Map<T, V>> sortedMap(GroupExpression<K, T> key, Expression<V> value) {
return sortedMap(key, new GOne<V>(value));
}
public static <K, V, U> AbstractGroupExpression<Pair<K, V>, Map<K, U>> sortedMap(Expression<K> key, GroupExpression<V, U> value) {
return sortedMap(new GOne<K>(key), value);
}
public static <K, V, T, U> AbstractGroupExpression<Pair<K, V>, Map<T, U>> sortedMap(GroupExpression<K, T> key, GroupExpression<V, U> value) {
return new GMap.Mixin<K, V, T, U, Map<T, U>>(key, value, GMap.createSorted(QPair.create(key, value)));
}
private GroupBy() {}