mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-21 21:14:12 +08:00
#551267 : fixed TINYINT as Byte Awkward to Use
This commit is contained in:
parent
21ec637cda
commit
f756ca2007
@ -159,7 +159,7 @@ public abstract class Expr<D> implements Serializable{
|
||||
* @param right rhs of the comparison
|
||||
* @return
|
||||
*/
|
||||
public final EBoolean notIn(D... right) {
|
||||
public EBoolean notIn(D... right) {
|
||||
if (right.length == 1){
|
||||
return ne(right[0]);
|
||||
}else{
|
||||
|
||||
@ -7,6 +7,8 @@ package com.mysema.query.types.expr;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@ -14,6 +16,7 @@ import com.mysema.query.types.Expr;
|
||||
import com.mysema.query.types.Operator;
|
||||
import com.mysema.query.types.Ops;
|
||||
import com.mysema.query.types.Ops.MathOps;
|
||||
import com.mysema.util.MathUtils;
|
||||
|
||||
/**
|
||||
* ENumber represents a numeric expression
|
||||
@ -197,7 +200,6 @@ public abstract class ENumber<D extends Number & Comparable<?>> extends ECompara
|
||||
return ONumber.create(Double.class, Ops.DIV, this, ENumberConst.create(right));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the double expression of this numeric expression
|
||||
*
|
||||
@ -517,4 +519,22 @@ public abstract class ENumber<D extends Number & Comparable<?>> extends ECompara
|
||||
return sum;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EBoolean in(Number... numbers){
|
||||
return super.in(convert(numbers));
|
||||
}
|
||||
|
||||
@Override
|
||||
public EBoolean notIn(Number... numbers){
|
||||
return super.notIn(convert(numbers));
|
||||
}
|
||||
|
||||
private List<D> convert(Number... numbers){
|
||||
List<D> list = new ArrayList<D>(numbers.length);
|
||||
for (int i = 0; i < numbers.length; i++){
|
||||
list.add(MathUtils.cast(numbers[i], getType()));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
||||
@ -25,9 +25,9 @@ public final class MathUtils {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <D extends Number & Comparable<?>> D cast(BigDecimal num, Class<D> type){
|
||||
public static <D extends Number & Comparable<?>> D cast(Number num, Class<D> type){
|
||||
Number rv;
|
||||
if (type.equals(Double.class)){
|
||||
if (type.equals(Byte.class)){
|
||||
rv = num.byteValue();
|
||||
}else if (type.equals(Double.class)){
|
||||
rv = num.doubleValue();
|
||||
@ -40,9 +40,20 @@ public final class MathUtils {
|
||||
}else if (type.equals(Short.class)){
|
||||
rv = num.shortValue();
|
||||
}else if (type.equals(BigDecimal.class)){
|
||||
if (num instanceof BigDecimal){
|
||||
rv = num;
|
||||
}else{
|
||||
rv = new BigDecimal(num.toString());
|
||||
}
|
||||
rv = num;
|
||||
}else if (type.equals(BigInteger.class)){
|
||||
rv = num.toBigInteger();
|
||||
if (num instanceof BigInteger){
|
||||
rv = num;
|
||||
}else if (num instanceof BigDecimal){
|
||||
rv = ((BigDecimal)num).toBigInteger();
|
||||
}else{
|
||||
rv = new BigInteger(num.toString());
|
||||
}
|
||||
}else{
|
||||
throw new IllegalArgumentException(String.format("Illegal type : %s", type.getSimpleName()));
|
||||
}
|
||||
|
||||
@ -175,6 +175,9 @@ public class Filters {
|
||||
rv.add(expr.loe(knownValue));
|
||||
rv.add(expr.lt(other));
|
||||
rv.add(expr.lt(knownValue));
|
||||
|
||||
rv.add(expr.in(1,2,3));
|
||||
rv.add(expr.in(1l,2l,3l));
|
||||
|
||||
if (expr.getType().equals(Integer.class)){
|
||||
ENumber<Integer> eint = (ENumber)expr;
|
||||
|
||||
@ -0,0 +1,39 @@
|
||||
package com.mysema.query.types.path;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.query.types.Constant;
|
||||
import com.mysema.query.types.Operation;
|
||||
|
||||
public class PNumberTest {
|
||||
|
||||
private PNumber<Byte> bytePath = new PNumber<Byte>(Byte.class, "bytePath");
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void bytePath_in(){
|
||||
Operation<?> operation = (Operation<?>) bytePath.in(1, 2, 3);
|
||||
|
||||
List<Byte> numbers = (List<Byte>) ((Constant)operation.getArg(1)).getConstant();
|
||||
assertEquals(Byte.valueOf((byte)1), numbers.get(0));
|
||||
assertEquals(Byte.valueOf((byte)2), numbers.get(1));
|
||||
assertEquals(Byte.valueOf((byte)3), numbers.get(2));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void bytePath_notIn(){
|
||||
Operation<?> operation = (Operation<?>) bytePath.notIn(1, 2, 3);
|
||||
// unwrap negation
|
||||
operation = (Operation<?>) operation.getArg(0);
|
||||
|
||||
List<Byte> numbers = (List<Byte>) ((Constant)operation.getArg(1)).getConstant();
|
||||
assertEquals(Byte.valueOf((byte)1), numbers.get(0));
|
||||
assertEquals(Byte.valueOf((byte)2), numbers.get(1));
|
||||
assertEquals(Byte.valueOf((byte)3), numbers.get(2));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user