gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dataTypes / impl / DefaultDataTypesManager.java @ 653
History | View | Annotate | Download (7.71 KB)
1 |
package org.gvsig.tools.dataTypes.impl; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.net.URI; |
5 |
import java.net.URL; |
6 |
import java.util.ArrayList; |
7 |
import java.util.Date; |
8 |
import java.util.Iterator; |
9 |
import java.util.List; |
10 |
import java.util.Map; |
11 |
import java.util.Set; |
12 |
|
13 |
import org.slf4j.Logger; |
14 |
import org.slf4j.LoggerFactory; |
15 |
|
16 |
import org.gvsig.tools.dataTypes.CoercionException; |
17 |
import org.gvsig.tools.dataTypes.DataType; |
18 |
import org.gvsig.tools.dataTypes.DataTypes; |
19 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
20 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToBoolean; |
21 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToByte; |
22 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDate; |
23 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDateTime; |
24 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDouble; |
25 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToFile; |
26 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToFloat; |
27 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToInt; |
28 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToLong; |
29 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToString; |
30 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToTime; |
31 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToURI; |
32 |
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToURL; |
33 |
import org.gvsig.tools.dynobject.DynObject; |
34 |
|
35 |
public class DefaultDataTypesManager implements DataTypesManager, DataTypes { |
36 |
|
37 |
private static final Logger LOG = LoggerFactory |
38 |
.getLogger(DefaultDataTypesManager.class); |
39 |
|
40 |
private static final int NEWINDEXES_AT = OBJECT + 1; |
41 |
|
42 |
private DataType[] types = new DefaultDataType[MAX_TYPE_VALUE]; |
43 |
|
44 |
public DefaultDataTypesManager() {
|
45 |
|
46 |
this.addtype(BOOLEAN, null, "Boolean", Boolean.class, |
47 |
new CoerceToBoolean());
|
48 |
this.addtype(BYTE, null, "Byte", Byte.class, new CoerceToByte()); |
49 |
this.addtype(CHAR, null, "Char", Character.class, new CoerceToString()); |
50 |
this.addtype(INT, null, "Integer", Integer.class, new CoerceToInt()); |
51 |
this.addtype(LONG, null, "Long", Long.class, new CoerceToLong()); |
52 |
this.addtype(FLOAT, null, "Float", Float.class, new CoerceToFloat()); |
53 |
this.addtype(DOUBLE, null, "Double", Double.class, new CoerceToDouble()); |
54 |
this.addtype(STRING, null, "String", String.class, new CoerceToString()); |
55 |
this.addtype(DATE, SUBTYPE_DATE, "Date", Date.class, new CoerceToDate()); |
56 |
this.addtype(TIME, SUBTYPE_DATE, "Time", Date.class, new CoerceToTime()); |
57 |
this.addtype(TIMESTAMP, SUBTYPE_DATE, "Timestamp", Date.class, |
58 |
new CoerceToDateTime());
|
59 |
|
60 |
this.addtype(BYTEARRAY, null, "ByteArray", null, null); |
61 |
this.addtype(FILE, SUBTYPE_FILE, "File", File.class, new CoerceToFile()); |
62 |
this.addtype(FOLDER, SUBTYPE_FOLDER, "Folder", File.class, |
63 |
new CoerceToFile());
|
64 |
this.addtype(DYNOBJECT, null, "DynObject", DynObject.class, null); |
65 |
this.addtype(URL, null, "URL", URL.class, new CoerceToURL()); |
66 |
this.addtype(URI, null, "URI", URI.class, new CoerceToURI()); |
67 |
|
68 |
this.addtype(ARRAY, null, "Array", null, null); |
69 |
this.addtype(LIST, null, "List", List.class, null); |
70 |
this.addtype(SET, null, "Set", Set.class, null); |
71 |
this.addtype(MAP, null, "Map", Map.class, null); |
72 |
|
73 |
this.addtype(OBJECT, null, "Object", null, null); |
74 |
} |
75 |
|
76 |
public synchronized int addtype(int type, String subtype, String name, |
77 |
Class defaultClass, Coercion coercion) {
|
78 |
if (type == INVALID) {
|
79 |
type = getNewObjectIndexType(); |
80 |
} |
81 |
if (type < 1 || type > MAX_TYPE_VALUE) { |
82 |
throw new IllegalArgumentException("Wrong type " |
83 |
+ Integer.toHexString(type).toUpperCase() + "."); |
84 |
} |
85 |
|
86 |
try {
|
87 |
// Check if the type has been already registereds
|
88 |
get(type); |
89 |
throw new IllegalArgumentException("type " |
90 |
+ Integer.toHexString(type).toUpperCase() + ", name " + name |
91 |
+ ", already registered as " + getTypeName(type) + "."); |
92 |
} catch (IllegalArgumentException e) { |
93 |
// OK, the type is still not registered.
|
94 |
} |
95 |
|
96 |
DataType dataType = |
97 |
new DefaultDataType(type, subtype, name, defaultClass, coercion);
|
98 |
types[type] = dataType; |
99 |
LOG.debug("Registered data type {}.", dataType.toString());
|
100 |
return type;
|
101 |
} |
102 |
|
103 |
private int getNewObjectIndexType() { |
104 |
for (int i = NEWINDEXES_AT; i <= MAX_TYPE_VALUE; i++) { |
105 |
if (types[i] == null) { |
106 |
return i;
|
107 |
} |
108 |
} |
109 |
return DataTypes.INVALID;
|
110 |
} |
111 |
|
112 |
public DataType get(int type) { |
113 |
DataType dataType = null;
|
114 |
if (type > 0 && type <= MAX_TYPE_VALUE) { |
115 |
dataType = types[type]; |
116 |
} |
117 |
|
118 |
if (dataType == null) { |
119 |
throw new IllegalArgumentException("DataType " + type |
120 |
+ " not registered");
|
121 |
} |
122 |
|
123 |
return dataType;
|
124 |
} |
125 |
|
126 |
public boolean isValidType(int type) { |
127 |
return type > 0 && type <= MAX_TYPE_VALUE && this.types[type] != null; |
128 |
} |
129 |
|
130 |
public boolean isObject(int type) { |
131 |
return (type & DataTypes.OBJECT) == DataTypes.OBJECT;
|
132 |
} |
133 |
|
134 |
public boolean isContainer(int type) { |
135 |
return (type & DataTypes.CONTAINER) == DataTypes.CONTAINER;
|
136 |
} |
137 |
|
138 |
public int getType(String name) { |
139 |
if (name != null) { |
140 |
for (int i = 0; i < types.length; i++) { |
141 |
if (types[i] != null |
142 |
&& name.equalsIgnoreCase(types[i].getName())) { |
143 |
return i;
|
144 |
} |
145 |
} |
146 |
} |
147 |
return DataTypes.INVALID;
|
148 |
} |
149 |
|
150 |
public String getTypeName(int type) { |
151 |
DataType dataType = get(type); |
152 |
String name = dataType.getName();
|
153 |
if (name == null) { |
154 |
return "unknow"; |
155 |
} |
156 |
return name;
|
157 |
} |
158 |
|
159 |
public Class getDefaultClass(int type) { |
160 |
DataType dataType = get(type); |
161 |
return dataType.getDefaultClass();
|
162 |
} |
163 |
|
164 |
public String getSubtype(int type) { |
165 |
DataType dataType = get(type); |
166 |
return dataType.getSubtype();
|
167 |
} |
168 |
|
169 |
public Coercion getCoercion(int type) { |
170 |
DataType dataType = get(type); |
171 |
return dataType.getCoercion();
|
172 |
} |
173 |
|
174 |
public void setCoercion(int type, Coercion coercion) { |
175 |
DataType dataType = get(type); |
176 |
dataType.setCoercion(coercion); |
177 |
} |
178 |
|
179 |
public Object coerce(int type, Object value) throws CoercionException { |
180 |
DataType dataType = get(type); |
181 |
return dataType.coerce(value);
|
182 |
} |
183 |
|
184 |
public Iterator iterator() { |
185 |
List list = new ArrayList(); |
186 |
DataType type = null;
|
187 |
for (int i = 0; i < this.types.length; i++) { |
188 |
type = this.types[i];
|
189 |
if (type != null) |
190 |
list.add(this.types[i]);
|
191 |
} |
192 |
return list.iterator();
|
193 |
} |
194 |
|
195 |
public DataType getDataType(Class defaultClass) { |
196 |
// First look for a class with exact match
|
197 |
for (int i = 0; i < types.length; i++) { |
198 |
if (types[i] != null) { |
199 |
Class typeClass = types[i].getDefaultClass();
|
200 |
if (typeClass != null && typeClass.equals(defaultClass)) { |
201 |
return types[i];
|
202 |
} |
203 |
} |
204 |
} |
205 |
// Then look for a class which is an interface or parent
|
206 |
for (int i = 0; i < types.length; i++) { |
207 |
if (types[i] != null) { |
208 |
Class typeClass = types[i].getDefaultClass();
|
209 |
if (typeClass != null |
210 |
&& typeClass.isAssignableFrom(defaultClass)) { |
211 |
return types[i];
|
212 |
} |
213 |
} |
214 |
} |
215 |
throw new IllegalArgumentException( |
216 |
"There is not any registered data type with the class or a "
|
217 |
+ "parent of the class: " + defaultClass);
|
218 |
} |
219 |
} |