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