mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-19 21:00:53 +08:00
improved Coalesce implementation
This commit is contained in:
parent
13b45cf08f
commit
47d2d981e7
@ -3,10 +3,17 @@ package com.mysema.query.collections;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public final class ColQueryFunctions {
|
||||
|
||||
private ColQueryFunctions(){}
|
||||
|
||||
@Nullable
|
||||
public static <T> T coalesce(T... args){
|
||||
for (T arg : args){
|
||||
if (arg != null){
|
||||
|
||||
@ -21,7 +21,8 @@ import com.mysema.query.types.Visitor;
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public class Coalesce<T> extends ESimple<T>{
|
||||
@SuppressWarnings("unchecked")
|
||||
public class Coalesce<T extends Comparable> extends EComparable<T>{
|
||||
|
||||
private static final long serialVersionUID = 445439522266250417L;
|
||||
|
||||
@ -32,32 +33,47 @@ public class Coalesce<T> extends ESimple<T>{
|
||||
add(exprs);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Coalesce(Expr<T> ...exprs){
|
||||
this((Class<T>)(exprs.length > 0 ? exprs[0].getType() : Object.class), exprs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(Visitor v) {
|
||||
OSimple.create(getType(), Ops.COALESCE, getExpressionList()).accept(v);
|
||||
}
|
||||
|
||||
public Coalesce<T> add(Expr<T>... exprs){
|
||||
for (Expr<T> expr : exprs){
|
||||
this.exprs.add(expr);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public Coalesce<T> add(T constant){
|
||||
this.exprs.add(ExprConst.create(constant));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(Visitor v) {
|
||||
Expr<?> arg = exprs.get(0);
|
||||
for (int i = 1; i < exprs.size(); i++){
|
||||
arg = OSimple.create(List.class, Ops.LIST, arg, exprs.get(i));
|
||||
}
|
||||
OSimple.create(getType(), Ops.COALESCE, arg).accept(v);
|
||||
|
||||
public EDate<T> asDate(){
|
||||
return (EDate<T>) ODate.create(getType(), Ops.COALESCE, getExpressionList());
|
||||
}
|
||||
|
||||
|
||||
public EDateTime<T> asDateTime(){
|
||||
return (EDateTime<T>) ODateTime.create(getType(), Ops.COALESCE, getExpressionList());
|
||||
}
|
||||
|
||||
public ENumber<?> asNumber(){
|
||||
return ONumber.create((Class)getType(), Ops.COALESCE, getExpressionList());
|
||||
}
|
||||
|
||||
public EString asString(){
|
||||
return OString.create(Ops.COALESCE, getExpressionList());
|
||||
}
|
||||
|
||||
public ETime<T> asTime(){
|
||||
return (ETime<T>) OTime.create(getType(), Ops.COALESCE, getExpressionList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o == this){
|
||||
@ -70,4 +86,17 @@ public class Coalesce<T> extends ESimple<T>{
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode(){
|
||||
return exprs.hashCode();
|
||||
}
|
||||
|
||||
private Expr<?> getExpressionList(){
|
||||
Expr<?> arg = exprs.get(0);
|
||||
for (int i = 1; i < exprs.size(); i++){
|
||||
arg = OSimple.create(List.class, Ops.LIST, arg, exprs.get(i));
|
||||
}
|
||||
return arg;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -24,17 +24,17 @@ public class OBoolean extends EBoolean implements Operation<Boolean> {
|
||||
|
||||
private static final long serialVersionUID = 7432281499861357581L;
|
||||
|
||||
public static EBoolean create(Operator<Boolean> op, Expr<?>... args){
|
||||
public static EBoolean create(Operator<? super Boolean> op, Expr<?>... args){
|
||||
return new OBoolean(op, args);
|
||||
}
|
||||
|
||||
private final Operation<Boolean> opMixin;
|
||||
|
||||
OBoolean(Operator<Boolean> op, Expr<?>... args) {
|
||||
OBoolean(Operator<? super Boolean> op, Expr<?>... args) {
|
||||
this(op, Arrays.asList(args));
|
||||
}
|
||||
|
||||
OBoolean(Operator<Boolean> op, List<Expr<?>> args) {
|
||||
OBoolean(Operator<? super Boolean> op, List<Expr<?>> args) {
|
||||
opMixin = new OperationMixin<Boolean>(this, op, args);
|
||||
}
|
||||
|
||||
|
||||
@ -23,17 +23,17 @@ public class OString extends EString implements Operation<String> {
|
||||
|
||||
private static final long serialVersionUID = 6846556373847139549L;
|
||||
|
||||
public static EString create(Operator<String> op, Expr<?>... args){
|
||||
public static EString create(Operator<? super String> op, Expr<?>... args){
|
||||
return new OString(op, args);
|
||||
}
|
||||
|
||||
private final Operation<String> opMixin;
|
||||
|
||||
OString(Operator<String> op, Expr<?>... args) {
|
||||
OString(Operator<? super String> op, Expr<?>... args) {
|
||||
this(op, Arrays.asList(args));
|
||||
}
|
||||
|
||||
OString(Operator<String> op, List<Expr<?>> args) {
|
||||
OString(Operator<? super String> op, List<Expr<?>> args) {
|
||||
this.opMixin = new OperationMixin<String>(this, op, args);
|
||||
}
|
||||
|
||||
|
||||
@ -45,7 +45,6 @@ public final class MathUtils {
|
||||
}else{
|
||||
rv = new BigDecimal(num.toString());
|
||||
}
|
||||
rv = num;
|
||||
}else if (type.equals(BigInteger.class)){
|
||||
if (num instanceof BigInteger){
|
||||
rv = num;
|
||||
|
||||
@ -1,26 +1,39 @@
|
||||
package com.mysema.query.types.expr;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.query.types.path.PString;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class CoalesceTest {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
PString firstname = new PString("firstname");
|
||||
|
||||
PString lastname = new PString("lastname");
|
||||
|
||||
@Test
|
||||
public void withList(){
|
||||
PString firstname = new PString("firstname");
|
||||
PString lastname = new PString("lastname");
|
||||
public void withList(){
|
||||
Coalesce<String> c = new Coalesce<String>(firstname, lastname).add("xxx");
|
||||
assertEquals("coalesce(firstname, lastname, xxx)", c.toString());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
@Test
|
||||
public void withSingleArg(){
|
||||
Coalesce<String> c = new Coalesce<String>().add("xxx");
|
||||
assertEquals("coalesce(xxx)", c.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asComparable(){
|
||||
Coalesce<String> c = new Coalesce<String>(firstname, lastname);
|
||||
c.asc();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asString(){
|
||||
Coalesce<String> c = new Coalesce<String>(firstname, lastname);
|
||||
c.asString().lower();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user