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 @ 43983
History | View | Annotate | Download (1.86 KB)
1 | 43512 | jjdelcerro | package org.gvsig.expressionevaluator.impl.function.numeric; |
---|---|---|---|
2 | |||
3 | import org.apache.commons.lang3.Range; |
||
4 | 43521 | jjdelcerro | import org.gvsig.expressionevaluator.Interpreter; |
5 | 43512 | jjdelcerro | import org.gvsig.expressionevaluator.spi.AbstractFunction; |
6 | |||
7 | public class AbsFunction extends AbstractFunction { |
||
8 | |||
9 | public AbsFunction() {
|
||
10 | 43983 | jjdelcerro | 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 | ); |
||
23 | 43512 | jjdelcerro | } |
24 | |||
25 | @Override
|
||
26 | 43521 | jjdelcerro | public Object call(Interpreter interpreter, Object[] args) { |
27 | 43512 | jjdelcerro | Object op1 = args[0]; |
28 | |||
29 | if( op1 instanceof Double ) { |
||
30 | double value = Math.abs(((Number) op1).doubleValue()); |
||
31 | return value;
|
||
32 | } |
||
33 | if( op1 instanceof Float ) { |
||
34 | float value = Math.abs(((Number) op1).floatValue()); |
||
35 | return value;
|
||
36 | } |
||
37 | if( op1 instanceof Long ) { |
||
38 | long value = Math.abs(((Number) op1).longValue()); |
||
39 | return value;
|
||
40 | } |
||
41 | if( op1 instanceof Integer ) { |
||
42 | int value = Math.abs(((Number) op1).intValue()); |
||
43 | return value;
|
||
44 | } |
||
45 | throw new IllegalArgumentException("Types not allowed in '"+name()+"' function."); |
||
46 | } |
||
47 | |||
48 | |||
49 | } |