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 / EqOperator.java @ 44009
History | View | Annotate | Download (1.89 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 | import org.gvsig.expressionevaluator.Function; |
||
6 | import org.gvsig.expressionevaluator.Interpreter; |
||
7 | |||
8 | 43512 | jjdelcerro | public class EqOperator extends AbstractBinaryOperator { |
9 | |||
10 | public EqOperator() {
|
||
11 | 43521 | jjdelcerro | super(Function.GROUP_BOOLEAN, "="); |
12 | 43512 | jjdelcerro | } |
13 | 43521 | jjdelcerro | |
14 | 43512 | jjdelcerro | @Override
|
15 | 44009 | jjdelcerro | public boolean allowConstantFolding() { |
16 | return true; |
||
17 | } |
||
18 | |||
19 | @Override
|
||
20 | 43521 | jjdelcerro | public Object call(Interpreter interpreter, Object op1, Object op2) { |
21 | 43531 | jjdelcerro | int type = this.getType(op1, op2); |
22 | if( (type & TYPE_DOUBLE) == TYPE_DOUBLE ) {
|
||
23 | boolean value = MathUtils.compareTo(
|
||
24 | ((Number) op1).doubleValue(),
|
||
25 | ((Number) op2).doubleValue(),
|
||
26 | interpreter.getAccuracy()==null? MathUtils.EPSILON:interpreter.getAccuracy()
|
||
27 | ) == 0;
|
||
28 | return value;
|
||
29 | 43521 | jjdelcerro | } |
30 | 43531 | jjdelcerro | if( (type & TYPE_FLOAT) == TYPE_FLOAT ) {
|
31 | boolean value = MathUtils.compareTo(
|
||
32 | ((Number) op1).floatValue(),
|
||
33 | ((Number) op2).floatValue(),
|
||
34 | interpreter.getAccuracy()==null? MathUtils.EPSILON:interpreter.getAccuracy()
|
||
35 | ) == 0;
|
||
36 | return value;
|
||
37 | } |
||
38 | if( (type & TYPE_LONG) == TYPE_LONG ) {
|
||
39 | boolean value = ((Number) op1).longValue() == ((Number) op2).longValue(); |
||
40 | return value;
|
||
41 | } |
||
42 | if( (type & TYPE_INT) == TYPE_INT ) {
|
||
43 | boolean value = ((Number) op1).intValue() == ((Number) op2).intValue(); |
||
44 | return value;
|
||
45 | } |
||
46 | 43512 | jjdelcerro | if( op2 instanceof Comparable && op2 instanceof Comparable ) { |
47 | 43521 | jjdelcerro | boolean value = ObjectUtils.compare((Comparable)op1,(Comparable)op2) == 0; |
48 | 43512 | jjdelcerro | return value;
|
49 | } |
||
50 | 43521 | jjdelcerro | throw new IllegalArgumentException("Types not allowed in '" + name() + "' operand."); |
51 | 43512 | jjdelcerro | } |
52 | |||
53 | } |