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