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 / IsOperator.java @ 45213

History | View | Annotate | Download (2.17 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 45041 jjdelcerro
        if( (type & TYPE_NULL) == TYPE_NULL ) {
27
            return op1==op2;
28
        }
29 43531 jjdelcerro
        if( (type & TYPE_DOUBLE) == TYPE_DOUBLE ) {
30
            boolean value = MathUtils.compareTo(
31
                ((Number) op1).doubleValue(),
32
                ((Number) op2).doubleValue(),
33
                interpreter.getAccuracy()==null? MathUtils.EPSILON:interpreter.getAccuracy()
34
            ) == 0;
35
            return value;
36 43521 jjdelcerro
        }
37 43531 jjdelcerro
        if( (type & TYPE_FLOAT) == TYPE_FLOAT ) {
38
            boolean value = MathUtils.compareTo(
39
                ((Number) op1).floatValue(),
40
                ((Number) op2).floatValue(),
41
                interpreter.getAccuracy()==null? MathUtils.EPSILON:interpreter.getAccuracy()
42
            ) == 0;
43
            return value;
44
        }
45
        if( (type & TYPE_LONG) == TYPE_LONG ) {
46
            boolean value = ((Number) op1).longValue() == ((Number) op2).longValue();
47
            return value;
48
        }
49
        if( (type & TYPE_INT) == TYPE_INT ) {
50
            boolean value = ((Number) op1).intValue() == ((Number) op2).intValue();
51
            return value;
52
        }
53 43512 jjdelcerro
        if( op2 instanceof Comparable && op2 instanceof Comparable ) {
54 43521 jjdelcerro
            boolean value = ObjectUtils.compare((Comparable)op1,(Comparable)op2) == 0;
55 43512 jjdelcerro
            return value;
56
        }
57 43521 jjdelcerro
        throw new IllegalArgumentException("Types not allowed in '" + name() + "' operand.");
58 43512 jjdelcerro
    }
59
60
}