-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(express): 支持一元正负号运算符 #355
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.ql.util.express.instruction.op; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
|
||
import com.ql.util.express.Operator; | ||
|
||
public class OperatorUnaryMinus extends Operator { | ||
public OperatorUnaryMinus(String name) { | ||
this.name = name; | ||
} | ||
|
||
public OperatorUnaryMinus(String aliasName, String name, String errorInfo) { | ||
this.name = name; | ||
this.aliasName = aliasName; | ||
this.errorInfo = errorInfo; | ||
} | ||
|
||
@Override | ||
public Object executeInner(Object[] list) throws Exception { | ||
if (list[0] instanceof Number) { | ||
return negate((Number) list[0]); | ||
} | ||
throw new Exception("Cannot apply unary minus to non-number type"); | ||
} | ||
|
||
public static Number negate(Number number) { | ||
if (number == null) { | ||
return null; | ||
} | ||
|
||
if (number instanceof Integer) { | ||
return -number.intValue(); | ||
} else if (number instanceof Long) { | ||
return -number.longValue(); | ||
} else if (number instanceof Float) { | ||
return -number.floatValue(); | ||
} else if (number instanceof Double) { | ||
return -number.doubleValue(); | ||
} else if (number instanceof BigInteger) { | ||
return ((BigInteger) number).negate(); | ||
} else if (number instanceof BigDecimal) { | ||
return ((BigDecimal) number).negate(); | ||
} else if (number instanceof Short) { | ||
return (short) -number.shortValue(); | ||
} else if (number instanceof Byte) { | ||
return (byte) -number.byteValue(); | ||
} else { | ||
// 对于其他类型的 Number | ||
return -number.doubleValue(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.ql.util.express.instruction.op; | ||
|
||
import com.ql.util.express.Operator; | ||
|
||
public class OperatorUnaryPlus extends Operator { | ||
public OperatorUnaryPlus(String name) { | ||
this.name = name; | ||
} | ||
|
||
public OperatorUnaryPlus(String aliasName, String name, String errorInfo) { | ||
this.name = name; | ||
this.aliasName = aliasName; | ||
this.errorInfo = errorInfo; | ||
} | ||
|
||
@Override | ||
public Object executeInner(Object[] list) throws Exception { | ||
if (list[0] instanceof Number) { | ||
return list[0]; | ||
} | ||
throw new Exception("Cannot apply unary plus to non-number type"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 抛 QLException |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,7 +60,7 @@ public class KeyWordDefine4Java { | |
+ "*$RIGHT_BRACKET~))", | ||
"[]:TYPE=EXPRESS,DEFINE=[~$EXPRESS*$]~#[]", | ||
|
||
"OP_LEVEL1:TYPE=OPERATOR,DEFINE=~|!", | ||
"OP_LEVEL1:TYPE=OPERATOR,DEFINE=~|!|-|+", | ||
"OP_LEVEL2:TYPE=OPERATOR,DEFINE=++|--", | ||
"OP_LEVEL3:TYPE=OPERATOR,DEFINE=&|MAYBE|XOR|<<|>>", | ||
"OP_LEVEL4:TYPE=OPERATOR,DEFINE=*|/|mod|%", | ||
|
@@ -97,7 +97,7 @@ public class KeyWordDefine4Java { | |
"ARRAY_CALL_POST:TYPE=EXPRESS,DEFINE=(METHOD_CALL|FIELD_CALL)$([->ARRAY_CALL^$EXPRESS$]~)^*", | ||
|
||
"CAST_CALL:TYPE=EXPRESS,DEFINE=(LEFT_BRACKET~$CONST_CLASS$RIGHT_BRACKET~#cast)^*$((LAMBDA#LAMBDA)|ARRAY_CALL)", | ||
"EXPRESS_OP_L1:TYPE=EXPRESS,DEFINE=OP_LEVEL1^*$CAST_CALL", | ||
"EXPRESS_OP_L1:TYPE=EXPRESS,DEFINE=(OP_LEVEL1^$CAST_CALL)^*", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里为什么要从左边改成右边?如果使用原来的就能能通过大多数测试(和 |
||
"EXPRESS_OP_L2:TYPE=EXPRESS,DEFINE=EXPRESS_OP_L1$OP_LEVEL2^*", | ||
"EXPRESS_OP_L3:TYPE=EXPRESS,DEFINE=EXPRESS_OP_L2$(OP_LEVEL3^$EXPRESS_OP_L2)^*", | ||
"EXPRESS_OP_L4:TYPE=EXPRESS,DEFINE=EXPRESS_OP_L3$(OP_LEVEL4^$EXPRESS_OP_L3)^*", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.ql.util.express.issue; | ||
|
||
import java.math.BigDecimal; | ||
|
||
import com.ql.util.express.DefaultContext; | ||
import com.ql.util.express.ExpressRunner; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* @author zgxkbtl | ||
*/ | ||
public class Issue345NegativeNumberTest { | ||
@Test | ||
public void test() throws Exception { | ||
ExpressRunner runner = new ExpressRunner(false, true); | ||
DefaultContext<String, Object> context = new DefaultContext<>(); | ||
BigDecimal value = new BigDecimal("12.22"); | ||
context.put("b", value); | ||
String express = "-b"; | ||
Object result = runner.execute(express, context, null, true, true); | ||
//Assert.assertTrue((Boolean)result); | ||
Assert.assertEquals(new BigDecimal("-12.22"), result); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 单元测试建议将所有的数字类型都进行一遍测试 |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
注释代码删除