gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dataTypes / impl / DefaultDataTypesManager.java @ 2080
History | View | Annotate | Download (10.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.Map; |
35 |
import java.util.Set; |
36 |
|
37 |
import org.gvsig.installer.lib.api.Version; |
38 |
import org.gvsig.tools.dataTypes.Coercion; |
39 |
import org.gvsig.tools.dataTypes.CoercionException; |
40 |
import org.gvsig.tools.dataTypes.DataType; |
41 |
import org.gvsig.tools.dataTypes.DataTypes; |
42 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
43 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDecimal; |
44 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToBoolean; |
45 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToByte; |
46 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToBytearray; |
47 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDate; |
48 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDateTime; |
49 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDouble; |
50 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToFile; |
51 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToFloat; |
52 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToInt; |
53 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToLong; |
54 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToObject; |
55 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToString; |
56 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToTime; |
57 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToURI; |
58 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToURL; |
59 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToVersion; |
60 |
import org.gvsig.tools.dynobject.DynObject; |
61 |
import org.slf4j.Logger; |
62 |
import org.slf4j.LoggerFactory; |
63 |
|
64 |
public class DefaultDataTypesManager implements DataTypesManager, DataTypes { |
65 |
|
66 |
private static final Logger LOG = LoggerFactory |
67 |
.getLogger(DefaultDataTypesManager.class); |
68 |
|
69 |
private static final int NEWINDEXES_AT = OBJECT + 1; |
70 |
|
71 |
private final DataType[] types = new DefaultDataType[MAX_TYPE_VALUE]; |
72 |
|
73 |
public DefaultDataTypesManager() {
|
74 |
|
75 |
this.addtype(BOOLEAN, null, "Boolean", Boolean.class, |
76 |
new CoerceToBoolean(), "datatype-boolean"); |
77 |
this.addtype(BYTE, null, "Byte", Byte.class, new CoerceToByte()); |
78 |
this.addtype(CHAR, null, "Char", Character.class, new CoerceToString()); |
79 |
this.addtype(INT, null, "Integer", Integer.class, new CoerceToInt(), "datatype-integer"); |
80 |
this.addtype(LONG, null, "Long", Long.class, new CoerceToLong(),"datatype-long"); |
81 |
this.addtype(FLOAT, null, "Float", Float.class, new CoerceToFloat(),"datatype-float"); |
82 |
this.addtype(DOUBLE, null, "Double", Double.class, new CoerceToDouble(),"datatype-double"); |
83 |
this.addtype(DECIMAL, null, "Decimal", BigDecimal.class, new CoerceToDecimal()); |
84 |
this.addtype(STRING, null, "String", String.class, new CoerceToString(), "datatype-string"); |
85 |
this.addtype(DATE, SUBTYPE_DATE, "Date", Date.class, new CoerceToDate(),"datatype-date"); |
86 |
this.addtype(TIME, SUBTYPE_DATE, "Time", Date.class, new CoerceToTime(),"datatype-time"); |
87 |
// this.addtype(TIMESTAMP, null, "Timestamp", Timestamp.class, new CoerceToTimestamp(),"datatype-timestamp");
|
88 |
this.addtype(TIMESTAMP, null, "Timestamp", Date.class, new CoerceToDateTime(),"datatype-timestamp"); |
89 |
this.addtype(VERSION, null, "Version", Version.class, new CoerceToVersion()); |
90 |
|
91 |
this.addtype(BYTEARRAY, null, "ByteArray", |
92 |
(new byte[1]).getClass(), |
93 |
new CoerceToBytearray(),
|
94 |
"datatype-bytearray"
|
95 |
); |
96 |
this.addtype(FILE, SUBTYPE_FILE, "File", File.class, new CoerceToFile()); |
97 |
this.addtype(FOLDER, SUBTYPE_FOLDER, "Folder", File.class, new CoerceToFile()); |
98 |
this.addtype(DYNOBJECT, null, "DynObject", DynObject.class, null); |
99 |
this.addtype(URL, null, "URL", URL.class, new CoerceToURL()); |
100 |
this.addtype(URI, null, "URI", URI.class, new CoerceToURI()); |
101 |
|
102 |
this.addtype(ARRAY, null, "Array", (new Object[1]).getClass(), null); |
103 |
this.addtype(LIST, null, "List", List.class, null); |
104 |
this.addtype(SET, null, "Set", Set.class, null); |
105 |
this.addtype(MAP, null, "Map", Map.class, null); |
106 |
|
107 |
this.addtype(OBJECT, null, "Object", Object.class, new CoerceToObject(),"datatype-object"); |
108 |
} |
109 |
|
110 |
@Override
|
111 |
public final synchronized int addtype(int type, String subtype, String name, |
112 |
Class defaultClass, Coercion coercion) {
|
113 |
return addtype(type, subtype, name, defaultClass, coercion, null); |
114 |
} |
115 |
|
116 |
public final synchronized int addtype(int type, String subtype, String name, |
117 |
Class defaultClass, Coercion coercion, String iconName) { |
118 |
if (type == INVALID) {
|
119 |
type = getNewObjectIndexType(); |
120 |
} |
121 |
if (type < 1 || type > MAX_TYPE_VALUE) { |
122 |
throw new IllegalArgumentException("Wrong type " |
123 |
+ Integer.toHexString(type).toUpperCase() + "."); |
124 |
} |
125 |
|
126 |
try {
|
127 |
// Check if the type has been already registereds
|
128 |
get(type); |
129 |
throw new IllegalArgumentException("type " |
130 |
+ Integer.toHexString(type).toUpperCase() + ", name " + name |
131 |
+ ", already registered as " + getTypeName(type) + "."); |
132 |
} catch (IllegalArgumentException e) { |
133 |
// OK, the type is still not registered.
|
134 |
} |
135 |
|
136 |
DataType dataType = |
137 |
new DefaultDataType(type, subtype, name, defaultClass, coercion, iconName);
|
138 |
types[type] = dataType; |
139 |
LOG.info("Registered data type {}.", dataType.toString());
|
140 |
return type;
|
141 |
} |
142 |
|
143 |
private int getNewObjectIndexType() { |
144 |
for (int i = NEWINDEXES_AT; i <= MAX_TYPE_VALUE; i++) { |
145 |
if (types[i] == null) { |
146 |
return i;
|
147 |
} |
148 |
} |
149 |
return DataTypes.INVALID;
|
150 |
} |
151 |
|
152 |
@Override
|
153 |
public DataType get(int type) { |
154 |
DataType dataType = null;
|
155 |
if (type > 0 && type <= MAX_TYPE_VALUE) { |
156 |
dataType = types[type]; |
157 |
} |
158 |
|
159 |
if (dataType == null) { |
160 |
throw new IllegalArgumentException("DataType " + type |
161 |
+ " not registered");
|
162 |
} |
163 |
|
164 |
return dataType;
|
165 |
} |
166 |
|
167 |
@Override
|
168 |
public boolean isValidType(int type) { |
169 |
return type > 0 && type <= MAX_TYPE_VALUE && this.types[type] != null; |
170 |
} |
171 |
|
172 |
@Override
|
173 |
public boolean isObject(int type) { |
174 |
return (type & DataTypes.OBJECT) == DataTypes.OBJECT;
|
175 |
} |
176 |
|
177 |
@Override
|
178 |
public boolean isContainer(int type) { |
179 |
return (type & DataTypes.CONTAINER) == DataTypes.CONTAINER;
|
180 |
} |
181 |
|
182 |
@Override
|
183 |
public int getType(String name) { |
184 |
if (name != null) { |
185 |
for (int i = 0; i < types.length; i++) { |
186 |
if (types[i] != null |
187 |
&& name.equalsIgnoreCase(types[i].getName())) { |
188 |
return i;
|
189 |
} |
190 |
} |
191 |
} |
192 |
return DataTypes.INVALID;
|
193 |
} |
194 |
|
195 |
@Override
|
196 |
public String getTypeName(int type) { |
197 |
DataType dataType = get(type); |
198 |
String name = dataType.getName();
|
199 |
if (name == null) { |
200 |
return "unknow"; |
201 |
} |
202 |
return name;
|
203 |
} |
204 |
|
205 |
@Override
|
206 |
public Class getDefaultClass(int type) { |
207 |
DataType dataType = get(type); |
208 |
return dataType.getDefaultClass();
|
209 |
} |
210 |
|
211 |
@Override
|
212 |
public String getSubtype(int type) { |
213 |
DataType dataType = get(type); |
214 |
return dataType.getSubtype();
|
215 |
} |
216 |
|
217 |
@Override
|
218 |
public Coercion getCoercion(int type) { |
219 |
DataType dataType = get(type); |
220 |
return dataType.getCoercion();
|
221 |
} |
222 |
|
223 |
// @Override
|
224 |
// public void setCoercion(int type, Coercion coercion) {
|
225 |
// DataType dataType = get(type);
|
226 |
// dataType.setCoercion(coercion);
|
227 |
// }
|
228 |
|
229 |
@Override
|
230 |
public void addCoercion(int type, Coercion coercion) { |
231 |
DataType dataType = get(type); |
232 |
dataType.addCoercion(coercion); |
233 |
} |
234 |
|
235 |
@Override
|
236 |
public Object coerce(int type, Object value) throws CoercionException { |
237 |
DataType dataType = get(type); |
238 |
return dataType.coerce(value);
|
239 |
} |
240 |
|
241 |
@Override
|
242 |
public Iterator iterator() { |
243 |
List list = new ArrayList(); |
244 |
DataType type; |
245 |
for (int i = 0; i < this.types.length; i++) { |
246 |
type = this.types[i];
|
247 |
if (type != null) |
248 |
list.add(this.types[i]);
|
249 |
} |
250 |
return list.iterator();
|
251 |
} |
252 |
|
253 |
@Override
|
254 |
public DataType getDataType(Class defaultClass) { |
255 |
// First look for a class with exact match
|
256 |
for (int i = 0; i < types.length; i++) { |
257 |
if (types[i] != null) { |
258 |
Class typeClass = types[i].getDefaultClass();
|
259 |
if (typeClass != null && typeClass.equals(defaultClass)) { |
260 |
return types[i];
|
261 |
} |
262 |
} |
263 |
} |
264 |
// Then look for a class which is an interface or parent
|
265 |
for (int i = 0; i < types.length; i++) { |
266 |
if (types[i] != null) { |
267 |
Class typeClass = types[i].getDefaultClass();
|
268 |
if (typeClass != null |
269 |
&& typeClass.isAssignableFrom(defaultClass)) { |
270 |
return types[i];
|
271 |
} |
272 |
} |
273 |
} |
274 |
throw new IllegalArgumentException( |
275 |
"There is not any registered data type with the class or a "
|
276 |
+ "parent of the class: " + defaultClass);
|
277 |
} |
278 |
|
279 |
} |