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 43512 jjdelcerro
package org.gvsig.expressionevaluator.impl.function.numeric;
2
3
import org.apache.commons.lang3.Range;
4 44206 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionBuilder;
5
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_INCR;
6
import org.gvsig.expressionevaluator.Function;
7 43521 jjdelcerro
import org.gvsig.expressionevaluator.Interpreter;
8 44098 jjdelcerro
import org.gvsig.expressionevaluator.MutableSymbolTable;
9 43512 jjdelcerro
import org.gvsig.expressionevaluator.spi.AbstractFunction;
10
11 44098 jjdelcerro
public class IncrFunction extends AbstractFunction {
12 43512 jjdelcerro
13 44098 jjdelcerro
    public IncrFunction() {
14 44206 jjdelcerro
        super("Numeric", FUNCTION_INCR, Range.between(1,3),
15 44098 jjdelcerro
            "Increase the value of the indicated identifier and return its value.",
16 44206 jjdelcerro
            FUNCTION_INCR+"('CONTADOR#1')",
17 43983 jjdelcerro
            new String[]{
18 44098 jjdelcerro
                "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 43983 jjdelcerro
            },
22 44098 jjdelcerro
            "Long",
23
            false
24 43983 jjdelcerro
        );
25 43512 jjdelcerro
    }
26
27
    @Override
28 44009 jjdelcerro
    public boolean allowConstantFolding() {
29 44207 jjdelcerro
        return false;
30 44009 jjdelcerro
    }
31
32
    @Override
33 43521 jjdelcerro
    public Object call(Interpreter interpreter, Object[] args) {
34 44098 jjdelcerro
        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 43512 jjdelcerro
        }
50 44098 jjdelcerro
        MutableSymbolTable symbolTable = (MutableSymbolTable) interpreter.getSymbolTable();
51
        if( symbolTable.exists(identifierName) ) {
52
            value = ((Number) symbolTable.value(identifierName)).longValue();
53
        } else {
54
            value = firstValue;
55 43512 jjdelcerro
        }
56 44098 jjdelcerro
        value = value+increment;
57
        symbolTable.setVar(identifierName, value);
58
        return value;
59 43512 jjdelcerro
    }
60
61
62
}