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 / ForFunction.java @ 47017
History | View | Annotate | Download (2.47 KB)
1 |
package org.gvsig.expressionevaluator.impl.function.programming; |
---|---|
2 |
|
3 |
import java.util.Objects; |
4 |
import org.apache.commons.lang3.Range; |
5 |
import org.gvsig.expressionevaluator.Codes; |
6 |
import org.gvsig.expressionevaluator.Function; |
7 |
import org.gvsig.expressionevaluator.Interpreter; |
8 |
import org.gvsig.expressionevaluator.impl.function.programming.BreakFunction.BreakException; |
9 |
import org.gvsig.expressionevaluator.impl.function.programming.ReturnFunction.ReturnException; |
10 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
11 |
|
12 |
public class ForFunction extends AbstractFunction { |
13 |
|
14 |
public static final String NAME = "FOR"; |
15 |
|
16 |
public ForFunction() {
|
17 |
super(Function.GROUP_PROGRAMMING,
|
18 |
NAME, |
19 |
Range.is(4),
|
20 |
"The for() function evaluate body and incr while the condition is true.",
|
21 |
NAME+"( init, condition, incr, body )",
|
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 |
int argn = 1; |
51 |
Object body = null; |
52 |
Object init = getObject(interpreter, args, 0); |
53 |
boolean condition = getBoolean(interpreter, args, 1); |
54 |
while( condition ) {
|
55 |
try {
|
56 |
body = getObject(interpreter, args, 3);
|
57 |
} catch(BreakException ex) {
|
58 |
break;
|
59 |
} catch(ReturnException ex) {
|
60 |
throw ex;
|
61 |
} catch(Exception ex) { |
62 |
String stmt = "unknown"; |
63 |
try {
|
64 |
stmt = Objects.toString(args); |
65 |
} catch(Exception ex2) { |
66 |
|
67 |
} |
68 |
LOGGER.warn("Error in for function calling arg "+ argn + ", "+ stmt, ex); |
69 |
throw ex;
|
70 |
} |
71 |
argn++; |
72 |
|
73 |
Object incr = getObject(interpreter, args, 2); |
74 |
condition = getBoolean(interpreter, args, 1);
|
75 |
} |
76 |
return body;
|
77 |
} |
78 |
|
79 |
} |