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

History | View | Annotate | Download (2.86 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
import org.gvsig.tools.util.PropertiesSupport;
14

    
15
public class GetitemFunction extends AbstractFunction {
16

    
17
    public static final String NAME = "GETITEM";
18
    
19
    public GetitemFunction() {
20
        super(Function.GROUP_PROGRAMMING, 
21
            NAME, 
22
            Range.is(2),
23
            null,
24
            null,
25
            null,
26
            "Object",
27
            false
28
        );
29
    }
30
    
31
    @Override
32
    public boolean allowConstantFolding() {
33
        return false;
34
    }
35
    
36
    @Override
37
    public Object call(Interpreter interpreter, Object[] args) throws Exception {
38
        Object value;
39
        Object obj = getObject(args, 0);
40
        if( obj == null ) {
41
            throw new ExpressionRuntimeException("Null is not valid in "+NAME+" function.");
42
        }
43
        
44
        if( obj instanceof List ) {
45
            List l = ((List)obj);
46
            int index = getInt(args,1);
47
            if( index<0 ) {
48
                index = l.size() + index;
49
            }
50
            value = l.get(index);
51
        
52
        } else if( obj instanceof CharSequence ) {
53
            CharSequence s = (CharSequence)obj;
54
            int index = getInt(args,1);
55
            if( index<0 ) {
56
                index = s.length() + index;
57
            }
58
            value = s.subSequence(index, index+1);
59

    
60
        } else if( obj instanceof Map ) {
61
            Map m = (Map)obj;
62
            Object index = getObject(args,1);
63
            value = m.get(index);
64
            
65
        } 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
        } else if( obj instanceof GetItemByKey ) {
76
            GetItemByKey l = (GetItemByKey)obj;
77
            Object key = getObject(args,1);
78
            value = l.get(key);
79
            
80
        } else if( obj instanceof PropertiesSupport ) {
81
            PropertiesSupport l = (PropertiesSupport)obj;
82
            String propName = getStr(args,1);
83
            value = l.getProperty(propName);
84
            
85
        } else {
86
            throw new ExpressionRuntimeException("The "+NAME+" function require a String, List or Map and a received a '"+obj.getClass().getSimpleName()+"'.");
87
        }
88
        return value;
89
    }
90
    
91
}