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 / DecrFunction.java @ 44207

History | View | Annotate | Download (2.01 KB)

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

    
3
import org.apache.commons.lang3.Range;
4
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_DECR;
5
import org.gvsig.expressionevaluator.Interpreter;
6
import org.gvsig.expressionevaluator.MutableSymbolTable;
7
import org.gvsig.expressionevaluator.spi.AbstractFunction;
8

    
9
public class DecrFunction extends AbstractFunction {
10

    
11
    public DecrFunction() {
12
        super("Numeric", FUNCTION_DECR,Range.between(1,3),
13
            "Decrease the value of the indicated identifier and return its value.",
14
            FUNCTION_DECR+"('CONTADOR#1')",
15
            new String[]{
16
                "identifier name - the identifier used as counter.",
17
                "first value - Optional. The value with which the identifier must be initialized in its first use. By default zero",
18
                "step - Optional. The value used to decrease the identifier. By default one",
19
            },
20
            "Long",
21
            false
22
        );
23
    }
24

    
25
    @Override
26
    public boolean allowConstantFolding() {
27
        return false;
28
    }
29
    
30
    @Override
31
    public Object call(Interpreter interpreter, Object[] args) {
32
        String identifierName = this.getStr(args, 0);
33
        long firstValue = 0;
34
        long increment = 1; 
35
        long value;
36
        switch(args.length) {
37
            default:
38
            case 1:
39
                break;
40
            case 2:
41
                firstValue = this.getLong(args, 1);
42
                break;
43
            case 3:
44
                firstValue = this.getLong(args, 1);
45
                increment = this.getLong(args, 2);
46
                break;
47
        }
48
        MutableSymbolTable symbolTable = (MutableSymbolTable) interpreter.getSymbolTable();
49
        if( symbolTable.exists(identifierName) ) {
50
            value = ((Number) symbolTable.value(identifierName)).longValue();
51
        } else {
52
            value = firstValue;
53
        }
54
        value = value-increment;
55
        symbolTable.setVar(identifierName, value);
56
        return value;
57
    }
58
    
59

    
60
}