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 / programming / CaseFunction.java @ 44669

History | View | Annotate | Download (2.43 KB)

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

    
3
import org.apache.commons.lang3.Range;
4
import org.gvsig.expressionevaluator.Codes;
5
import org.gvsig.expressionevaluator.Function;
6
import org.gvsig.expressionevaluator.Interpreter;
7
import org.gvsig.expressionevaluator.spi.AbstractFunction;
8
import org.gvsig.tools.ToolsLocator;
9
import org.gvsig.tools.dataTypes.DataTypes;
10
import org.gvsig.tools.dataTypes.Coercion;
11

    
12
public class CaseFunction extends AbstractFunction {
13

    
14
    public static final String NAME = "CASE";
15

    
16
    public CaseFunction() {
17
        super(Function.GROUP_PROGRAMMING, 
18
                NAME, 
19
                Range.between(2, Integer.MAX_VALUE),
20
                "",
21
                null,
22
                null,
23
                "Object",
24
                false
25
        );
26
    }
27

    
28
    @Override
29
    public boolean useArgumentsInsteadObjects() {
30
        return true;
31
    }
32

    
33
    @Override
34
    public boolean allowConstantFolding() {
35
        return false;
36
    }
37
    
38
    @Override
39
    public Object call(Interpreter interpreter, Object[] args) throws Exception {
40
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
41
    }
42
    
43
    @Override
44
    public Object call(Interpreter interpreter, Codes args) throws Exception {
45
        // CASE(cond1, cond2, cond3,... condN, expr1, expr2, expr3... exprN, exprElse)
46
        //      0      1      2                3      4      5               6
47
        Coercion toBoolean = ToolsLocator.getDataTypesManager().getCoercion(DataTypes.BOOLEAN);
48
        int count;
49
        int firstValue;
50
        int elseValue;
51
        
52
        if( (args.size() % 2) == 0) {
53
            count = args.size() / 2;
54
            firstValue = count;
55
            elseValue = -1;
56
        } else {
57
            count = (args.size()-1) / 2;
58
            firstValue = count;
59
            elseValue = args.size()-1;
60
        }
61
        boolean needElse = true;
62
        Object value = null;
63
        for( int n=0; n<count; n++ ) {
64
            Object condition_o = getObject(interpreter, args, n);
65
            Boolean condition = (Boolean) toBoolean.coerce(condition_o);
66
            if( condition ) {
67
                needElse = false;
68
                value = getObject(interpreter, args, n+firstValue);
69
            }
70
        }
71
        if( needElse && elseValue>0 ) {
72
            value = getObject(interpreter, args, elseValue);        
73
        }
74
        return value;
75
    }
76
    
77
}