svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.impl / src / main / java / org / gvsig / expressionevaluator / impl / function / operator / IsOperator.java @ 44207
History | View | Annotate | Download (2.09 KB)
1 | 43512 | jjdelcerro | package org.gvsig.expressionevaluator.impl.function.operator; |
---|---|---|---|
2 | |||
3 | 43521 | jjdelcerro | import org.apache.commons.lang3.ObjectUtils; |
4 | import org.apache.commons.math.util.MathUtils; |
||
5 | 44207 | jjdelcerro | import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_IS; |
6 | 43521 | jjdelcerro | import org.gvsig.expressionevaluator.Function; |
7 | import org.gvsig.expressionevaluator.Interpreter; |
||
8 | |||
9 | 44009 | jjdelcerro | public class IsOperator extends AbstractBinaryOperator { |
10 | 43512 | jjdelcerro | |
11 | 44009 | jjdelcerro | public IsOperator() {
|
12 | 44207 | jjdelcerro | super(Function.GROUP_BOOLEAN, OPERATOR_IS, true); |
13 | 43512 | jjdelcerro | } |
14 | 43521 | jjdelcerro | |
15 | 43512 | jjdelcerro | @Override
|
16 | 44009 | jjdelcerro | public boolean allowConstantFolding() { |
17 | return true; |
||
18 | } |
||
19 | |||
20 | @Override
|
||
21 | 43521 | jjdelcerro | public Object call(Interpreter interpreter, Object op1, Object op2) { |
22 | 44034 | jjdelcerro | |
23 | // FIXME: https://stackoverflow.com/questions/9822154/standard-sql-boolean-operator-is-vs-equals-operator
|
||
24 | |||
25 | 43531 | jjdelcerro | int type = this.getType(op1, op2); |
26 | if( (type & TYPE_DOUBLE) == TYPE_DOUBLE ) {
|
||
27 | boolean value = MathUtils.compareTo(
|
||
28 | ((Number) op1).doubleValue(),
|
||
29 | ((Number) op2).doubleValue(),
|
||
30 | interpreter.getAccuracy()==null? MathUtils.EPSILON:interpreter.getAccuracy()
|
||
31 | ) == 0;
|
||
32 | return value;
|
||
33 | 43521 | jjdelcerro | } |
34 | 43531 | jjdelcerro | if( (type & TYPE_FLOAT) == TYPE_FLOAT ) {
|
35 | boolean value = MathUtils.compareTo(
|
||
36 | ((Number) op1).floatValue(),
|
||
37 | ((Number) op2).floatValue(),
|
||
38 | interpreter.getAccuracy()==null? MathUtils.EPSILON:interpreter.getAccuracy()
|
||
39 | ) == 0;
|
||
40 | return value;
|
||
41 | } |
||
42 | if( (type & TYPE_LONG) == TYPE_LONG ) {
|
||
43 | boolean value = ((Number) op1).longValue() == ((Number) op2).longValue(); |
||
44 | return value;
|
||
45 | } |
||
46 | if( (type & TYPE_INT) == TYPE_INT ) {
|
||
47 | boolean value = ((Number) op1).intValue() == ((Number) op2).intValue(); |
||
48 | return value;
|
||
49 | } |
||
50 | 43512 | jjdelcerro | if( op2 instanceof Comparable && op2 instanceof Comparable ) { |
51 | 43521 | jjdelcerro | boolean value = ObjectUtils.compare((Comparable)op1,(Comparable)op2) == 0; |
52 | 43512 | jjdelcerro | return value;
|
53 | } |
||
54 | 43521 | jjdelcerro | throw new IllegalArgumentException("Types not allowed in '" + name() + "' operand."); |
55 | 43512 | jjdelcerro | } |
56 | |||
57 | } |