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 / string / MidFunction.java @ 44139
History | View | Annotate | Download (1.12 KB)
1 |
package org.gvsig.expressionevaluator.impl.function.string; |
---|---|
2 |
|
3 |
import org.apache.commons.lang3.Range; |
4 |
import org.apache.commons.lang3.StringUtils; |
5 |
import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
6 |
import org.gvsig.expressionevaluator.Interpreter; |
7 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
8 |
|
9 |
public class MidFunction extends AbstractFunction { |
10 |
|
11 |
public MidFunction() {
|
12 |
super("String", "MID", Range.between(2, 3)); |
13 |
} |
14 |
|
15 |
|
16 |
@Override
|
17 |
public boolean allowConstantFolding() { |
18 |
return true; |
19 |
} |
20 |
|
21 |
@Override
|
22 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
23 |
String value;
|
24 |
|
25 |
String str = getStr(args,0); |
26 |
int start = getInt(args, 1); |
27 |
|
28 |
switch(args.length) {
|
29 |
case 2: |
30 |
value = StringUtils.substring(str, start); |
31 |
return value;
|
32 |
case 3: |
33 |
int len = getInt(args, 2); |
34 |
value = StringUtils.mid(str, start, len); |
35 |
return value;
|
36 |
} |
37 |
throw new ExpressionRuntimeException("Incorrect arguments number"); |
38 |
} |
39 |
|
40 |
} |