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

History | View | Annotate | Download (2.11 KB)

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

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

    
11
public class IncrFunction extends AbstractFunction {
12

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

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

    
62
}