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 / ForEachFunction.java @ 44207
History | View | Annotate | Download (2.03 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.expressionevaluator.ExpressionRuntimeException; |
9 | import org.gvsig.expressionevaluator.MutableSymbolTable; |
||
10 | 43512 | jjdelcerro | |
11 | 44138 | jjdelcerro | public class ForEachFunction extends AbstractFunction { |
12 | |||
13 | public static final String NAME = "FOREACH"; |
||
14 | 43512 | jjdelcerro | |
15 | 44138 | jjdelcerro | public ForEachFunction() {
|
16 | super(Function.GROUP_PROGRAMMING,
|
||
17 | NAME, |
||
18 | Range.is(3),
|
||
19 | "The foreach() function evaluate body for every element of the iterable.",
|
||
20 | NAME+"( varname, iterable, body )",
|
||
21 | 44098 | jjdelcerro | null,
|
22 | "Object",
|
||
23 | false
|
||
24 | 43939 | jjdelcerro | ); |
25 | 43512 | jjdelcerro | } |
26 | |||
27 | @Override
|
||
28 | 43939 | jjdelcerro | public boolean useArgumentsInsteadObjects() { |
29 | return true; |
||
30 | } |
||
31 | |||
32 | @Override
|
||
33 | 44138 | jjdelcerro | public boolean allowConstantFolding() { |
34 | return false; |
||
35 | } |
||
36 | |||
37 | @Override
|
||
38 | 43521 | jjdelcerro | public Object call(Interpreter interpreter, Object[] args) throws Exception { |
39 | 43939 | jjdelcerro | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
40 | } |
||
41 | |||
42 | @Override
|
||
43 | 44138 | jjdelcerro | public Object call(Interpreter interpreter, Codes args) throws Exception { |
44 | if( !(interpreter.getSymbolTable() instanceof MutableSymbolTable) ) { |
||
45 | throw new ExpressionRuntimeException("The use of forech loops require a mutable symbol table."); |
||
46 | } |
||
47 | MutableSymbolTable symbolTable = (MutableSymbolTable) interpreter.getSymbolTable(); |
||
48 | |||
49 | 43939 | jjdelcerro | Object body = null; |
50 | 44138 | jjdelcerro | String varname = getObject(interpreter, args, 0).toString(); |
51 | |||
52 | Iterable iterable = (Iterable) getObject(interpreter, args, 1); |
||
53 | for (Object value : iterable) { |
||
54 | symbolTable.setVar(varname, value); |
||
55 | body = getObject(interpreter, args, 2);
|
||
56 | 43512 | jjdelcerro | } |
57 | 43939 | jjdelcerro | return body;
|
58 | 43512 | jjdelcerro | } |
59 | |||
60 | } |