|
|
@@ -27,7 +27,7 @@ import com.jfinal.template.stat.Scope;
|
|
|
/**
|
|
|
* Compare
|
|
|
*
|
|
|
- * 1:支持 byte short int long float double BigDecimal 的 == != > >= < <= 操作
|
|
|
+ * 1:支持 byte short int long float double BigInteger BigDecimal 的 == != > >= < <= 操作
|
|
|
* 2:== != 作用于 string,调用其 equals 方法进行比较
|
|
|
* 3:> >= < <= 可以比较实现了 Comparable 接口的对象
|
|
|
*
|
|
|
@@ -57,7 +57,7 @@ public class Compare extends Expr {
|
|
|
case EQUAL:
|
|
|
return equal(leftValue, rightValue);
|
|
|
case NOTEQUAL:
|
|
|
- return ! equal(leftValue, rightValue);
|
|
|
+ return equal(leftValue, rightValue) ? Boolean.FALSE : Boolean.TRUE;
|
|
|
case GT:
|
|
|
return gt(leftValue, rightValue);
|
|
|
case GE:
|
|
|
@@ -86,7 +86,7 @@ public class Compare extends Expr {
|
|
|
if (leftValue instanceof Number && rightValue instanceof Number) {
|
|
|
Number l = (Number)leftValue;
|
|
|
Number r = (Number)rightValue;
|
|
|
- int maxType = getMaxType(l, r);
|
|
|
+ int maxType = Arith.getMaxType(l, r);
|
|
|
switch (maxType) {
|
|
|
case Arith.INT:
|
|
|
return l.intValue() == r.intValue();
|
|
|
@@ -101,11 +101,15 @@ public class Compare extends Expr {
|
|
|
// return Double.doubleToLongBits(l.doubleValue()) == Double.doubleToLongBits(r.doubleValue());
|
|
|
return l.doubleValue() == r.doubleValue();
|
|
|
case Arith.BIGDECIMAL:
|
|
|
- case Arith.BIGINTEGER:
|
|
|
BigDecimal[] bd = Arith.toBigDecimals(l, r);
|
|
|
return (bd[0]).compareTo(bd[1]) == 0;
|
|
|
+ case Arith.BIGINTEGER:
|
|
|
+ BigInteger[] bi = Arith.toBigIntegers(l, r);
|
|
|
+ return (bi[0]).compareTo(bi[1]) == 0;
|
|
|
+ case Arith.UNKNOWN:
|
|
|
+ throw Arith.unsupportedTypeException(l, r, location);
|
|
|
}
|
|
|
- throw new TemplateException("Equal comparison support types of int long float double and BigDeciaml", location);
|
|
|
+ throw new TemplateException("Equal comparison support types of int long float double BigInteger and BigDeciaml", location);
|
|
|
}
|
|
|
|
|
|
return Boolean.FALSE;
|
|
|
@@ -116,7 +120,7 @@ public class Compare extends Expr {
|
|
|
if (leftValue instanceof Number && rightValue instanceof Number) {
|
|
|
Number l = (Number)leftValue;
|
|
|
Number r = (Number)rightValue;
|
|
|
- int maxType = getMaxType(l, r);
|
|
|
+ int maxType = Arith.getMaxType(l, r);
|
|
|
switch (maxType) {
|
|
|
case Arith.INT:
|
|
|
return l.intValue() > r.intValue();
|
|
|
@@ -129,9 +133,13 @@ public class Compare extends Expr {
|
|
|
// return Double.doubleToLongBits(l.doubleValue()) > Double.doubleToLongBits(r.doubleValue());
|
|
|
return l.doubleValue() > r.doubleValue();
|
|
|
case Arith.BIGDECIMAL:
|
|
|
- case Arith.BIGINTEGER:
|
|
|
BigDecimal[] bd = Arith.toBigDecimals(l, r);
|
|
|
return (bd[0]).compareTo(bd[1]) > 0;
|
|
|
+ case Arith.BIGINTEGER:
|
|
|
+ BigInteger[] bi = Arith.toBigIntegers(l, r);
|
|
|
+ return (bi[0]).compareTo(bi[1]) > 0;
|
|
|
+ case Arith.UNKNOWN:
|
|
|
+ throw Arith.unsupportedTypeException(l, r, location);
|
|
|
}
|
|
|
throw new TemplateException("Unsupported operation: " + l.getClass().getSimpleName() + " \">\" " + r.getClass().getSimpleName(), location);
|
|
|
}
|
|
|
@@ -150,7 +158,7 @@ public class Compare extends Expr {
|
|
|
if (leftValue instanceof Number && rightValue instanceof Number) {
|
|
|
Number l = (Number)leftValue;
|
|
|
Number r = (Number)rightValue;
|
|
|
- int maxType = getMaxType(l, r);
|
|
|
+ int maxType = Arith.getMaxType(l, r);
|
|
|
switch (maxType) {
|
|
|
case Arith.INT:
|
|
|
return l.intValue() >= r.intValue();
|
|
|
@@ -163,9 +171,13 @@ public class Compare extends Expr {
|
|
|
// return Double.doubleToLongBits(l.doubleValue()) >= Double.doubleToLongBits(r.doubleValue());
|
|
|
return l.doubleValue() >= r.doubleValue();
|
|
|
case Arith.BIGDECIMAL:
|
|
|
- case Arith.BIGINTEGER:
|
|
|
BigDecimal[] bd = Arith.toBigDecimals(l, r);
|
|
|
return (bd[0]).compareTo(bd[1]) >= 0;
|
|
|
+ case Arith.BIGINTEGER:
|
|
|
+ BigInteger[] bi = Arith.toBigIntegers(l, r);
|
|
|
+ return (bi[0]).compareTo(bi[1]) >= 0;
|
|
|
+ case Arith.UNKNOWN:
|
|
|
+ throw Arith.unsupportedTypeException(l, r, location);
|
|
|
}
|
|
|
throw new TemplateException("Unsupported operation: " + l.getClass().getSimpleName() + " \">=\" " + r.getClass().getSimpleName(), location);
|
|
|
}
|
|
|
@@ -184,7 +196,7 @@ public class Compare extends Expr {
|
|
|
if (leftValue instanceof Number && rightValue instanceof Number) {
|
|
|
Number l = (Number)leftValue;
|
|
|
Number r = (Number)rightValue;
|
|
|
- int maxType = getMaxType(l, r);
|
|
|
+ int maxType = Arith.getMaxType(l, r);
|
|
|
switch (maxType) {
|
|
|
case Arith.INT:
|
|
|
return l.intValue() < r.intValue();
|
|
|
@@ -197,9 +209,13 @@ public class Compare extends Expr {
|
|
|
// return Double.doubleToLongBits(l.doubleValue()) < Double.doubleToLongBits(r.doubleValue());
|
|
|
return l.doubleValue() < r.doubleValue();
|
|
|
case Arith.BIGDECIMAL:
|
|
|
- case Arith.BIGINTEGER:
|
|
|
BigDecimal[] bd = Arith.toBigDecimals(l, r);
|
|
|
return (bd[0]).compareTo(bd[1]) < 0;
|
|
|
+ case Arith.BIGINTEGER:
|
|
|
+ BigInteger[] bi = Arith.toBigIntegers(l, r);
|
|
|
+ return (bi[0]).compareTo(bi[1]) < 0;
|
|
|
+ case Arith.UNKNOWN:
|
|
|
+ throw Arith.unsupportedTypeException(l, r, location);
|
|
|
}
|
|
|
throw new TemplateException("Unsupported operation: " + l.getClass().getSimpleName() + " \"<\" " + r.getClass().getSimpleName(), location);
|
|
|
}
|
|
|
@@ -218,7 +234,7 @@ public class Compare extends Expr {
|
|
|
if (leftValue instanceof Number && rightValue instanceof Number) {
|
|
|
Number l = (Number)leftValue;
|
|
|
Number r = (Number)rightValue;
|
|
|
- int maxType = getMaxType(l, r);
|
|
|
+ int maxType = Arith.getMaxType(l, r);
|
|
|
switch (maxType) {
|
|
|
case Arith.INT:
|
|
|
return l.intValue() <= r.intValue();
|
|
|
@@ -231,9 +247,13 @@ public class Compare extends Expr {
|
|
|
// return Double.doubleToLongBits(l.doubleValue()) <= Double.doubleToLongBits(r.doubleValue());
|
|
|
return l.doubleValue() <= r.doubleValue();
|
|
|
case Arith.BIGDECIMAL:
|
|
|
- case Arith.BIGINTEGER:
|
|
|
BigDecimal[] bd = Arith.toBigDecimals(l, r);
|
|
|
return (bd[0]).compareTo(bd[1]) <= 0;
|
|
|
+ case Arith.BIGINTEGER:
|
|
|
+ BigInteger[] bi = Arith.toBigIntegers(l, r);
|
|
|
+ return (bi[0]).compareTo(bi[1]) <= 0;
|
|
|
+ case Arith.UNKNOWN:
|
|
|
+ throw Arith.unsupportedTypeException(l, r, location);
|
|
|
}
|
|
|
throw new TemplateException("Unsupported operation: " + l.getClass().getSimpleName() + " \"<=\" " + r.getClass().getSimpleName(), location);
|
|
|
}
|
|
|
@@ -247,35 +267,6 @@ public class Compare extends Expr {
|
|
|
return checkComparisonValue(leftValue, rightValue);
|
|
|
}
|
|
|
|
|
|
- private int getMaxType(Number obj1, Number obj2) {
|
|
|
- int t1 = getType(obj1);
|
|
|
- if (t1 == Arith.BIGDECIMAL) {
|
|
|
- return Arith.BIGDECIMAL;
|
|
|
- }
|
|
|
- int t2 = getType(obj2);
|
|
|
- return t1 > t2 ? t1 : t2;
|
|
|
- }
|
|
|
-
|
|
|
- private int getType(Number obj) {
|
|
|
- if (obj instanceof Integer) {
|
|
|
- return Arith.INT;
|
|
|
- } else if (obj instanceof Long) {
|
|
|
- return Arith.LONG;
|
|
|
- } else if (obj instanceof Float) {
|
|
|
- return Arith.FLOAT;
|
|
|
- } else if (obj instanceof Double) {
|
|
|
- return Arith.DOUBLE;
|
|
|
- } else if (obj instanceof BigDecimal) {
|
|
|
- return Arith.BIGDECIMAL;
|
|
|
- } else if (obj instanceof Short || obj instanceof Byte) {
|
|
|
- return Arith.INT; // short byte 用 int 支持,java 表达式亦如此
|
|
|
- } else if (obj instanceof BigInteger) {
|
|
|
- return Arith.BIGINTEGER; // 新增 BigInteger 支持
|
|
|
- }
|
|
|
-
|
|
|
- throw new TemplateException("Unsupported data type: " + obj.getClass().getName(), location);
|
|
|
- }
|
|
|
-
|
|
|
private Boolean checkComparisonValue(Object leftValue, Object rightValue) {
|
|
|
if (leftValue == null) {
|
|
|
throw new TemplateException("The operation target on the left side of \"" + op.value() + "\" can not be null", location);
|