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

History | View | Annotate | Download (3.74 KB)

1 44138 jjdelcerro
package org.gvsig.expressionevaluator.impl.function.programming;
2 43512 jjdelcerro
3
import org.apache.commons.lang3.Range;
4 46001 omartinez
import org.gvsig.expressionevaluator.Code;
5 44138 jjdelcerro
import org.gvsig.expressionevaluator.Codes;
6 46001 omartinez
import org.gvsig.expressionevaluator.Formatter;
7 43939 jjdelcerro
import org.gvsig.expressionevaluator.Function;
8 43521 jjdelcerro
import org.gvsig.expressionevaluator.Interpreter;
9 43512 jjdelcerro
import org.gvsig.expressionevaluator.spi.AbstractFunction;
10 44138 jjdelcerro
import org.gvsig.tools.ToolsLocator;
11
import org.gvsig.tools.dataTypes.DataTypes;
12 44669 jjdelcerro
import org.gvsig.tools.dataTypes.Coercion;
13 43512 jjdelcerro
14 44138 jjdelcerro
public class CaseFunction extends AbstractFunction {
15 43512 jjdelcerro
16 44138 jjdelcerro
    public static final String NAME = "CASE";
17
18
    public CaseFunction() {
19
        super(Function.GROUP_PROGRAMMING,
20
                NAME,
21
                Range.between(2, Integer.MAX_VALUE),
22
                "",
23 44924 jjdelcerro
                "CASE\n  WHEN {{condition}} THEN PASS\n  ELSE PASS\nEND CASE\n",
24 44098 jjdelcerro
                null,
25
                "Object",
26 46001 omartinez
                true
27 43939 jjdelcerro
        );
28 43512 jjdelcerro
    }
29
30
    @Override
31 44738 jjdelcerro
    public boolean isHidden() {
32
      return true;
33
    }
34
35
    @Override
36 43939 jjdelcerro
    public boolean useArgumentsInsteadObjects() {
37
        return true;
38
    }
39
40
    @Override
41 44138 jjdelcerro
    public boolean allowConstantFolding() {
42
        return false;
43
    }
44
45
    @Override
46 43521 jjdelcerro
    public Object call(Interpreter interpreter, Object[] args) throws Exception {
47 43939 jjdelcerro
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
48
    }
49
50
    @Override
51 44138 jjdelcerro
    public Object call(Interpreter interpreter, Codes args) throws Exception {
52
        // CASE(cond1, cond2, cond3,... condN, expr1, expr2, expr3... exprN, exprElse)
53
        //      0      1      2                3      4      5               6
54
        Coercion toBoolean = ToolsLocator.getDataTypesManager().getCoercion(DataTypes.BOOLEAN);
55
        int count;
56
        int firstValue;
57
        int elseValue;
58
59
        if( (args.size() % 2) == 0) {
60
            count = args.size() / 2;
61
            firstValue = count;
62
            elseValue = -1;
63
        } else {
64
            count = (args.size()-1) / 2;
65
            firstValue = count;
66
            elseValue = args.size()-1;
67 43512 jjdelcerro
        }
68 44138 jjdelcerro
        boolean needElse = true;
69
        Object value = null;
70
        for( int n=0; n<count; n++ ) {
71
            Object condition_o = getObject(interpreter, args, n);
72
            Boolean condition = (Boolean) toBoolean.coerce(condition_o);
73 45307 omartinez
            if( condition!=null && condition==true ) {
74 44138 jjdelcerro
                needElse = false;
75
                value = getObject(interpreter, args, n+firstValue);
76 45354 omartinez
                return value;
77 44138 jjdelcerro
            }
78
        }
79
        if( needElse && elseValue>0 ) {
80
            value = getObject(interpreter, args, elseValue);
81
        }
82
        return value;
83 43512 jjdelcerro
    }
84
85 46001 omartinez
    @Override
86
    public String toString(Codes args, Formatter<Code> formatter) {
87
        StringBuilder builder = new StringBuilder();
88
89
        int count;
90
        int firstValue;
91
        int elseValue;
92
93
        if( (args.size() % 2) == 0) {
94
            count = args.size() / 2;
95
            firstValue = count;
96
            elseValue = -1;
97
        } else {
98
            count = (args.size()-1) / 2;
99
            firstValue = count;
100
            elseValue = args.size()-1;
101
        }
102
103
        Object value = null;
104
        builder.append("CASE");
105
        for( int n=0; n<count; n++ ) {
106
            builder.append(" WHEN ");
107
            builder.append(args.get(n).toString(formatter));
108
            builder.append(" THEN ");
109
            builder.append(args.get(n+firstValue).toString(formatter));
110
        }
111
        if( elseValue>0 && args.get(elseValue)!=null) {
112
            builder.append(" ELSE ");
113
            builder.append(args.get(elseValue).toString(formatter));
114
        }
115
        builder.append(" END");
116
        return builder.toString();
117
118
    }
119 43512 jjdelcerro
}