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 / LenFunction.java @ 44207
History | View | Annotate | Download (1.88 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.Size; |
11 | import org.gvsig.tools.util.Size64; |
||
12 | 43512 | jjdelcerro | |
13 | 44181 | jjdelcerro | public class LenFunction extends AbstractFunction { |
14 | 43512 | jjdelcerro | |
15 | 44181 | jjdelcerro | public static final String NAME = "LEN"; |
16 | 44138 | jjdelcerro | |
17 | 44181 | jjdelcerro | public LenFunction() {
|
18 | 44138 | jjdelcerro | super(Function.GROUP_PROGRAMMING,
|
19 | NAME, |
||
20 | 44181 | jjdelcerro | Range.is(1),
|
21 | 44138 | jjdelcerro | null,
|
22 | null,
|
||
23 | null,
|
||
24 | 44098 | jjdelcerro | "Object",
|
25 | false
|
||
26 | ); |
||
27 | 43512 | jjdelcerro | } |
28 | 44138 | jjdelcerro | |
29 | 43512 | jjdelcerro | @Override
|
30 | 44138 | jjdelcerro | public boolean allowConstantFolding() { |
31 | return false; |
||
32 | } |
||
33 | |||
34 | @Override
|
||
35 | 43521 | jjdelcerro | public Object call(Interpreter interpreter, Object[] args) throws Exception { |
36 | 44138 | jjdelcerro | Object value;
|
37 | Object obj = getObject(args, 0); |
||
38 | if( obj == null ) { |
||
39 | 44181 | jjdelcerro | return 0; |
40 | } |
||
41 | 44138 | jjdelcerro | if( obj instanceof List ) { |
42 | 44151 | jjdelcerro | List l = ((List)obj); |
43 | 44181 | jjdelcerro | value = l.size(); |
44 | 44138 | jjdelcerro | |
45 | } else if( obj instanceof CharSequence ) { |
||
46 | 44151 | jjdelcerro | CharSequence s = (CharSequence)obj; |
47 | 44181 | jjdelcerro | value = s.length(); |
48 | 44138 | jjdelcerro | |
49 | } else if( obj instanceof Map ) { |
||
50 | 44151 | jjdelcerro | Map m = (Map)obj; |
51 | 44181 | jjdelcerro | value = m.size(); |
52 | 44138 | jjdelcerro | |
53 | 44181 | jjdelcerro | } else if( obj instanceof Size ) { |
54 | Size s = (Size)obj; |
||
55 | value = s.size(); |
||
56 | |||
57 | } else if( obj instanceof Size64 ) { |
||
58 | Size64 s = (Size64)obj; |
||
59 | value = s.size64(); |
||
60 | |||
61 | 43939 | jjdelcerro | } else {
|
62 | 44181 | jjdelcerro | throw new ExpressionRuntimeException("The "+NAME+" function require a String, List, Map or Size and a received a '"+obj.getClass().getSimpleName()+"'."); |
63 | 43939 | jjdelcerro | } |
64 | 44138 | jjdelcerro | return value;
|
65 | 43512 | jjdelcerro | } |
66 | |||
67 | } |