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 @ 45011
History | View | Annotate | Download (4.42 KB)
1 |
package org.gvsig.expressionevaluator.impl.function.programming; |
---|---|
2 |
|
3 |
import java.util.List; |
4 |
import java.util.Map; |
5 |
import javax.json.JsonArray; |
6 |
import javax.json.JsonObject; |
7 |
import org.apache.commons.lang3.Range; |
8 |
import org.apache.commons.lang3.StringUtils; |
9 |
import org.gvsig.expressionevaluator.Code; |
10 |
import org.gvsig.expressionevaluator.Codes; |
11 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
12 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_GETITEM; |
13 |
import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
14 |
import org.gvsig.expressionevaluator.Formatter; |
15 |
import org.gvsig.expressionevaluator.Function; |
16 |
import org.gvsig.expressionevaluator.Interpreter; |
17 |
import org.gvsig.expressionevaluator.impl.JsonUtils; |
18 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
19 |
import org.gvsig.tools.util.GetItem; |
20 |
import org.gvsig.tools.util.GetItem64; |
21 |
import org.gvsig.tools.util.GetItemByKey; |
22 |
import org.gvsig.tools.util.PropertiesSupport; |
23 |
|
24 |
public class GetitemFunction extends AbstractFunction { |
25 |
|
26 |
public GetitemFunction() {
|
27 |
super(Function.GROUP_PROGRAMMING,
|
28 |
FUNCTION_GETITEM, |
29 |
Range.is(2),
|
30 |
null,
|
31 |
FUNCTION_GETITEM+"({{object}}, index_or_key)",
|
32 |
null,
|
33 |
"Object",
|
34 |
false
|
35 |
); |
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 |
Object value;
|
46 |
Object obj = getObject(args, 0); |
47 |
if( obj == null ) { |
48 |
throw new ExpressionRuntimeException("Null is not valid in "+FUNCTION_GETITEM+" function."); |
49 |
} |
50 |
|
51 |
if( obj instanceof List ) { |
52 |
List l = ((List)obj); |
53 |
int index = getInt(args,1); |
54 |
if( index<0 ) { |
55 |
index = l.size() + index; |
56 |
} |
57 |
value = l.get(index); |
58 |
} else if( obj instanceof Object[] ) { |
59 |
Object[] a = ((Object[])obj); |
60 |
int index = getInt(args,1); |
61 |
if( index<0 ) { |
62 |
index = a.length + index; |
63 |
} |
64 |
value = a[index]; |
65 |
|
66 |
} else if( obj instanceof CharSequence ) { |
67 |
CharSequence s = (CharSequence)obj; |
68 |
int index = getInt(args,1); |
69 |
if( index<0 ) { |
70 |
index = s.length() + index; |
71 |
} |
72 |
value = s.subSequence(index, index+1);
|
73 |
|
74 |
} else if( obj instanceof Map ) { |
75 |
Map m = (Map)obj; |
76 |
Object index = getObject(args,1); |
77 |
value = m.get(index); |
78 |
|
79 |
} else if( obj instanceof JsonArray ) { |
80 |
JsonArray l = ((JsonArray)obj); |
81 |
int index = getInt(args,1); |
82 |
if( index<0 ) { |
83 |
index = l.size() + index; |
84 |
} |
85 |
value = JsonUtils.get(l.get(index)); |
86 |
|
87 |
} else if( obj instanceof JsonObject ) { |
88 |
JsonObject jsonobj = ((JsonObject)obj); |
89 |
String name = getStr(args,1); |
90 |
value = JsonUtils.get(jsonobj.get(name)); |
91 |
|
92 |
} else if( obj instanceof GetItem ) { |
93 |
GetItem l = (GetItem)obj; |
94 |
int index = getInt(args,1); |
95 |
value = l.get(index); |
96 |
|
97 |
} else if( obj instanceof GetItem64 ) { |
98 |
GetItem64 l = (GetItem64)obj; |
99 |
long index = getLong(args,1); |
100 |
value = l.get64(index); |
101 |
|
102 |
} else if( obj instanceof GetItemByKey ) { |
103 |
GetItemByKey l = (GetItemByKey)obj; |
104 |
Object key = getObject(args,1); |
105 |
value = l.get(key); |
106 |
|
107 |
} else if( obj instanceof PropertiesSupport ) { |
108 |
PropertiesSupport l = (PropertiesSupport)obj; |
109 |
String propName = getStr(args,1); |
110 |
value = l.getProperty(propName); |
111 |
|
112 |
} else {
|
113 |
throw new ExpressionRuntimeException("The "+FUNCTION_GETITEM+" function require a String, List or Map and a received a '"+obj.getClass().getSimpleName()+"'."); |
114 |
} |
115 |
return value;
|
116 |
} |
117 |
|
118 |
@Override
|
119 |
public String toString(Codes args, Formatter<Code> formatter) { |
120 |
StringBuilder builder = new StringBuilder(); |
121 |
Code arg0 = args.get(0);
|
122 |
Code arg1 = args.get(1);
|
123 |
builder.append(arg0.toString(formatter)); |
124 |
builder.append("[");
|
125 |
builder.append(arg1.toString(formatter)); |
126 |
builder.append("]");
|
127 |
return builder.toString();
|
128 |
} |
129 |
|
130 |
|
131 |
} |