gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dataTypes / impl / DefaultDataType.java @ 630
History | View | Annotate | Download (2.56 KB)
1 | 145 | jjdelcerro | package org.gvsig.tools.dataTypes.impl; |
---|---|---|---|
2 | |||
3 | import java.text.MessageFormat; |
||
4 | |||
5 | 630 | cmartin | import org.slf4j.Logger; |
6 | import org.slf4j.LoggerFactory; |
||
7 | |||
8 | 145 | jjdelcerro | import org.gvsig.tools.dataTypes.CoercionException; |
9 | import org.gvsig.tools.dataTypes.DataType; |
||
10 | import org.gvsig.tools.dataTypes.DataTypes; |
||
11 | import org.gvsig.tools.dataTypes.DataTypesManager.Coercion; |
||
12 | |||
13 | public class DefaultDataType implements DataType { |
||
14 | |||
15 | private static final Logger LOG = LoggerFactory.getLogger(DefaultDataTypesManager.class); |
||
16 | |||
17 | private Coercion coercion;
|
||
18 | private Class defaultClass; |
||
19 | private String subtype; |
||
20 | private int type; |
||
21 | private String name; |
||
22 | |||
23 | DefaultDataType(int type, String subtype, String name, Class defaultClass, Coercion coercion) { |
||
24 | |||
25 | if( name == null ) { |
||
26 | LOG.trace("Can't register null type name for type {}.", new Object[] { Integer.toHexString(type).toUpperCase()}); |
||
27 | throw new IllegalArgumentException(); |
||
28 | } |
||
29 | this.type = type;
|
||
30 | this.subtype = subtype;
|
||
31 | this.name = name;
|
||
32 | this.defaultClass = defaultClass;
|
||
33 | this.coercion = coercion;
|
||
34 | } |
||
35 | |||
36 | public Object coerce(Object value) throws CoercionException { |
||
37 | // http://en.wikipedia.org/wiki/Type_conversion#Implicit_type_conversion
|
||
38 | |||
39 | if( this.coercion != null ) { |
||
40 | return this.coercion.coerce(value); |
||
41 | } |
||
42 | if( defaultClass == null ) { |
||
43 | return value; // ?? |
||
44 | } |
||
45 | if( defaultClass.isInstance(value) ) {
|
||
46 | return value;
|
||
47 | } |
||
48 | throw new CoercionException(); |
||
49 | } |
||
50 | |||
51 | public Coercion getCoercion() {
|
||
52 | return this.coercion; |
||
53 | } |
||
54 | |||
55 | public Class getDefaultClass() { |
||
56 | return this.defaultClass; |
||
57 | } |
||
58 | |||
59 | public String getSubtype() { |
||
60 | return this.subtype; |
||
61 | } |
||
62 | |||
63 | public int getType() { |
||
64 | return this.type; |
||
65 | } |
||
66 | |||
67 | public String getName() { |
||
68 | return this.name; |
||
69 | } |
||
70 | |||
71 | public boolean isContainer() { |
||
72 | return (type & DataTypes.CONTAINER) == DataTypes.CONTAINER;
|
||
73 | } |
||
74 | |||
75 | public boolean isObject() { |
||
76 | return (type & DataTypes.OBJECT) == DataTypes.OBJECT;
|
||
77 | } |
||
78 | |||
79 | 630 | cmartin | public boolean isDynObject() { |
80 | return type == DataTypes.DYNOBJECT;
|
||
81 | } |
||
82 | |||
83 | 145 | jjdelcerro | public void setCoercion(Coercion coercion) { |
84 | this.coercion = coercion;
|
||
85 | LOG.trace("Add coercion operation for data type {}.", new Object[] { name }); |
||
86 | } |
||
87 | |||
88 | public String toString() { |
||
89 | return MessageFormat.format( |
||
90 | "type=0x{0};subtype={1};name={2};class={3};coercion={4}",
|
||
91 | new Object[] { |
||
92 | Integer.toHexString(type).toUpperCase(),
|
||
93 | subtype, |
||
94 | name, |
||
95 | defaultClass==null? null:defaultClass.getName(), |
||
96 | coercion==null? null:coercion.getClass().getName() |
||
97 | } |
||
98 | ); |
||
99 | } |
||
100 | 216 | jjdelcerro | |
101 | public boolean isNumeric() { |
||
102 | if( type == DataTypes.DOUBLE ||
|
||
103 | type == DataTypes.FLOAT || |
||
104 | type == DataTypes.INT || |
||
105 | type == DataTypes.LONG){ |
||
106 | return true; |
||
107 | } |
||
108 | return false; |
||
109 | } |
||
110 | |||
111 | 145 | jjdelcerro | } |