Statistics
| Revision:

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 @ 45043

History | View | Annotate | Download (4.34 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.gvsig.expressionevaluator.Code;
9
import org.gvsig.expressionevaluator.Codes;
10
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_GETITEM;
11
import org.gvsig.expressionevaluator.ExpressionRuntimeException;
12
import org.gvsig.expressionevaluator.Formatter;
13
import org.gvsig.expressionevaluator.Function;
14
import org.gvsig.expressionevaluator.Interpreter;
15
import org.gvsig.expressionevaluator.spi.JsonUtils;
16
import org.gvsig.expressionevaluator.spi.AbstractFunction;
17
import org.gvsig.tools.util.GetItem;
18
import org.gvsig.tools.util.GetItem64;
19
import org.gvsig.tools.util.GetItemByKey;
20
import org.gvsig.tools.util.PropertiesSupport;
21

    
22
public class GetitemFunction extends AbstractFunction {
23

    
24
    public GetitemFunction() {
25
        super(Function.GROUP_PROGRAMMING, 
26
            FUNCTION_GETITEM, 
27
            Range.is(2),
28
            null,
29
            FUNCTION_GETITEM+"({{object}}, index_or_key)",
30
            null,
31
            "Object",
32
            false
33
        );
34
    }
35
    
36
    @Override
37
    public boolean allowConstantFolding() {
38
        return false;
39
    }
40
    
41
    @Override
42
    public Object call(Interpreter interpreter, Object[] args) throws Exception {
43
        Object value;
44
        Object obj = getObject(args, 0);
45
        if( obj == null ) {
46
            throw new ExpressionRuntimeException("Null is not valid in "+FUNCTION_GETITEM+" function.");
47
        }
48
        
49
        if( obj instanceof List ) {
50
            List l = ((List)obj);
51
            int index = getInt(args,1);
52
            if( index<0 ) {
53
                index = l.size() + index;
54
            }
55
            value = l.get(index);
56
        } else if( obj instanceof Object[] ) {
57
            Object[] a = ((Object[])obj);
58
            int index = getInt(args,1);
59
            if( index<0 ) {
60
                index = a.length + index;
61
            }
62
            value = a[index];
63
            
64
        } else if( obj instanceof CharSequence ) {
65
            CharSequence s = (CharSequence)obj;
66
            int index = getInt(args,1);
67
            if( index<0 ) {
68
                index = s.length() + index;
69
            }
70
            value = s.subSequence(index, index+1);
71

    
72
        } else if( obj instanceof Map ) {
73
            Map m = (Map)obj;
74
            Object index = getObject(args,1);
75
            value = m.get(index);
76
            
77
        } else if( obj instanceof JsonArray ) {
78
            JsonArray jsonArray = ((JsonArray)obj);
79
            int index = getInt(args,1);
80
            if( index<0 ) {
81
                index = jsonArray.size() + index;
82
            }
83
            value = JsonUtils.getitem(jsonArray, index);
84
            
85
        } else if( obj instanceof JsonObject ) {
86
            JsonObject jsonobj = ((JsonObject)obj);
87
            String name = getStr(args,1);
88
            value = JsonUtils.getitem(jsonobj, name);
89
            
90
        } else if( obj instanceof GetItem ) {
91
            GetItem l = (GetItem)obj;
92
            int index = getInt(args,1);
93
            value = l.get(index);
94
            
95
        } else if( obj instanceof GetItem64 ) {
96
            GetItem64 l = (GetItem64)obj;
97
            long index = getLong(args,1);
98
            value = l.get64(index);
99
            
100
        } else if( obj instanceof GetItemByKey ) {
101
            GetItemByKey l = (GetItemByKey)obj;
102
            Object key = getObject(args,1);
103
            value = l.get(key);
104
            
105
        } else if( obj instanceof PropertiesSupport ) {
106
            PropertiesSupport l = (PropertiesSupport)obj;
107
            String propName = getStr(args,1);
108
            value = l.getProperty(propName);
109
            
110
        } else {
111
            throw new ExpressionRuntimeException("The "+FUNCTION_GETITEM+" function require a String, List or Map and a received a '"+obj.getClass().getSimpleName()+"'.");
112
        }
113
        return value;
114
    }
115

    
116
    @Override
117
    public String toString(Codes args, Formatter<Code> formatter) {
118
        StringBuilder builder = new StringBuilder();
119
        Code arg0 = args.get(0);
120
        Code arg1 = args.get(1);
121
        builder.append(arg0.toString(formatter));
122
        builder.append("[");
123
        builder.append(arg1.toString(formatter));
124
        builder.append("]");
125
        return builder.toString();
126
    }
127
    
128
    
129
}