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 / typeconversion / CastFunction.java @ 44198
History | View | Annotate | Download (2.13 KB)
1 | 44139 | jjdelcerro | package org.gvsig.expressionevaluator.impl.function.typeconversion; |
---|---|---|---|
2 | 43512 | jjdelcerro | |
3 | 44139 | jjdelcerro | import java.util.Iterator; |
4 | 43512 | jjdelcerro | import org.apache.commons.lang3.Range; |
5 | 44198 | jjdelcerro | import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_CAST; |
6 | 44139 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
7 | 43521 | jjdelcerro | import org.gvsig.expressionevaluator.Interpreter; |
8 | 43512 | jjdelcerro | import org.gvsig.expressionevaluator.spi.AbstractFunction; |
9 | 44139 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
10 | import org.gvsig.tools.dataTypes.DataType; |
||
11 | import org.gvsig.tools.dataTypes.DataTypes; |
||
12 | import org.gvsig.tools.dataTypes.DataTypesManager; |
||
13 | import org.gvsig.tools.dataTypes.DataTypesManager.Coercion; |
||
14 | 43512 | jjdelcerro | |
15 | 44139 | jjdelcerro | public class CastFunction extends AbstractFunction { |
16 | |||
17 | public CastFunction() {
|
||
18 | 44198 | jjdelcerro | super(GROUP_CONVERSION, FUNCTION_CAST, Range.is(2)); |
19 | 43512 | jjdelcerro | } |
20 | |||
21 | @Override
|
||
22 | 44009 | jjdelcerro | public boolean allowConstantFolding() { |
23 | return true; |
||
24 | } |
||
25 | |||
26 | @Override
|
||
27 | 43521 | jjdelcerro | public Object call(Interpreter interpreter, Object[] args) throws Exception { |
28 | 44139 | jjdelcerro | DataTypesManager manager = ToolsLocator.getDataTypesManager(); |
29 | |||
30 | Object valueIn = this.getObject(args, 0); |
||
31 | String typeName = this.getStr(args, 1); |
||
32 | |||
33 | int type = manager.getType(typeName);
|
||
34 | if( type == DataTypes.INVALID ) {
|
||
35 | throw new ExpressionRuntimeException("The value of type ("+typeName+") in function CAST is incorrect", getTip()); |
||
36 | } |
||
37 | Coercion converter = manager.get(type).getCoercion(); |
||
38 | if( converter==null ) { |
||
39 | throw new IllegalArgumentException("Can't convert to the indicated type ("+typeName+")."); |
||
40 | } |
||
41 | Object valueOut = converter.coerce(valueIn);
|
||
42 | return valueOut;
|
||
43 | } |
||
44 | |||
45 | private String getTip() { |
||
46 | DataTypesManager manager = ToolsLocator.getDataTypesManager(); |
||
47 | |||
48 | 43512 | jjdelcerro | StringBuilder builder = new StringBuilder(); |
49 | 44139 | jjdelcerro | Iterator<DataType> it = manager.iterator();
|
50 | while( it.hasNext() ) {
|
||
51 | DataType dataType = it.next(); |
||
52 | builder.append("- ");
|
||
53 | builder.append(dataType.getName()); |
||
54 | builder.append("\n");
|
||
55 | 43512 | jjdelcerro | } |
56 | 44139 | jjdelcerro | |
57 | return "The valid type names for the function CAST are:\n"+ builder.toString(); |
||
58 | 43512 | jjdelcerro | } |
59 | |||
60 | } |