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

History | View | Annotate | Download (1.88 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 Object call(Interpreter interpreter, Object[] args) {
28
        Object op1 = args[0];
29
        
30
        if( op1 instanceof Double ) {
31
            double value = Math.abs(((Number) op1).doubleValue());
32
            return value;
33
        }
34
        if( op1 instanceof Float ) {
35
            float value = Math.abs(((Number) op1).floatValue());
36
            return value;
37
        }
38
        if( op1 instanceof Long ) {
39
            long value = Math.abs(((Number) op1).longValue());
40
            return value;
41
        }
42
        if( op1 instanceof Integer ) {
43
            int value = Math.abs(((Number) op1).intValue());
44
            return value;
45
        }
46
        throw new IllegalArgumentException("Types not allowed in '"+name()+"' function.");
47
    }
48
    
49

    
50
}