gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dataTypes / impl / DefaultDataTypesManager.java @ 2332
History | View | Annotate | Download (14.2 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.tools.dataTypes.impl; |
25 |
|
26 |
import java.io.File; |
27 |
import java.math.BigDecimal; |
28 |
import java.net.URI; |
29 |
import java.net.URL; |
30 |
import java.util.ArrayList; |
31 |
import java.util.Date; |
32 |
import java.util.Iterator; |
33 |
import java.util.List; |
34 |
import java.util.Locale; |
35 |
import java.util.Map; |
36 |
import java.util.Set; |
37 |
import org.apache.commons.lang3.StringUtils; |
38 |
|
39 |
import org.gvsig.installer.lib.api.Version; |
40 |
import org.gvsig.tools.dataTypes.Coercion; |
41 |
import org.gvsig.tools.dataTypes.CoercionException; |
42 |
import org.gvsig.tools.dataTypes.DataType; |
43 |
import static org.gvsig.tools.dataTypes.DataType.BYTE_DEFAULT_PRECISION; |
44 |
import static org.gvsig.tools.dataTypes.DataType.BYTE_MAX_PRECISION; |
45 |
import static org.gvsig.tools.dataTypes.DataType.DECIMAL_DEFAULT_PRECISION; |
46 |
import static org.gvsig.tools.dataTypes.DataType.DECIMAL_DEFAULT_SCALE; |
47 |
import static org.gvsig.tools.dataTypes.DataType.DECIMAL_MAX_PRECISION; |
48 |
import static org.gvsig.tools.dataTypes.DataType.DOUBLE_DEFAULT_PRECISION; |
49 |
import static org.gvsig.tools.dataTypes.DataType.DOUBLE_DEFAULT_SCALE; |
50 |
import static org.gvsig.tools.dataTypes.DataType.DOUBLE_MAX_PRECISION; |
51 |
import static org.gvsig.tools.dataTypes.DataType.FLAG_FLOATING_POINT; |
52 |
import static org.gvsig.tools.dataTypes.DataType.FLAG_NUMBER; |
53 |
import static org.gvsig.tools.dataTypes.DataType.FLAG_PREDEFINED_PRECISION; |
54 |
import static org.gvsig.tools.dataTypes.DataType.FLAG_SUPPORT_PRECISION; |
55 |
import static org.gvsig.tools.dataTypes.DataType.FLAG_SUPPORT_SCALE; |
56 |
import static org.gvsig.tools.dataTypes.DataType.FLAG_SUPPORT_SIZE; |
57 |
import static org.gvsig.tools.dataTypes.DataType.FLOAT_DEFAULT_PRECISION; |
58 |
import static org.gvsig.tools.dataTypes.DataType.FLOAT_DEFAULT_SCALE; |
59 |
import static org.gvsig.tools.dataTypes.DataType.FLOAT_MAX_PRECISION; |
60 |
import static org.gvsig.tools.dataTypes.DataType.INT_DEFAULT_PRECISION; |
61 |
import static org.gvsig.tools.dataTypes.DataType.INT_MAX_PRECISION; |
62 |
import static org.gvsig.tools.dataTypes.DataType.LONG_DEFAULT_PRECISION; |
63 |
import static org.gvsig.tools.dataTypes.DataType.LONG_MAX_PRECISION; |
64 |
import static org.gvsig.tools.dataTypes.DataType.PRECISION_NONE; |
65 |
import static org.gvsig.tools.dataTypes.DataType.SCALE_NONE; |
66 |
import static org.gvsig.tools.dataTypes.DataType.STRING_DEFAULT_SIZE; |
67 |
import org.gvsig.tools.dataTypes.DataTypes; |
68 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
69 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDecimal; |
70 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToBoolean; |
71 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToByte; |
72 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToBytearray; |
73 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDate; |
74 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDouble; |
75 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToFile; |
76 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToFloat; |
77 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToInt; |
78 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToLocale; |
79 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToLong; |
80 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToObject; |
81 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToString; |
82 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToTime; |
83 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToTimestamp; |
84 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToURI; |
85 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToURL; |
86 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToVersion; |
87 |
import org.gvsig.tools.dynobject.DynObject; |
88 |
import org.slf4j.Logger; |
89 |
import org.slf4j.LoggerFactory; |
90 |
|
91 |
public class DefaultDataTypesManager implements DataTypesManager, DataTypes { |
92 |
|
93 |
private static final Logger LOG = LoggerFactory |
94 |
.getLogger(DefaultDataTypesManager.class); |
95 |
|
96 |
private static final int NEWINDEXES_AT = OBJECT + 1; |
97 |
|
98 |
private final DataType[] types = new DefaultDataType[MAX_TYPE_VALUE]; |
99 |
|
100 |
public DefaultDataTypesManager() {
|
101 |
|
102 |
this.addtype(BOOLEAN, null, DataTypes.BOOLEAN_NAME, Boolean.class, new CoerceToBoolean(), |
103 |
"datatype-boolean"
|
104 |
); |
105 |
this.addtype(BYTE, null, DataTypes.BYTE_NAME, Byte.class, new CoerceToByte(), |
106 |
"datatype-byte",
|
107 |
FLAG_NUMBER|FLAG_SUPPORT_PRECISION|FLAG_PREDEFINED_PRECISION, |
108 |
BYTE_MAX_PRECISION, BYTE_DEFAULT_PRECISION |
109 |
); |
110 |
this.addtype(CHAR, null, DataTypes.CHAR_NAME, Character.class, new CoerceToString()); |
111 |
this.addtype(INT, null, "Integer", Integer.class, new CoerceToInt(), |
112 |
"datatype-integer",
|
113 |
FLAG_NUMBER|FLAG_SUPPORT_PRECISION|FLAG_PREDEFINED_PRECISION, |
114 |
INT_MAX_PRECISION, INT_DEFAULT_PRECISION |
115 |
); |
116 |
this.addtype(LONG, null, DataTypes.LONG_NAME, Long.class, new CoerceToLong(), |
117 |
"datatype-long",
|
118 |
FLAG_NUMBER|FLAG_SUPPORT_PRECISION|FLAG_PREDEFINED_PRECISION, |
119 |
LONG_MAX_PRECISION, LONG_DEFAULT_PRECISION |
120 |
); |
121 |
this.addtype(FLOAT, null, DataTypes.FLOAT_NAME, Float.class, new CoerceToFloat(), |
122 |
"datatype-float",
|
123 |
FLAG_FLOATING_POINT|FLAG_SUPPORT_PRECISION|FLAG_PREDEFINED_PRECISION|FLAG_SUPPORT_SCALE, |
124 |
FLOAT_MAX_PRECISION, FLOAT_DEFAULT_PRECISION, FLOAT_DEFAULT_SCALE |
125 |
); |
126 |
this.addtype(DOUBLE, null, DataTypes.DOUBLE_NAME, Double.class, new CoerceToDouble(), |
127 |
"datatype-double",
|
128 |
FLAG_FLOATING_POINT|FLAG_SUPPORT_PRECISION|FLAG_PREDEFINED_PRECISION|FLAG_SUPPORT_SCALE, |
129 |
DOUBLE_MAX_PRECISION, DOUBLE_DEFAULT_PRECISION, DOUBLE_DEFAULT_SCALE |
130 |
); |
131 |
this.addtype(DECIMAL, null, DataTypes.DECIMAL_NAME, BigDecimal.class, new CoerceToDecimal(), |
132 |
"datatype-decimal",
|
133 |
FLAG_NUMBER|FLAG_SUPPORT_PRECISION|FLAG_SUPPORT_SCALE, |
134 |
DECIMAL_MAX_PRECISION, DECIMAL_DEFAULT_PRECISION, DECIMAL_DEFAULT_SCALE |
135 |
); |
136 |
this.addtype(STRING, null, DataTypes.STRING_NAME, String.class, new CoerceToString(), |
137 |
"datatype-string",
|
138 |
FLAG_SUPPORT_SIZE |
139 |
).setDefaultSize(STRING_DEFAULT_SIZE); |
140 |
this.addtype(DATE, SUBTYPE_DATE, DataTypes.DATE_NAME, java.sql.Date.class, new CoerceToDate(), "datatype-date"); |
141 |
this.addtype(TIME, SUBTYPE_DATE, DataTypes.TIME_NAME, java.sql.Time.class, new CoerceToTime(), "datatype-time"); |
142 |
this.addtype(TIMESTAMP, SUBTYPE_DATE, DataTypes.TIMESTAMP_NAME, java.sql.Timestamp.class, new CoerceToTimestamp(), "datatype-timestamp"); |
143 |
|
144 |
this.addtype(LOCALE, null, DataTypes.LOCALE_NAME, Locale.class, new CoerceToLocale(), |
145 |
"datatype-locale"
|
146 |
); |
147 |
|
148 |
|
149 |
this.addtype(VERSION, null, DataTypes.VERSION_NAME, Version.class, new CoerceToVersion()); |
150 |
|
151 |
this.addtype(BYTEARRAY, null, DataTypes.BYTEARRAY_NAME, |
152 |
(new byte[1]).getClass(), |
153 |
new CoerceToBytearray(),
|
154 |
"datatype-bytearray",
|
155 |
FLAG_SUPPORT_SIZE |
156 |
); |
157 |
this.addtype(FILE, SUBTYPE_FILE, DataTypes.FILE_NAME, File.class, new CoerceToFile()); |
158 |
this.addtype(FOLDER, SUBTYPE_FOLDER, DataTypes.FOLDER_NAME, File.class, new CoerceToFile()); |
159 |
this.addtype(DYNOBJECT, null, DataTypes.DYNOBJECT_NAME, DynObject.class, null); |
160 |
this.addtype(URL, null, DataTypes.URL_NAME, URL.class, new CoerceToURL()); |
161 |
this.addtype(URI, null, DataTypes.URI_NAME, URI.class, new CoerceToURI()); |
162 |
|
163 |
this.addtype(ARRAY, null, DataTypes.ARRAY_NAME, (new Object[1]).getClass(), null); |
164 |
this.addtype(LIST, null, DataTypes.LIST_NAME, List.class, null); |
165 |
this.addtype(SET, null, DataTypes.SET_NAME, Set.class, null); |
166 |
this.addtype(MAP, null, DataTypes.MAP_NAME, Map.class, null); |
167 |
|
168 |
this.addtype(OBJECT, null, DataTypes.OBJECT_NAME, Object.class, new CoerceToObject(), "datatype-object"); |
169 |
} |
170 |
|
171 |
@Override
|
172 |
public final synchronized DataType addtype(int type, String subtype, String name, |
173 |
Class defaultClass, Coercion coercion) {
|
174 |
return addtype(type, subtype, name, defaultClass, coercion, null, 0, PRECISION_NONE, PRECISION_NONE, SCALE_NONE); |
175 |
} |
176 |
|
177 |
public final synchronized DataType addtype(int type, String subtype, String name, |
178 |
Class defaultClass, Coercion coercion, String iconName) { |
179 |
return addtype(type, subtype, name, defaultClass, coercion, iconName, 0, PRECISION_NONE, PRECISION_NONE, SCALE_NONE); |
180 |
} |
181 |
|
182 |
@Override
|
183 |
public final synchronized DataType addtype(int type, String subtype, String name, |
184 |
Class defaultClass, Coercion coercion, String iconName, int flags) { |
185 |
return addtype(type, subtype, name, defaultClass, coercion, iconName,
|
186 |
flags, PRECISION_NONE, PRECISION_NONE, SCALE_NONE |
187 |
); |
188 |
} |
189 |
|
190 |
public final synchronized DataType addtype(int type, String subtype, String name, |
191 |
Class defaultClass, Coercion coercion, String iconName, |
192 |
int flags, int max_precision, int default_precision) { |
193 |
return addtype(type, subtype, name, defaultClass, coercion, iconName,
|
194 |
flags, max_precision, default_precision, SCALE_NONE |
195 |
); |
196 |
} |
197 |
|
198 |
@Override
|
199 |
public final synchronized DataType addtype(int type, String subtype, String name, |
200 |
Class defaultClass, Coercion coercion, String iconName, |
201 |
int flags, int max_precision, int default_precision, int default_scale) { |
202 |
if (type == INVALID) {
|
203 |
type = getNewObjectIndexType(); |
204 |
} |
205 |
if (type < 1 || type > MAX_TYPE_VALUE) { |
206 |
throw new IllegalArgumentException("Wrong type " |
207 |
+ Integer.toHexString(type).toUpperCase() + "."); |
208 |
} |
209 |
|
210 |
try {
|
211 |
// Check if the type has been already registereds
|
212 |
get(type); |
213 |
throw new IllegalArgumentException("type " |
214 |
+ Integer.toHexString(type).toUpperCase() + ", name " + name |
215 |
+ ", already registered as " + getTypeName(type) + "."); |
216 |
} catch (IllegalArgumentException e) { |
217 |
// OK, the type is still not registered.
|
218 |
} |
219 |
|
220 |
DataType dataType = new DefaultDataType(
|
221 |
type, subtype, name, defaultClass, coercion, iconName, |
222 |
flags, max_precision, default_precision, default_scale |
223 |
); |
224 |
types[type] = dataType; |
225 |
LOG.info("Registered data type {}.", dataType.toString());
|
226 |
return dataType;
|
227 |
} |
228 |
|
229 |
private int getNewObjectIndexType() { |
230 |
for (int i = NEWINDEXES_AT; i <= MAX_TYPE_VALUE; i++) { |
231 |
if (types[i] == null) { |
232 |
return i;
|
233 |
} |
234 |
} |
235 |
return DataTypes.INVALID;
|
236 |
} |
237 |
|
238 |
@Override
|
239 |
public DataType get(String typename) { |
240 |
for (DataType type : this.types) { |
241 |
if( type!=null && StringUtils.equalsIgnoreCase(typename, type.getName()) ) { |
242 |
return type;
|
243 |
} |
244 |
} |
245 |
return null; |
246 |
} |
247 |
|
248 |
@Override
|
249 |
public DataType get(int type) { |
250 |
DataType dataType = null;
|
251 |
if (type > 0 && type <= MAX_TYPE_VALUE) { |
252 |
dataType = types[type]; |
253 |
} |
254 |
|
255 |
if (dataType == null) { |
256 |
throw new IllegalArgumentException("DataType " + type |
257 |
+ " not registered");
|
258 |
} |
259 |
|
260 |
return dataType;
|
261 |
} |
262 |
|
263 |
@Override
|
264 |
public boolean isValidType(int type) { |
265 |
return type > 0 && type <= MAX_TYPE_VALUE && this.types[type] != null; |
266 |
} |
267 |
|
268 |
@Override
|
269 |
public boolean isObject(int type) { |
270 |
return (type & DataTypes.OBJECT) == DataTypes.OBJECT;
|
271 |
} |
272 |
|
273 |
@Override
|
274 |
public boolean isContainer(int type) { |
275 |
return (type & DataTypes.CONTAINER) == DataTypes.CONTAINER;
|
276 |
} |
277 |
|
278 |
@Override
|
279 |
public int getType(String name) { |
280 |
if (name != null) { |
281 |
for (int i = 0; i < types.length; i++) { |
282 |
if (types[i] != null |
283 |
&& name.equalsIgnoreCase(types[i].getName())) { |
284 |
return i;
|
285 |
} |
286 |
} |
287 |
} |
288 |
return DataTypes.INVALID;
|
289 |
} |
290 |
|
291 |
@Override
|
292 |
public String getTypeName(int type) { |
293 |
DataType dataType = get(type); |
294 |
String name = dataType.getName();
|
295 |
if (name == null) { |
296 |
return "unknow"; |
297 |
} |
298 |
return name;
|
299 |
} |
300 |
|
301 |
@Override
|
302 |
public Class getDefaultClass(int type) { |
303 |
DataType dataType = get(type); |
304 |
return dataType.getDefaultClass();
|
305 |
} |
306 |
|
307 |
@Override
|
308 |
public String getSubtype(int type) { |
309 |
DataType dataType = get(type); |
310 |
return dataType.getSubtype();
|
311 |
} |
312 |
|
313 |
@Override
|
314 |
public Coercion getCoercion(int type) { |
315 |
DataType dataType = get(type); |
316 |
return dataType.getCoercion();
|
317 |
} |
318 |
|
319 |
// @Override
|
320 |
// public void setCoercion(int type, Coercion coercion) {
|
321 |
// DataType dataType = get(type);
|
322 |
// dataType.setCoercion(coercion);
|
323 |
// }
|
324 |
@Override
|
325 |
public void addCoercion(int type, Coercion coercion) { |
326 |
DataType dataType = get(type); |
327 |
dataType.addCoercion(coercion); |
328 |
} |
329 |
|
330 |
@Override
|
331 |
public Object coerce(int type, Object value) throws CoercionException { |
332 |
DataType dataType = get(type); |
333 |
return dataType.coerce(value);
|
334 |
} |
335 |
|
336 |
@Override
|
337 |
public Iterator iterator() { |
338 |
List list = new ArrayList(); |
339 |
DataType type; |
340 |
for (int i = 0; i < this.types.length; i++) { |
341 |
type = this.types[i];
|
342 |
if (type != null) { |
343 |
list.add(this.types[i]);
|
344 |
} |
345 |
} |
346 |
return list.iterator();
|
347 |
} |
348 |
|
349 |
@Override
|
350 |
public DataType getDataType(Class defaultClass) { |
351 |
// First look for a class with exact match
|
352 |
for (DataType type : types) {
|
353 |
if (type != null) { |
354 |
Class typeClass = type.getDefaultClass();
|
355 |
if (typeClass != null && typeClass.equals(defaultClass)) { |
356 |
return type;
|
357 |
} |
358 |
} |
359 |
} |
360 |
// Then look for a class which is an interface or parent
|
361 |
for (DataType type : types) {
|
362 |
if (type != null) { |
363 |
Class typeClass = type.getDefaultClass();
|
364 |
if (typeClass != null |
365 |
&& typeClass.isAssignableFrom(defaultClass)) { |
366 |
return type;
|
367 |
} |
368 |
} |
369 |
} |
370 |
throw new IllegalArgumentException( |
371 |
"There is not any registered data type with the class or a "
|
372 |
+ "parent of the class: " + defaultClass);
|
373 |
} |
374 |
|
375 |
} |