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 / GetitemFunction.java @ 44207
History | View | Annotate | Download (2.6 KB)
1 |
package org.gvsig.expressionevaluator.impl.function.programming; |
---|---|
2 |
|
3 |
import java.util.List; |
4 |
import java.util.Map; |
5 |
import org.apache.commons.lang3.Range; |
6 |
import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
7 |
import org.gvsig.expressionevaluator.Function; |
8 |
import org.gvsig.expressionevaluator.Interpreter; |
9 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
10 |
import org.gvsig.tools.util.GetItem; |
11 |
import org.gvsig.tools.util.GetItem64; |
12 |
import org.gvsig.tools.util.GetItemByKey; |
13 |
|
14 |
public class GetitemFunction extends AbstractFunction { |
15 |
|
16 |
public static final String NAME = "GETITEM"; |
17 |
|
18 |
public GetitemFunction() {
|
19 |
super(Function.GROUP_PROGRAMMING,
|
20 |
NAME, |
21 |
Range.is(2),
|
22 |
null,
|
23 |
null,
|
24 |
null,
|
25 |
"Object",
|
26 |
false
|
27 |
); |
28 |
} |
29 |
|
30 |
@Override
|
31 |
public boolean allowConstantFolding() { |
32 |
return false; |
33 |
} |
34 |
|
35 |
@Override
|
36 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
37 |
Object value;
|
38 |
Object obj = getObject(args, 0); |
39 |
if( obj == null ) { |
40 |
throw new ExpressionRuntimeException("Null is not valid in "+NAME+" function."); |
41 |
} |
42 |
|
43 |
if( obj instanceof List ) { |
44 |
List l = ((List)obj); |
45 |
int index = getInt(args,1); |
46 |
if( index<0 ) { |
47 |
index = l.size() + index; |
48 |
} |
49 |
value = l.get(index); |
50 |
|
51 |
} else if( obj instanceof CharSequence ) { |
52 |
CharSequence s = (CharSequence)obj; |
53 |
int index = getInt(args,1); |
54 |
if( index<0 ) { |
55 |
index = s.length() + index; |
56 |
} |
57 |
value = s.subSequence(index, index+1);
|
58 |
|
59 |
} else if( obj instanceof Map ) { |
60 |
Map m = (Map)obj; |
61 |
Object index = getObject(args,1); |
62 |
value = m.get(index); |
63 |
|
64 |
} else if( obj instanceof GetItem ) { |
65 |
GetItem l = (GetItem)obj; |
66 |
int index = getInt(args,1); |
67 |
value = l.get(index); |
68 |
|
69 |
} else if( obj instanceof GetItem64 ) { |
70 |
GetItem64 l = (GetItem64)obj; |
71 |
long index = getLong(args,1); |
72 |
value = l.get64(index); |
73 |
|
74 |
} else if( obj instanceof GetItemByKey ) { |
75 |
GetItemByKey l = (GetItemByKey)obj; |
76 |
Object key = getObject(args,1); |
77 |
value = l.get(key); |
78 |
|
79 |
} else {
|
80 |
throw new ExpressionRuntimeException("The "+NAME+" function require a String, List or Map and a received a '"+obj.getClass().getSimpleName()+"'."); |
81 |
} |
82 |
return value;
|
83 |
} |
84 |
|
85 |
} |