mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-24 21:07:26 +08:00
Add null skipping
This commit is contained in:
parent
68e5c81bed
commit
aa7f33b72a
@ -0,0 +1,65 @@
|
||||
package com.mysema.query.types;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Common superclass for FactoryExpression implementations
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public abstract class FactoryExpressionBase<T> extends ExpressionBase<T> implements FactoryExpression<T> {
|
||||
|
||||
private static class FactoryExpressionWrapper<T> extends ExpressionBase<T> implements FactoryExpression<T> {
|
||||
private final FactoryExpression<T> expr;
|
||||
|
||||
public FactoryExpressionWrapper(FactoryExpression<T> expr) {
|
||||
super(expr.getType());
|
||||
this.expr = expr;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Expression<?>> getArgs() {
|
||||
return expr.getArgs();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public T newInstance(Object... args) {
|
||||
if (args != null) {
|
||||
for (Object arg : args) {
|
||||
if (arg != null) {
|
||||
return expr.newInstance(args);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public <R, C> R accept(Visitor<R, C> v, @Nullable C context) {
|
||||
return expr.accept(v, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return expr.equals(o);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public FactoryExpressionBase(Class<? extends T> type) {
|
||||
super(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a wrapper expression which returns null if all arguments to newInstance are null
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public FactoryExpression<T> skipNulls() {
|
||||
return new FactoryExpressionWrapper<T>(this);
|
||||
}
|
||||
|
||||
}
|
||||
@ -46,7 +46,7 @@ import com.google.common.primitives.Primitives;
|
||||
*
|
||||
* @param <T> bean type
|
||||
*/
|
||||
public class QBean<T> extends ExpressionBase<T> implements FactoryExpression<T> {
|
||||
public class QBean<T> extends FactoryExpressionBase<T> implements FactoryExpression<T> {
|
||||
|
||||
private static final long serialVersionUID = -8210214512730989778L;
|
||||
|
||||
|
||||
@ -13,12 +13,11 @@
|
||||
*/
|
||||
package com.mysema.query.types;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
/**
|
||||
@ -27,7 +26,7 @@ import com.google.common.collect.ImmutableList;
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public class QList extends ExpressionBase<List<?>> implements FactoryExpression<List<?>> {
|
||||
public class QList extends FactoryExpressionBase<List<?>> implements FactoryExpression<List<?>> {
|
||||
|
||||
private static final long serialVersionUID = -7545994090073480810L;
|
||||
|
||||
|
||||
@ -37,7 +37,7 @@ import com.google.common.collect.Maps;
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public class QMap extends ExpressionBase<Map<Expression<?>,?>> implements FactoryExpression<Map<Expression<?>,?>>{
|
||||
public class QMap extends FactoryExpressionBase<Map<Expression<?>,?>> implements FactoryExpression<Map<Expression<?>,?>>{
|
||||
|
||||
private static final long serialVersionUID = -7545994090073480810L;
|
||||
|
||||
|
||||
@ -13,13 +13,12 @@
|
||||
*/
|
||||
package com.mysema.query.types;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
import java.io.Serializable;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Maps;
|
||||
@ -53,7 +52,7 @@ import com.mysema.query.Tuple;
|
||||
*
|
||||
*/
|
||||
@Immutable
|
||||
public class QTuple extends ExpressionBase<Tuple> implements FactoryExpression<Tuple> {
|
||||
public class QTuple extends FactoryExpressionBase<Tuple> implements FactoryExpression<Tuple> {
|
||||
|
||||
private static ImmutableMap<Expression<?>, Integer> createBindings(List<Expression<?>> exprs) {
|
||||
Map<Expression<?>, Integer> map = Maps.newHashMap();
|
||||
|
||||
@ -13,16 +13,16 @@
|
||||
*/
|
||||
package com.mysema.query.types;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.mysema.query.Tuple;
|
||||
import com.mysema.query.types.path.StringPath;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
public class QTupleTest {
|
||||
|
||||
@ -108,4 +108,10 @@ public class QTupleTest {
|
||||
assertEquals(1, expr.getArgs().size());
|
||||
assertEquals(str1, expr.getArgs().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void NewInstance() {
|
||||
assertNotNull(new QTuple(str1, str1).newInstance(null, null));
|
||||
assertNull(new QTuple(str1, str1).skipNulls().newInstance(null, null));
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user