gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dataTypes / impl / DefaultDataTypesManager.java @ 130
History | View | Annotate | Download (7.74 KB)
1 |
package org.gvsig.tools.dataTypes.impl; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.util.Date; |
5 |
import java.util.List; |
6 |
import java.util.Map; |
7 |
import java.util.Set; |
8 |
|
9 |
import org.gvsig.tools.dataTypes.CoercionException; |
10 |
import org.gvsig.tools.dataTypes.DataTypes; |
11 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
12 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToBoolean; |
13 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToByte; |
14 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDate; |
15 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDouble; |
16 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToFile; |
17 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToFloat; |
18 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToInt; |
19 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToLong; |
20 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToString; |
21 |
import org.gvsig.tools.dynobject.DynObject; |
22 |
|
23 |
public class DefaultDataTypesManager implements DataTypesManager { |
24 |
|
25 |
public void addtype(int type, String subtype, String name, Class defaultClass, Coercion coercion) { |
26 |
if( (type & DataTypes.OBJECT) != DataTypes.OBJECT ) {
|
27 |
throw new IllegalArgumentException(); |
28 |
} |
29 |
if( name == null || TYPE_NAMES[type] != null ) { |
30 |
throw new IllegalArgumentException(); |
31 |
} |
32 |
SUBTYPES[type]=subtype; |
33 |
TYPE_NAMES[type]=name; |
34 |
DEFAULT_CLASSES[type]=defaultClass; |
35 |
COERCION[type]=coercion; |
36 |
} |
37 |
|
38 |
public boolean isValidType(int type) { |
39 |
return type>0 && type < 0x41 && TYPE_NAMES[type] != null; |
40 |
} |
41 |
|
42 |
public boolean isObject(int type) { |
43 |
return (type & DataTypes.OBJECT) == DataTypes.OBJECT;
|
44 |
} |
45 |
|
46 |
public boolean isContainer(int type) { |
47 |
return (type & DataTypes.CONTAINER) == DataTypes.CONTAINER;
|
48 |
} |
49 |
|
50 |
public String getTypeName(int type) { |
51 |
if( type<1 || type>0x40 ) { |
52 |
return "unknow"; |
53 |
} |
54 |
String name = TYPE_NAMES[type];
|
55 |
if( name==null ) { |
56 |
return "unknow"; |
57 |
} |
58 |
return name;
|
59 |
} |
60 |
|
61 |
public int getType(String name) { |
62 |
if( name != null ) { |
63 |
for( int i=0; i<TYPE_NAMES.length; i++ ) { |
64 |
if( name.equalsIgnoreCase(TYPE_NAMES[i]) ) {
|
65 |
return i;
|
66 |
} |
67 |
} |
68 |
} |
69 |
return DataTypes.INVALID;
|
70 |
} |
71 |
|
72 |
public Class getDefaultClass(int type) { |
73 |
if( type<1 || type>0x40 ) { |
74 |
return null; |
75 |
} |
76 |
return DEFAULT_CLASSES[type];
|
77 |
} |
78 |
|
79 |
public String getSubtype(int type) { |
80 |
if( type<1 || type>0x40 ) { |
81 |
return null; |
82 |
} |
83 |
return SUBTYPES[type];
|
84 |
} |
85 |
|
86 |
public Coercion getCoercion(int type) { |
87 |
// http://en.wikipedia.org/wiki/Type_conversion#Implicit_type_conversion
|
88 |
|
89 |
return COERCION[type];
|
90 |
} |
91 |
|
92 |
public void setCoercion(int type, Coercion coercion) { |
93 |
if( (type & DataTypes.OBJECT) != DataTypes.OBJECT ) {
|
94 |
throw new IllegalArgumentException(); |
95 |
} |
96 |
COERCION[type]=coercion; |
97 |
} |
98 |
|
99 |
public Object coerce(int type, Object value) throws CoercionException { |
100 |
Coercion coercion = this.getCoercion(type);
|
101 |
if( coercion != null ) { |
102 |
return coercion.coerce(value);
|
103 |
} |
104 |
Class defaultClass = this.getDefaultClass(type); |
105 |
if( defaultClass == null ) { |
106 |
return value; // ?? |
107 |
} |
108 |
if( defaultClass.isInstance(value) ) {
|
109 |
return value;
|
110 |
} |
111 |
throw new CoercionException(); |
112 |
} |
113 |
|
114 |
private static final String[] SUBTYPES = new String[] { |
115 |
/* 0x00 */ null, |
116 |
/* 0x01 */ null, |
117 |
/* 0x02 */ null, |
118 |
/* 0x03 */ null, |
119 |
/* 0x04 */ null, |
120 |
/* 0x05 */ null, |
121 |
/* 0x06 */ null, |
122 |
/* 0x07 */ null, |
123 |
/* 0x08 */ null, |
124 |
/* 0x09 */ DataTypes.SUBTYPE_DATE,
|
125 |
/* 0x0a */ DataTypes.SUBTYPE_DATE,
|
126 |
/* 0x0b */ DataTypes.SUBTYPE_DATE,
|
127 |
/* 0x0c */ null, |
128 |
/* 0x0d */ null, |
129 |
/* 0x0e */ null, |
130 |
/* 0x0f */ null, |
131 |
|
132 |
/* 0x14 */ null, |
133 |
/* 0x11 */ null, |
134 |
/* 0x12 */ null, |
135 |
/* 0x13 */ null, |
136 |
/* 0x14 */ null, |
137 |
/* 0x15 */ null, |
138 |
/* 0x16 */ null, |
139 |
/* 0x17 */ null, |
140 |
/* 0x18 */ null, |
141 |
/* 0x19 */ null, |
142 |
/* 0x1a */ null, |
143 |
/* 0x1b */ null, |
144 |
/* 0x1c */ null, |
145 |
/* 0x1d */ DataTypes.SUBTYPE_FILE,
|
146 |
/* 0x1e */ DataTypes.SUBTYPE_FOLDER,
|
147 |
/* 0x1f */ null, |
148 |
|
149 |
|
150 |
/* 0x20 */ null, |
151 |
/* 0x21 */ null, |
152 |
/* 0x22 */ null, |
153 |
/* 0x23 */ null, |
154 |
/* 0x24 */ null, |
155 |
/* 0x25 */ null, |
156 |
/* 0x26 */ null, |
157 |
/* 0x27 */ null, |
158 |
/* 0x28 */ null, |
159 |
/* 0x29 */ null, |
160 |
/* 0x30 */ null, |
161 |
/* 0x31 */ null, |
162 |
/* 0x32 */ null, |
163 |
/* 0x33 */ null, |
164 |
/* 0x34 */ null, |
165 |
/* 0x35 */ null, |
166 |
/* 0x36 */ null, |
167 |
/* 0x37 */ null, |
168 |
/* 0x38 */ null, |
169 |
/* 0x39 */ null, |
170 |
/* 0x40 */ null, |
171 |
|
172 |
}; |
173 |
|
174 |
|
175 |
private static final String[] TYPE_NAMES = new String[] { |
176 |
/* 0x00 */ null, |
177 |
/* 0x01 */ "Boolean", |
178 |
/* 0x02 */ "Byte", |
179 |
/* 0x03 */ "Char", |
180 |
/* 0x04 */ "Integer", |
181 |
/* 0x05 */ "Long", |
182 |
/* 0x06 */ "Float", |
183 |
/* 0x07 */ "Double", |
184 |
/* 0x08 */ "String", |
185 |
/* 0x09 */ "Date", |
186 |
/* 0x0a */ "Time", |
187 |
/* 0x0b */ "Timestamp", |
188 |
/* 0x0c */ "ByteArray", |
189 |
/* 0x0d */ "File", |
190 |
/* 0x0e */ "Folder", |
191 |
/* 0x0f */ null, |
192 |
|
193 |
/* 0x14 */ null, |
194 |
/* 0x11 */ "Array", |
195 |
/* 0x12 */ "List", |
196 |
/* 0x13 */ "Set", |
197 |
/* 0x14 */ "Map", |
198 |
/* 0x15 */ "DynObject", |
199 |
/* 0x16 */ null, |
200 |
/* 0x17 */ null, |
201 |
/* 0x18 */ null, |
202 |
/* 0x19 */ null, |
203 |
/* 0x1a */ null, |
204 |
/* 0x1b */ null, |
205 |
/* 0x1c */ null, |
206 |
/* 0x1d */ null, |
207 |
/* 0x1e */ null, |
208 |
/* 0x1f */ null, |
209 |
|
210 |
|
211 |
/* 0x20 */ "Object", |
212 |
/* 0x21 */ null, |
213 |
/* 0x22 */ null, |
214 |
/* 0x23 */ null, |
215 |
/* 0x24 */ null, |
216 |
/* 0x25 */ null, |
217 |
/* 0x26 */ null, |
218 |
/* 0x27 */ null, |
219 |
/* 0x28 */ null, |
220 |
/* 0x29 */ null, |
221 |
/* 0x30 */ null, |
222 |
/* 0x31 */ null, |
223 |
/* 0x32 */ null, |
224 |
/* 0x33 */ null, |
225 |
/* 0x34 */ null, |
226 |
/* 0x35 */ null, |
227 |
/* 0x36 */ null, |
228 |
/* 0x37 */ null, |
229 |
/* 0x38 */ null, |
230 |
/* 0x39 */ null, |
231 |
/* 0x40 */ null, |
232 |
|
233 |
}; |
234 |
|
235 |
private static final Class[] DEFAULT_CLASSES = new Class[] { |
236 |
/* 0x00 */ null, |
237 |
/* 0x01 */ Boolean.class, |
238 |
/* 0x02 */ Byte.class, |
239 |
/* 0x03 */ Character.class, |
240 |
/* 0x04 */ Integer.class, |
241 |
/* 0x05 */ Long.class, |
242 |
/* 0x06 */ Float.class, |
243 |
/* 0x07 */ Double.class, |
244 |
/* 0x08 */ String.class, |
245 |
/* 0x09 */ Date.class, |
246 |
/* 0x0a */ Date.class, |
247 |
/* 0x0b */ Date.class, |
248 |
/* 0x0c */ new byte[] {}.getClass(), |
249 |
/* 0x0d */ File.class, |
250 |
/* 0x0e */ File.class, |
251 |
/* 0x0f */ null, |
252 |
|
253 |
/* 0x10 */ null, |
254 |
/* 0x11 */ new Object[] {}.getClass(), |
255 |
/* 0x12 */ List.class, |
256 |
/* 0x13 */ Set.class, |
257 |
/* 0x14 */ Map.class, |
258 |
/* 0x15 */ DynObject.class,
|
259 |
/* 0x16 */ null, |
260 |
/* 0x17 */ null, |
261 |
/* 0x18 */ null, |
262 |
/* 0x19 */ null, |
263 |
/* 0x1a */ null, |
264 |
/* 0x1b */ null, |
265 |
/* 0x1c */ null, |
266 |
/* 0x1d */ null, |
267 |
/* 0x1e */ null, |
268 |
/* 0x1f */ null, |
269 |
|
270 |
|
271 |
/* 0x20 */ Object.class, |
272 |
/* 0x21 */ null, |
273 |
/* 0x22 */ null, |
274 |
/* 0x23 */ null, |
275 |
/* 0x24 */ null, |
276 |
/* 0x25 */ null, |
277 |
/* 0x26 */ null, |
278 |
/* 0x27 */ null, |
279 |
/* 0x28 */ null, |
280 |
/* 0x29 */ null, |
281 |
/* 0x30 */ null, |
282 |
/* 0x31 */ null, |
283 |
/* 0x32 */ null, |
284 |
/* 0x33 */ null, |
285 |
/* 0x34 */ null, |
286 |
/* 0x35 */ null, |
287 |
/* 0x36 */ null, |
288 |
/* 0x37 */ null, |
289 |
/* 0x38 */ null, |
290 |
/* 0x39 */ null, |
291 |
/* 0x40 */ null, |
292 |
|
293 |
}; |
294 |
|
295 |
private static final Coercion[] COERCION = new Coercion[] { |
296 |
/* 0x00 */ null, |
297 |
/* 0x01 */ new CoerceToBoolean(), |
298 |
/* 0x02 */ new CoerceToByte(), |
299 |
/* 0x03 */ new CoerceToString(), |
300 |
/* 0x04 */ new CoerceToInt(), |
301 |
/* 0x05 */ new CoerceToLong(), |
302 |
/* 0x06 */ new CoerceToFloat(), |
303 |
/* 0x07 */ new CoerceToDouble(), |
304 |
/* 0x08 */ new CoerceToString(), |
305 |
/* 0x09 */ new CoerceToDate(), |
306 |
/* 0x0a */ new CoerceToDate(), |
307 |
/* 0x0b */ new CoerceToDate(), |
308 |
/* 0x0c */ null, |
309 |
/* 0x0d */ new CoerceToFile(), |
310 |
/* 0x0e */ new CoerceToFile(), |
311 |
/* 0x0f */ null, |
312 |
|
313 |
/* 0x10 */ null, |
314 |
/* 0x11 */ null, |
315 |
/* 0x12 */ null, |
316 |
/* 0x13 */ null, |
317 |
/* 0x14 */ null, |
318 |
/* 0x15 */ null, |
319 |
/* 0x16 */ null, |
320 |
/* 0x17 */ null, |
321 |
/* 0x18 */ null, |
322 |
/* 0x19 */ null, |
323 |
/* 0x1a */ null, |
324 |
/* 0x1b */ null, |
325 |
/* 0x1c */ null, |
326 |
/* 0x1d */ null, |
327 |
/* 0x1e */ null, |
328 |
/* 0x1f */ null, |
329 |
|
330 |
|
331 |
/* 0x20 */ null, |
332 |
/* 0x21 */ null, |
333 |
/* 0x22 */ null, |
334 |
/* 0x23 */ null, |
335 |
/* 0x24 */ null, |
336 |
/* 0x25 */ null, |
337 |
/* 0x26 */ null, |
338 |
/* 0x27 */ null, |
339 |
/* 0x28 */ null, |
340 |
/* 0x29 */ null, |
341 |
/* 0x30 */ null, |
342 |
/* 0x31 */ null, |
343 |
/* 0x32 */ null, |
344 |
/* 0x33 */ null, |
345 |
/* 0x34 */ null, |
346 |
/* 0x35 */ null, |
347 |
/* 0x36 */ null, |
348 |
/* 0x37 */ null, |
349 |
/* 0x38 */ null, |
350 |
/* 0x39 */ null, |
351 |
/* 0x40 */ null, |
352 |
}; |
353 |
|
354 |
|
355 |
} |