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 @ 44592
History | View | Annotate | Download (2.45 KB)
1 | 44138 | jjdelcerro | package org.gvsig.expressionevaluator.impl.function.programming; |
---|---|---|---|
2 | 43512 | jjdelcerro | |
3 | import org.apache.commons.lang3.Range; |
||
4 | 44138 | jjdelcerro | import org.gvsig.expressionevaluator.Codes; |
5 | 43939 | jjdelcerro | import org.gvsig.expressionevaluator.Function; |
6 | 43521 | jjdelcerro | import org.gvsig.expressionevaluator.Interpreter; |
7 | 43512 | jjdelcerro | import org.gvsig.expressionevaluator.spi.AbstractFunction; |
8 | 44138 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
9 | import org.gvsig.tools.dataTypes.DataTypes; |
||
10 | import org.gvsig.tools.dataTypes.DataTypesManager.Coercion; |
||
11 | 43512 | jjdelcerro | |
12 | 44138 | jjdelcerro | public class CaseFunction extends AbstractFunction { |
13 | 43512 | jjdelcerro | |
14 | 44138 | jjdelcerro | 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 | 44098 | jjdelcerro | null,
|
22 | 44138 | jjdelcerro | null,
|
23 | 44098 | jjdelcerro | "Object",
|
24 | false
|
||
25 | 43939 | jjdelcerro | ); |
26 | 43512 | jjdelcerro | } |
27 | |||
28 | @Override
|
||
29 | 43939 | jjdelcerro | public boolean useArgumentsInsteadObjects() { |
30 | return true; |
||
31 | } |
||
32 | |||
33 | @Override
|
||
34 | 44138 | jjdelcerro | public boolean allowConstantFolding() { |
35 | return false; |
||
36 | } |
||
37 | |||
38 | @Override
|
||
39 | 43521 | jjdelcerro | public Object call(Interpreter interpreter, Object[] args) throws Exception { |
40 | 43939 | jjdelcerro | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
41 | } |
||
42 | |||
43 | @Override
|
||
44 | 44138 | jjdelcerro | 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 | 43512 | jjdelcerro | } |
61 | 44138 | jjdelcerro | 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 | 43512 | jjdelcerro | } |
76 | |||
77 | } |