#314 move duplicate removal to MappingProjection, since QTuple is used internally in nearly all query implementations

This commit is contained in:
Timo Westkämper 2012-12-14 10:42:08 +02:00
parent 735b9cc79d
commit ca4f0ca55d
5 changed files with 59 additions and 25 deletions

View File

@ -14,7 +14,9 @@
package com.mysema.query.types;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;
@ -393,6 +395,38 @@ public final class ExpressionUtils {
return PredicateOperation.create(Ops.OR, left, right);
}
/**
* @param args
* @return
*/
public static List<Expression<?>> distinctList(Expression<?>... args) {
final ImmutableList.Builder<Expression<?>> builder = ImmutableList.builder();
final Set<Expression<?>> set = new HashSet<Expression<?>>(args.length);
for (Expression<?> arg : args) {
if (set.add(arg)) {
builder.add(arg);
}
}
return builder.build();
}
/**
* @param args
* @return
*/
public static List<Expression<?>> distinctList(Expression<?>[]... args) {
final ImmutableList.Builder<Expression<?>> builder = ImmutableList.builder();
final Set<Expression<?>> set = new HashSet<Expression<?>>();
for (Expression<?>[] arr : args) {
for (Expression<?> arg : arr) {
if (set.add(arg)) {
builder.add(arg);
}
}
}
return builder.build();
}
private ExpressionUtils(){}
}

View File

@ -50,7 +50,7 @@ public abstract class MappingProjection<T> extends ExpressionBase<T> implements
@SuppressWarnings("unchecked")
public MappingProjection(Class<? super T> type, Expression<?>... args) {
super((Class)type);
qTuple = new QTuple(args);
qTuple = new QTuple(ExpressionUtils.distinctList(args));
}
/**
@ -62,7 +62,7 @@ public abstract class MappingProjection<T> extends ExpressionBase<T> implements
@SuppressWarnings("unchecked")
public MappingProjection(Class<? super T> type, Expression<?>[]... args) {
super((Class)type);
qTuple = new QTuple(args);
qTuple = new QTuple(ExpressionUtils.distinctList(args));
}
public T newInstance(Object... values) {

View File

@ -36,8 +36,6 @@ import com.mysema.query.Tuple;
* }
* </pre>
*
* Duplicate expressions are removed in the constructor.
*
* @author tiwe
*
*/
@ -104,14 +102,7 @@ public class QTuple extends ExpressionBase<Tuple> implements FactoryExpression<T
*/
public QTuple(Expression<?>... args) {
super(Tuple.class);
Set<Expression<?>> set = new HashSet<Expression<?>>(args.length);
ImmutableList.Builder<Expression<?>> builder = ImmutableList.builder();
for (Expression<?> arg : args) {
if (set.add(arg)) {
builder.add(arg);
}
}
this.args = builder.build();
this.args = ImmutableList.copyOf(args);
}
/**
@ -119,16 +110,9 @@ public class QTuple extends ExpressionBase<Tuple> implements FactoryExpression<T
*
* @param args
*/
public QTuple(List<? extends Expression<?>> args) {
public QTuple(List<Expression<?>> args) {
super(Tuple.class);
ImmutableList.Builder<Expression<?>> builder = ImmutableList.builder();
for (int i = 0; i < args.size(); i++) {
Expression<?> arg = args.get(i);
if (args.indexOf(arg) == i) {
builder.add(arg);
}
}
this.args = builder.build();
this.args = args;
}
/**
@ -138,11 +122,11 @@ public class QTuple extends ExpressionBase<Tuple> implements FactoryExpression<T
*/
public QTuple(Expression<?>[]... args) {
super(Tuple.class);
Set<Expression<?>> argsSet = new LinkedHashSet<Expression<?>>();
ImmutableList.Builder<Expression<?>> builder = ImmutableList.builder();
for (Expression<?>[] exprs: args){
argsSet.addAll(Arrays.asList(exprs));
builder.addAll(Arrays.asList(exprs));
}
this.args = ImmutableList.copyOf(argsSet);
this.args = builder.build();
}
@Override

View File

@ -54,4 +54,17 @@ public class MappingProjectionTest {
assertEquals("1", mapping.newInstance("1"));
}
@Test
public void Distinct_Expressions() {
MappingProjection<Pair<String,String>> mapping = new MappingProjection<Pair<String,String>>(Pair.class, str1, str1) {
@Override
protected Pair<String, String> map(Tuple row) {
return Pair.of(row.get(str1), row.get(str1));
}
};
assertEquals(1, mapping.getArgs().size());
assertEquals(Pair.of("1", "1"), mapping.newInstance("1"));
}
}

View File

@ -17,6 +17,7 @@ import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import org.junit.Ignore;
import org.junit.Test;
import com.mysema.query.types.path.StringPath;
@ -82,6 +83,7 @@ public class QTupleTest {
}
@Test
@Ignore
public void Duplicates() {
QTuple expr = new QTuple(str1, str1);
assertEquals(1, expr.getArgs().size());
@ -89,8 +91,9 @@ public class QTupleTest {
}
@Test
@Ignore
public void Duplicates2() {
QTuple expr = new QTuple(Arrays.asList(str1, str1));
QTuple expr = new QTuple(Arrays.<Expression<?>>asList(str1, str1));
assertEquals(1, expr.getArgs().size());
assertEquals(str1, expr.getArgs().get(0));
}