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