Statistics
| Revision:

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 @ 45041

History | View | Annotate | Download (2.08 KB)

1
package org.gvsig.expressionevaluator.impl.function.operator;
2

    
3
import org.apache.commons.math.util.MathUtils;
4
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_EQ;
5
import org.gvsig.expressionevaluator.Function;
6
import org.gvsig.expressionevaluator.Interpreter;
7

    
8
public class EqOperator extends AbstractBinaryOperator {
9

    
10
    public EqOperator() {
11
        super(Function.GROUP_BOOLEAN, OPERATOR_EQ, true);
12
    }
13

    
14
    @Override
15
    public boolean allowConstantFolding() {
16
        return true;
17
    }
18
    
19
    @Override
20
    public Object call(Interpreter interpreter, Object op1, Object op2) {
21
        if( op1==op2 ) {
22
            return true;
23
        }
24
        int type = this.getType(op1, op2);
25
        if( (type & TYPE_NULL) == TYPE_NULL ) {
26
            return op1==op2;
27
        }
28
        if( (type & TYPE_DOUBLE) == TYPE_DOUBLE ) {
29
            double accuracy = MathUtils.EPSILON;
30
            if( interpreter.getAccuracy()!=null ) {
31
              accuracy = interpreter.getAccuracy();
32
            }
33
            boolean value = MathUtils.compareTo(getDouble(op1, 1),getDouble(op2, 2),accuracy) == 0;
34
            return value;
35
        }
36
        if( (type & TYPE_FLOAT) == TYPE_FLOAT ) {
37
            double accuracy = MathUtils.EPSILON;
38
            if( interpreter.getAccuracy()!=null ) {
39
              accuracy = interpreter.getAccuracy();
40
            }
41
            boolean value = MathUtils.compareTo(getFloat(op1, 1),getFloat(op2, 2),accuracy) == 0;
42
            return value;
43
        }
44
        if( (type & TYPE_LONG) == TYPE_LONG ) {
45
            boolean value = Long.compare(getLong(op1, 1),getLong(op2, 2)) == 0;
46
            return value;
47
        }
48
        if( (type & TYPE_INT) == TYPE_INT ) {
49
            boolean value = Integer.compare(getInt(op1, 1),getInt(op2, 2)) == 0;
50
            return value;
51
        }
52
        if( op2 instanceof Comparable && op2 instanceof Comparable ) {
53
            boolean value =   getComparable(op1,1).compareTo(getComparable(op2,2)) == 0;
54
            return value;
55
        }
56
        throw new IllegalArgumentException("Types not allowed in '" + name() + "' operand.");
57
    }
58

    
59
}