mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-30 21:08:30 +08:00
Merge NumberExpression.cast() and MathUtils.cast()
This commit is contained in:
parent
4c644cef38
commit
c07a87a120
@ -13,8 +13,6 @@
|
||||
*/
|
||||
package com.mysema.query.types.expr;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ -148,30 +146,8 @@ public abstract class NumberExpression<T extends Number & Comparable<?>> extends
|
||||
return castToNum(Byte.class);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private T cast(Number number) {
|
||||
Class<T> type = (Class<T>) getType();
|
||||
if (type.isAssignableFrom(number.getClass())) {
|
||||
return (T) number;
|
||||
} else if (Byte.class.equals(type)) {
|
||||
return (T) Byte.valueOf(number.byteValue());
|
||||
} else if (Double.class.equals(type)) {
|
||||
return (T) Double.valueOf(number.doubleValue());
|
||||
} else if (Float.class.equals(type)) {
|
||||
return (T) Float.valueOf(number.floatValue());
|
||||
} else if (Integer.class.equals(type)) {
|
||||
return (T) Integer.valueOf(number.intValue());
|
||||
} else if (Long.class.equals(type)) {
|
||||
return (T) Long.valueOf(number.longValue());
|
||||
} else if (Short.class.equals(type)) {
|
||||
return (T) Short.valueOf(number.shortValue());
|
||||
} else if (BigInteger.class.equals(type)) {
|
||||
return (T) new BigInteger(String.valueOf(number.longValue()));
|
||||
} else if (BigDecimal.class.equals(type)) {
|
||||
return (T) new BigDecimal(number.toString());
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unsupported target type : " + type.getName());
|
||||
}
|
||||
return MathUtils.cast(number, getType());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -63,7 +63,7 @@ public final class MathUtils {
|
||||
rv = type.cast(new BigInteger(num.toString()));
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException(String.format("Illegal type : %s", type.getSimpleName()));
|
||||
throw new IllegalArgumentException(String.format("Unsupported target type : %s", type.getSimpleName()));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user