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