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 / IncrFunction.java @ 44098

History | View | Annotate | Download (1.92 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.MutableSymbolTable;
6
import org.gvsig.expressionevaluator.spi.AbstractFunction;
7

    
8
public class IncrFunction extends AbstractFunction {
9

    
10
    public IncrFunction() {
11
        super("Numeric", "INCR",Range.between(1,3),
12
            "Increase the value of the indicated identifier and return its value.",
13
            "Incr({{identifier_name}})",
14
            new String[]{
15
                "identifier name - the identifier used as counter.",
16
                "first value - Optional. The value with which the identifier must be initialized in its first use. By default zero",
17
                "step - Optional. The value used to increase the identifier. By default one",
18
            },
19
            "Long",
20
            false
21
        );
22
    }
23

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

    
59
}