Skip to content

Commit

Permalink
增强单字符串比较问题
Browse files Browse the repository at this point in the history
  • Loading branch information
baoxingjie committed May 14, 2018
1 parent 529581f commit bf2d98b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/ql/util/express/Operator.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ public static int compareData(Object op1,Object op2) throws Exception{
compareResult = ((String)op1).compareTo(op2.toString());
}else if(op2 instanceof String){
compareResult = op1.toString().compareTo((String)op2);
}else if(op1 instanceof Character || op2 instanceof Character){
if(op1 instanceof Character && op2 instanceof Character ){
compareResult = ((Character) op1) .compareTo ((Character) op2);
}else if(op1 instanceof Number){
compareResult = OperatorOfNumber.compareNumber((Number)op1, (int)((Character) op2).charValue());
}else if(op2 instanceof Number){
compareResult = OperatorOfNumber.compareNumber((int)((Character) op1).charValue(),(Number)op2);
}else {
throw new Exception(op1 + "和" + op2 + "不能执行compare 操作");
}
}else if(op1 instanceof Number && op2 instanceof Number){
//数字比较
compareResult = OperatorOfNumber.compareNumber((Number)op1, (Number)op2);
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/com/ql/util/express/bugfix/CompareObjectTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.ql.util.express.bugfix;

import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import com.ql.util.express.IExpressContext;
import org.junit.Test;

public class CompareObjectTest {

@Test
public void test() throws Exception{

System.out.println('a'<98);
ExpressRunner runner = new ExpressRunner();
String[] expList = new String[]{
"'a'<'b'",
"'a'<='b'",
"'a'=='a'",
"test=='a'",
"test<='a'",
"'a'>=test",
};
IExpressContext<String, Object> context = new DefaultContext<String, Object>();
context.put("test",'a'+0);
for(String exp : expList) {
Object result = runner.execute(exp, context, null, true, false);
System.out.println(result);
assert (true == result);
}
}
}

1 comment on commit bf2d98b

@ccqy66
Copy link

@ccqy66 ccqy66 commented on bf2d98b May 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#21 ????

Please sign in to comment.