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 / numeric / AbsFunction.java @ 44009

History | View | Annotate | Download (1.97 KB)

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

    
3
import org.apache.commons.lang3.Range;
4
import org.gvsig.expressionevaluator.Interpreter;
5
import org.gvsig.expressionevaluator.spi.AbstractFunction;
6

    
7
public class AbsFunction extends AbstractFunction {
8

    
9
    public AbsFunction() {
10
        super("Numeric", "ABS",Range.is(1),
11
            "Returns the absolute value of a double value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned. Special cases:\n" +
12
            "- If the argument is positive zero or negative zero, the result is positive zero.\n" +
13
            "- If the argument is infinite, the result is positive infinity.\n" +
14
            "- If the argument is NaN, the result is NaN.\n" +
15
            "In other words, the result is the same as the value of the expression:\n" +
16
            "Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)",
17
            "ABS({{a}})",
18
            new String[]{
19
                "a - the argument whose absolute value is to be determined"
20
            },
21
            "Double",
22
            true
23
        );
24
    }
25

    
26
    @Override
27
    public boolean allowConstantFolding() {
28
        return true;
29
    }
30
    
31
    @Override
32
    public Object call(Interpreter interpreter, Object[] args) {
33
        Object op1 = args[0];
34
        
35
        if( op1 instanceof Double ) {
36
            double value = Math.abs(((Number) op1).doubleValue());
37
            return value;
38
        }
39
        if( op1 instanceof Float ) {
40
            float value = Math.abs(((Number) op1).floatValue());
41
            return value;
42
        }
43
        if( op1 instanceof Long ) {
44
            long value = Math.abs(((Number) op1).longValue());
45
            return value;
46
        }
47
        if( op1 instanceof Integer ) {
48
            int value = Math.abs(((Number) op1).intValue());
49
            return value;
50
        }
51
        throw new IllegalArgumentException("Types not allowed in '"+name()+"' function.");
52
    }
53
    
54

    
55
}