gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dataTypes / impl / DefaultDataTypesManager.java @ 2160
History | View | Annotate | Download (13.5 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 |
|
38 |
import org.gvsig.installer.lib.api.Version; |
39 |
import org.gvsig.tools.dataTypes.Coercion; |
40 |
import org.gvsig.tools.dataTypes.CoercionException; |
41 |
import org.gvsig.tools.dataTypes.DataType; |
42 |
import static org.gvsig.tools.dataTypes.DataType.BYTE_DEFAULT_PRECISION; |
43 |
import static org.gvsig.tools.dataTypes.DataType.BYTE_MAX_PRECISION; |
44 |
import static org.gvsig.tools.dataTypes.DataType.DECIMAL_DEFAULT_PRECISION; |
45 |
import static org.gvsig.tools.dataTypes.DataType.DECIMAL_DEFAULT_SCALE; |
46 |
import static org.gvsig.tools.dataTypes.DataType.DECIMAL_MAX_PRECISION; |
47 |
import static org.gvsig.tools.dataTypes.DataType.DOUBLE_DEFAULT_PRECISION; |
48 |
import static org.gvsig.tools.dataTypes.DataType.DOUBLE_DEFAULT_SCALE; |
49 |
import static org.gvsig.tools.dataTypes.DataType.DOUBLE_MAX_PRECISION; |
50 |
import static org.gvsig.tools.dataTypes.DataType.FLAG_FLOATING_POINT; |
51 |
import static org.gvsig.tools.dataTypes.DataType.FLAG_NUMBER; |
52 |
import static org.gvsig.tools.dataTypes.DataType.FLAG_PREDEFINED_PRECISION; |
53 |
import static org.gvsig.tools.dataTypes.DataType.FLAG_SUPPORT_PRECISION; |
54 |
import static org.gvsig.tools.dataTypes.DataType.FLAG_SUPPORT_SCALE; |
55 |
import static org.gvsig.tools.dataTypes.DataType.FLAG_SUPPORT_SIZE; |
56 |
import static org.gvsig.tools.dataTypes.DataType.FLOAT_DEFAULT_PRECISION; |
57 |
import static org.gvsig.tools.dataTypes.DataType.FLOAT_DEFAULT_SCALE; |
58 |
import static org.gvsig.tools.dataTypes.DataType.FLOAT_MAX_PRECISION; |
59 |
import static org.gvsig.tools.dataTypes.DataType.INT_DEFAULT_PRECISION; |
60 |
import static org.gvsig.tools.dataTypes.DataType.INT_MAX_PRECISION; |
61 |
import static org.gvsig.tools.dataTypes.DataType.LONG_DEFAULT_PRECISION; |
62 |
import static org.gvsig.tools.dataTypes.DataType.LONG_MAX_PRECISION; |
63 |
import static org.gvsig.tools.dataTypes.DataType.PRECISION_NONE; |
64 |
import static org.gvsig.tools.dataTypes.DataType.SCALE_NONE; |
65 |
import org.gvsig.tools.dataTypes.DataTypes; |
66 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
67 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDecimal; |
68 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToBoolean; |
69 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToByte; |
70 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToBytearray; |
71 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDate; |
72 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDouble; |
73 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToFile; |
74 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToFloat; |
75 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToInt; |
76 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToLocale; |
77 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToLong; |
78 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToObject; |
79 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToString; |
80 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToTime; |
81 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToTimestamp; |
82 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToURI; |
83 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToURL; |
84 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToVersion; |
85 |
import org.gvsig.tools.dynobject.DynObject; |
86 |
import org.slf4j.Logger; |
87 |
import org.slf4j.LoggerFactory; |
88 |
|
89 |
public class DefaultDataTypesManager implements DataTypesManager, DataTypes { |
90 |
|
91 |
private static final Logger LOG = LoggerFactory |
92 |
.getLogger(DefaultDataTypesManager.class); |
93 |
|
94 |
private static final int NEWINDEXES_AT = OBJECT + 1; |
95 |
|
96 |
private final DataType[] types = new DefaultDataType[MAX_TYPE_VALUE]; |
97 |
|
98 |
public DefaultDataTypesManager() {
|
99 |
|
100 |
this.addtype(BOOLEAN, null, "Boolean", Boolean.class, new CoerceToBoolean(), |
101 |
"datatype-boolean"
|
102 |
); |
103 |
this.addtype(BYTE, null, "Byte", Byte.class, new CoerceToByte(), |
104 |
"datatype-byte",
|
105 |
FLAG_NUMBER|FLAG_SUPPORT_PRECISION|FLAG_PREDEFINED_PRECISION, |
106 |
BYTE_MAX_PRECISION, BYTE_DEFAULT_PRECISION |
107 |
); |
108 |
this.addtype(CHAR, null, "Char", Character.class, new CoerceToString()); |
109 |
this.addtype(INT, null, "Integer", Integer.class, new CoerceToInt(), |
110 |
"datatype-integer",
|
111 |
FLAG_NUMBER|FLAG_SUPPORT_PRECISION|FLAG_PREDEFINED_PRECISION, |
112 |
INT_MAX_PRECISION, INT_DEFAULT_PRECISION |
113 |
); |
114 |
this.addtype(LONG, null, "Long", Long.class, new CoerceToLong(), |
115 |
"datatype-long",
|
116 |
FLAG_NUMBER|FLAG_SUPPORT_PRECISION|FLAG_PREDEFINED_PRECISION, |
117 |
LONG_MAX_PRECISION, LONG_DEFAULT_PRECISION |
118 |
); |
119 |
this.addtype(FLOAT, null, "Float", Float.class, new CoerceToFloat(), |
120 |
"datatype-float",
|
121 |
FLAG_FLOATING_POINT|FLAG_SUPPORT_PRECISION|FLAG_PREDEFINED_PRECISION|FLAG_SUPPORT_SCALE, |
122 |
FLOAT_MAX_PRECISION, FLOAT_DEFAULT_PRECISION, FLOAT_DEFAULT_SCALE |
123 |
); |
124 |
this.addtype(DOUBLE, null, "Double", Double.class, new CoerceToDouble(), |
125 |
"datatype-double",
|
126 |
FLAG_FLOATING_POINT|FLAG_SUPPORT_PRECISION|FLAG_PREDEFINED_PRECISION|FLAG_SUPPORT_SCALE, |
127 |
DOUBLE_MAX_PRECISION, DOUBLE_DEFAULT_PRECISION, DOUBLE_DEFAULT_SCALE |
128 |
); |
129 |
this.addtype(DECIMAL, null, "Decimal", BigDecimal.class, new CoerceToDecimal(), |
130 |
"datatype-decimal",
|
131 |
FLAG_NUMBER|FLAG_SUPPORT_PRECISION|FLAG_SUPPORT_SCALE, |
132 |
DECIMAL_MAX_PRECISION, DECIMAL_DEFAULT_PRECISION, DECIMAL_DEFAULT_SCALE |
133 |
); |
134 |
this.addtype(STRING, null, "String", String.class, new CoerceToString(), |
135 |
"datatype-string",
|
136 |
FLAG_SUPPORT_SIZE |
137 |
); |
138 |
this.addtype(DATE, SUBTYPE_DATE, "Date", Date.class, new CoerceToDate(), "datatype-date"); |
139 |
this.addtype(TIME, SUBTYPE_DATE, "Time", Date.class, new CoerceToTime(), "datatype-time"); |
140 |
this.addtype(TIMESTAMP, SUBTYPE_DATE, "Timestamp", Date.class, new CoerceToTimestamp(), "datatype-timestamp"); |
141 |
|
142 |
this.addtype(LOCALE, null, "Locale", Locale.class, new CoerceToLocale(), |
143 |
"datatype-locale"
|
144 |
); |
145 |
|
146 |
|
147 |
this.addtype(VERSION, null, "Version", Version.class, new CoerceToVersion()); |
148 |
|
149 |
this.addtype(BYTEARRAY, null, "ByteArray", |
150 |
(new byte[1]).getClass(), |
151 |
new CoerceToBytearray(),
|
152 |
"datatype-bytearray",
|
153 |
FLAG_SUPPORT_SIZE |
154 |
); |
155 |
this.addtype(FILE, SUBTYPE_FILE, "File", File.class, new CoerceToFile()); |
156 |
this.addtype(FOLDER, SUBTYPE_FOLDER, "Folder", File.class, new CoerceToFile()); |
157 |
this.addtype(DYNOBJECT, null, "DynObject", DynObject.class, null); |
158 |
this.addtype(URL, null, "URL", URL.class, new CoerceToURL()); |
159 |
this.addtype(URI, null, "URI", URI.class, new CoerceToURI()); |
160 |
|
161 |
this.addtype(ARRAY, null, "Array", (new Object[1]).getClass(), null); |
162 |
this.addtype(LIST, null, "List", List.class, null); |
163 |
this.addtype(SET, null, "Set", Set.class, null); |
164 |
this.addtype(MAP, null, "Map", Map.class, null); |
165 |
|
166 |
this.addtype(OBJECT, null, "Object", Object.class, new CoerceToObject(), "datatype-object"); |
167 |
} |
168 |
|
169 |
@Override
|
170 |
public final synchronized int addtype(int type, String subtype, String name, |
171 |
Class defaultClass, Coercion coercion) {
|
172 |
return addtype(type, subtype, name, defaultClass, coercion, null, 0, PRECISION_NONE, PRECISION_NONE, SCALE_NONE); |
173 |
} |
174 |
|
175 |
public final synchronized int addtype(int type, String subtype, String name, |
176 |
Class defaultClass, Coercion coercion, String iconName) { |
177 |
return addtype(type, subtype, name, defaultClass, coercion, iconName, 0, PRECISION_NONE, PRECISION_NONE, SCALE_NONE); |
178 |
} |
179 |
|
180 |
@Override
|
181 |
public final synchronized int addtype(int type, String subtype, String name, |
182 |
Class defaultClass, Coercion coercion, String iconName, int flags) { |
183 |
return addtype(type, subtype, name, defaultClass, coercion, iconName,
|
184 |
flags, PRECISION_NONE, PRECISION_NONE, SCALE_NONE |
185 |
); |
186 |
} |
187 |
|
188 |
public final synchronized int addtype(int type, String subtype, String name, |
189 |
Class defaultClass, Coercion coercion, String iconName, |
190 |
int flags, int max_precision, int default_precision) { |
191 |
return addtype(type, subtype, name, defaultClass, coercion, iconName,
|
192 |
flags, max_precision, default_precision, SCALE_NONE |
193 |
); |
194 |
} |
195 |
|
196 |
@Override
|
197 |
public final synchronized int addtype(int type, String subtype, String name, |
198 |
Class defaultClass, Coercion coercion, String iconName, |
199 |
int flags, int max_precision, int default_precision, int default_scale) { |
200 |
if (type == INVALID) {
|
201 |
type = getNewObjectIndexType(); |
202 |
} |
203 |
if (type < 1 || type > MAX_TYPE_VALUE) { |
204 |
throw new IllegalArgumentException("Wrong type " |
205 |
+ Integer.toHexString(type).toUpperCase() + "."); |
206 |
} |
207 |
|
208 |
try {
|
209 |
// Check if the type has been already registereds
|
210 |
get(type); |
211 |
throw new IllegalArgumentException("type " |
212 |
+ Integer.toHexString(type).toUpperCase() + ", name " + name |
213 |
+ ", already registered as " + getTypeName(type) + "."); |
214 |
} catch (IllegalArgumentException e) { |
215 |
// OK, the type is still not registered.
|
216 |
} |
217 |
|
218 |
DataType dataType = new DefaultDataType(
|
219 |
type, subtype, name, defaultClass, coercion, iconName, |
220 |
flags, max_precision, default_precision, default_scale |
221 |
); |
222 |
types[type] = dataType; |
223 |
LOG.info("Registered data type {}.", dataType.toString());
|
224 |
return type;
|
225 |
} |
226 |
|
227 |
private int getNewObjectIndexType() { |
228 |
for (int i = NEWINDEXES_AT; i <= MAX_TYPE_VALUE; i++) { |
229 |
if (types[i] == null) { |
230 |
return i;
|
231 |
} |
232 |
} |
233 |
return DataTypes.INVALID;
|
234 |
} |
235 |
|
236 |
@Override
|
237 |
public DataType get(int type) { |
238 |
DataType dataType = null;
|
239 |
if (type > 0 && type <= MAX_TYPE_VALUE) { |
240 |
dataType = types[type]; |
241 |
} |
242 |
|
243 |
if (dataType == null) { |
244 |
throw new IllegalArgumentException("DataType " + type |
245 |
+ " not registered");
|
246 |
} |
247 |
|
248 |
return dataType;
|
249 |
} |
250 |
|
251 |
@Override
|
252 |
public boolean isValidType(int type) { |
253 |
return type > 0 && type <= MAX_TYPE_VALUE && this.types[type] != null; |
254 |
} |
255 |
|
256 |
@Override
|
257 |
public boolean isObject(int type) { |
258 |
return (type & DataTypes.OBJECT) == DataTypes.OBJECT;
|
259 |
} |
260 |
|
261 |
@Override
|
262 |
public boolean isContainer(int type) { |
263 |
return (type & DataTypes.CONTAINER) == DataTypes.CONTAINER;
|
264 |
} |
265 |
|
266 |
@Override
|
267 |
public int getType(String name) { |
268 |
if (name != null) { |
269 |
for (int i = 0; i < types.length; i++) { |
270 |
if (types[i] != null |
271 |
&& name.equalsIgnoreCase(types[i].getName())) { |
272 |
return i;
|
273 |
} |
274 |
} |
275 |
} |
276 |
return DataTypes.INVALID;
|
277 |
} |
278 |
|
279 |
@Override
|
280 |
public String getTypeName(int type) { |
281 |
DataType dataType = get(type); |
282 |
String name = dataType.getName();
|
283 |
if (name == null) { |
284 |
return "unknow"; |
285 |
} |
286 |
return name;
|
287 |
} |
288 |
|
289 |
@Override
|
290 |
public Class getDefaultClass(int type) { |
291 |
DataType dataType = get(type); |
292 |
return dataType.getDefaultClass();
|
293 |
} |
294 |
|
295 |
@Override
|
296 |
public String getSubtype(int type) { |
297 |
DataType dataType = get(type); |
298 |
return dataType.getSubtype();
|
299 |
} |
300 |
|
301 |
@Override
|
302 |
public Coercion getCoercion(int type) { |
303 |
DataType dataType = get(type); |
304 |
return dataType.getCoercion();
|
305 |
} |
306 |
|
307 |
// @Override
|
308 |
// public void setCoercion(int type, Coercion coercion) {
|
309 |
// DataType dataType = get(type);
|
310 |
// dataType.setCoercion(coercion);
|
311 |
// }
|
312 |
@Override
|
313 |
public void addCoercion(int type, Coercion coercion) { |
314 |
DataType dataType = get(type); |
315 |
dataType.addCoercion(coercion); |
316 |
} |
317 |
|
318 |
@Override
|
319 |
public Object coerce(int type, Object value) throws CoercionException { |
320 |
DataType dataType = get(type); |
321 |
return dataType.coerce(value);
|
322 |
} |
323 |
|
324 |
@Override
|
325 |
public Iterator iterator() { |
326 |
List list = new ArrayList(); |
327 |
DataType type; |
328 |
for (int i = 0; i < this.types.length; i++) { |
329 |
type = this.types[i];
|
330 |
if (type != null) { |
331 |
list.add(this.types[i]);
|
332 |
} |
333 |
} |
334 |
return list.iterator();
|
335 |
} |
336 |
|
337 |
@Override
|
338 |
public DataType getDataType(Class defaultClass) { |
339 |
// First look for a class with exact match
|
340 |
for (DataType type : types) {
|
341 |
if (type != null) { |
342 |
Class typeClass = type.getDefaultClass();
|
343 |
if (typeClass != null && typeClass.equals(defaultClass)) { |
344 |
return type;
|
345 |
} |
346 |
} |
347 |
} |
348 |
// Then look for a class which is an interface or parent
|
349 |
for (DataType type : types) {
|
350 |
if (type != null) { |
351 |
Class typeClass = type.getDefaultClass();
|
352 |
if (typeClass != null |
353 |
&& typeClass.isAssignableFrom(defaultClass)) { |
354 |
return type;
|
355 |
} |
356 |
} |
357 |
} |
358 |
throw new IllegalArgumentException( |
359 |
"There is not any registered data type with the class or a "
|
360 |
+ "parent of the class: " + defaultClass);
|
361 |
} |
362 |
|
363 |
} |