Statistics
| Revision:

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 802 cordinyana
/**
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 112 jjdelcerro
package org.gvsig.tools.dataTypes.impl;
25
26
import java.io.File;
27 1426 dmartinezizquierdo
import java.math.BigDecimal;
28 220 jjdelcerro
import java.net.URI;
29
import java.net.URL;
30 328 cmartin
import java.util.ArrayList;
31 112 jjdelcerro
import java.util.Date;
32 328 cmartin
import java.util.Iterator;
33 112 jjdelcerro
import java.util.List;
34 2088 jjdelcerro
import java.util.Locale;
35 112 jjdelcerro
import java.util.Map;
36
import java.util.Set;
37
38 931 jjdelcerro
import org.gvsig.installer.lib.api.Version;
39 2080 jjdelcerro
import org.gvsig.tools.dataTypes.Coercion;
40 112 jjdelcerro
import org.gvsig.tools.dataTypes.CoercionException;
41 145 jjdelcerro
import org.gvsig.tools.dataTypes.DataType;
42 2083 jjdelcerro
import static org.gvsig.tools.dataTypes.DataType.BYTE_DEFAULT_PRECISION;
43
import static org.gvsig.tools.dataTypes.DataType.BYTE_MAX_PRECISION;
44 2082 jjdelcerro
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 2088 jjdelcerro
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 2082 jjdelcerro
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 2083 jjdelcerro
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 2088 jjdelcerro
import static org.gvsig.tools.dataTypes.DataType.PRECISION_NONE;
64
import static org.gvsig.tools.dataTypes.DataType.SCALE_NONE;
65 112 jjdelcerro
import org.gvsig.tools.dataTypes.DataTypes;
66
import org.gvsig.tools.dataTypes.DataTypesManager;
67 2080 jjdelcerro
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDecimal;
68 112 jjdelcerro
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToBoolean;
69
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToByte;
70 1894 jjdelcerro
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToBytearray;
71 112 jjdelcerro
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 2088 jjdelcerro
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToLocale;
77 112 jjdelcerro
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToLong;
78 1003 jjdelcerro
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToObject;
79 112 jjdelcerro
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToString;
80 602 cordinyana
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToTime;
81 2160 jjdelcerro
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToTimestamp;
82 220 jjdelcerro
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToURI;
83
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToURL;
84 931 jjdelcerro
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToVersion;
85 112 jjdelcerro
import org.gvsig.tools.dynobject.DynObject;
86 931 jjdelcerro
import org.slf4j.Logger;
87
import org.slf4j.LoggerFactory;
88 112 jjdelcerro
89 145 jjdelcerro
public class DefaultDataTypesManager implements DataTypesManager, DataTypes {
90 112 jjdelcerro
91 2083 jjdelcerro
  private static final Logger LOG = LoggerFactory
92
          .getLogger(DefaultDataTypesManager.class);
93 653 cordinyana
94 2083 jjdelcerro
  private static final int NEWINDEXES_AT = OBJECT + 1;
95 653 cordinyana
96 2083 jjdelcerro
  private final DataType[] types = new DefaultDataType[MAX_TYPE_VALUE];
97 653 cordinyana
98 2083 jjdelcerro
  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 2088 jjdelcerro
            FLAG_NUMBER|FLAG_SUPPORT_PRECISION|FLAG_PREDEFINED_PRECISION,
106
            BYTE_MAX_PRECISION, BYTE_DEFAULT_PRECISION
107 2083 jjdelcerro
    );
108
    this.addtype(CHAR, null, "Char", Character.class, new CoerceToString());
109
    this.addtype(INT, null, "Integer", Integer.class, new CoerceToInt(),
110
            "datatype-integer",
111 2088 jjdelcerro
            FLAG_NUMBER|FLAG_SUPPORT_PRECISION|FLAG_PREDEFINED_PRECISION,
112
            INT_MAX_PRECISION, INT_DEFAULT_PRECISION
113 2083 jjdelcerro
    );
114
    this.addtype(LONG, null, "Long", Long.class, new CoerceToLong(),
115
            "datatype-long",
116 2088 jjdelcerro
            FLAG_NUMBER|FLAG_SUPPORT_PRECISION|FLAG_PREDEFINED_PRECISION,
117
            LONG_MAX_PRECISION, LONG_DEFAULT_PRECISION
118 2083 jjdelcerro
    );
119
    this.addtype(FLOAT, null, "Float", Float.class, new CoerceToFloat(),
120
            "datatype-float",
121 2088 jjdelcerro
            FLAG_FLOATING_POINT|FLAG_SUPPORT_PRECISION|FLAG_PREDEFINED_PRECISION|FLAG_SUPPORT_SCALE,
122
            FLOAT_MAX_PRECISION, FLOAT_DEFAULT_PRECISION, FLOAT_DEFAULT_SCALE
123 2083 jjdelcerro
    );
124
    this.addtype(DOUBLE, null, "Double", Double.class, new CoerceToDouble(),
125
            "datatype-double",
126 2088 jjdelcerro
            FLAG_FLOATING_POINT|FLAG_SUPPORT_PRECISION|FLAG_PREDEFINED_PRECISION|FLAG_SUPPORT_SCALE,
127
            DOUBLE_MAX_PRECISION, DOUBLE_DEFAULT_PRECISION, DOUBLE_DEFAULT_SCALE
128 2083 jjdelcerro
    );
129
    this.addtype(DECIMAL, null, "Decimal", BigDecimal.class, new CoerceToDecimal(),
130
            "datatype-decimal",
131 2088 jjdelcerro
            FLAG_NUMBER|FLAG_SUPPORT_PRECISION|FLAG_SUPPORT_SCALE,
132
            DECIMAL_MAX_PRECISION, DECIMAL_DEFAULT_PRECISION, DECIMAL_DEFAULT_SCALE
133 2083 jjdelcerro
    );
134 2088 jjdelcerro
    this.addtype(STRING, null, "String", String.class, new CoerceToString(),
135
            "datatype-string",
136
            FLAG_SUPPORT_SIZE
137
    );
138 2083 jjdelcerro
    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 2160 jjdelcerro
    this.addtype(TIMESTAMP, SUBTYPE_DATE, "Timestamp", Date.class, new CoerceToTimestamp(), "datatype-timestamp");
141 2088 jjdelcerro
142
    this.addtype(LOCALE, null, "Locale", Locale.class, new CoerceToLocale(),
143
            "datatype-locale"
144
    );
145
146
147 2083 jjdelcerro
    this.addtype(VERSION, null, "Version", Version.class, new CoerceToVersion());
148 145 jjdelcerro
149 2083 jjdelcerro
    this.addtype(BYTEARRAY, null, "ByteArray",
150
            (new byte[1]).getClass(),
151
            new CoerceToBytearray(),
152 2088 jjdelcerro
            "datatype-bytearray",
153
            FLAG_SUPPORT_SIZE
154 2083 jjdelcerro
    );
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 145 jjdelcerro
161 2083 jjdelcerro
    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 112 jjdelcerro
166 2083 jjdelcerro
    this.addtype(OBJECT, null, "Object", Object.class, new CoerceToObject(), "datatype-object");
167
  }
168 184 cordinyana
169 2083 jjdelcerro
  @Override
170
  public final synchronized int addtype(int type, String subtype, String name,
171
          Class defaultClass, Coercion coercion) {
172 2088 jjdelcerro
    return addtype(type, subtype, name, defaultClass, coercion, null, 0, PRECISION_NONE, PRECISION_NONE, SCALE_NONE);
173 2083 jjdelcerro
  }
174 184 cordinyana
175 2083 jjdelcerro
  public final synchronized int addtype(int type, String subtype, String name,
176
          Class defaultClass, Coercion coercion, String iconName) {
177 2088 jjdelcerro
    return addtype(type, subtype, name, defaultClass, coercion, iconName, 0, PRECISION_NONE, PRECISION_NONE, SCALE_NONE);
178 2083 jjdelcerro
  }
179 112 jjdelcerro
180 2088 jjdelcerro
  @Override
181 2083 jjdelcerro
  public final synchronized int addtype(int type, String subtype, String name,
182 2088 jjdelcerro
          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 2083 jjdelcerro
          Class defaultClass, Coercion coercion, String iconName,
190 2088 jjdelcerro
          int flags, int max_precision, int default_precision) {
191 2083 jjdelcerro
    return addtype(type, subtype, name, defaultClass, coercion, iconName,
192 2088 jjdelcerro
            flags, max_precision, default_precision, SCALE_NONE
193 2083 jjdelcerro
    );
194
  }
195
196
  @Override
197
  public final synchronized int addtype(int type, String subtype, String name,
198
          Class defaultClass, Coercion coercion, String iconName,
199 2088 jjdelcerro
          int flags, int max_precision, int default_precision, int default_scale) {
200 2083 jjdelcerro
    if (type == INVALID) {
201
      type = getNewObjectIndexType();
202 653 cordinyana
    }
203 2083 jjdelcerro
    if (type < 1 || type > MAX_TYPE_VALUE) {
204
      throw new IllegalArgumentException("Wrong type "
205
              + Integer.toHexString(type).toUpperCase() + ".");
206
    }
207 112 jjdelcerro
208 2083 jjdelcerro
    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 653 cordinyana
    }
217 112 jjdelcerro
218 2083 jjdelcerro
    DataType dataType = new DefaultDataType(
219
            type, subtype, name, defaultClass, coercion, iconName,
220 2088 jjdelcerro
            flags, max_precision, default_precision, default_scale
221 2083 jjdelcerro
    );
222
    types[type] = dataType;
223
    LOG.info("Registered data type {}.", dataType.toString());
224
    return type;
225
  }
226 112 jjdelcerro
227 2083 jjdelcerro
  private int getNewObjectIndexType() {
228
    for (int i = NEWINDEXES_AT; i <= MAX_TYPE_VALUE; i++) {
229
      if (types[i] == null) {
230
        return i;
231
      }
232 653 cordinyana
    }
233 2083 jjdelcerro
    return DataTypes.INVALID;
234
  }
235 119 jjdelcerro
236 2083 jjdelcerro
  @Override
237
  public DataType get(int type) {
238
    DataType dataType = null;
239
    if (type > 0 && type <= MAX_TYPE_VALUE) {
240
      dataType = types[type];
241 653 cordinyana
    }
242 328 cmartin
243 2083 jjdelcerro
    if (dataType == null) {
244
      throw new IllegalArgumentException("DataType " + type
245
              + " not registered");
246 653 cordinyana
    }
247
248 2083 jjdelcerro
    return dataType;
249
  }
250 653 cordinyana
251 2083 jjdelcerro
  @Override
252
  public boolean isValidType(int type) {
253
    return type > 0 && type <= MAX_TYPE_VALUE && this.types[type] != null;
254
  }
255 653 cordinyana
256 2083 jjdelcerro
  @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 653 cordinyana
        }
274 2083 jjdelcerro
      }
275 653 cordinyana
    }
276 2083 jjdelcerro
    return DataTypes.INVALID;
277
  }
278 653 cordinyana
279 2083 jjdelcerro
  @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 653 cordinyana
    }
286 2083 jjdelcerro
    return name;
287
  }
288 653 cordinyana
289 2083 jjdelcerro
  @Override
290
  public Class getDefaultClass(int type) {
291
    DataType dataType = get(type);
292
    return dataType.getDefaultClass();
293
  }
294 653 cordinyana
295 2083 jjdelcerro
  @Override
296
  public String getSubtype(int type) {
297
    DataType dataType = get(type);
298
    return dataType.getSubtype();
299
  }
300 653 cordinyana
301 2083 jjdelcerro
  @Override
302
  public Coercion getCoercion(int type) {
303
    DataType dataType = get(type);
304
    return dataType.getCoercion();
305
  }
306
307 2080 jjdelcerro
//    @Override
308
//    public void setCoercion(int type, Coercion coercion) {
309
//        DataType dataType = get(type);
310
//        dataType.setCoercion(coercion);
311
//    }
312 2083 jjdelcerro
  @Override
313
  public void addCoercion(int type, Coercion coercion) {
314
    DataType dataType = get(type);
315
    dataType.addCoercion(coercion);
316
  }
317 653 cordinyana
318 2083 jjdelcerro
  @Override
319
  public Object coerce(int type, Object value) throws CoercionException {
320
    DataType dataType = get(type);
321
    return dataType.coerce(value);
322
  }
323 697 jjdelcerro
324 2083 jjdelcerro
  @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 653 cordinyana
    }
334 2083 jjdelcerro
    return list.iterator();
335
  }
336 653 cordinyana
337 2083 jjdelcerro
  @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 328 cmartin
        }
346 2083 jjdelcerro
      }
347 328 cmartin
    }
348 2083 jjdelcerro
    // 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 653 cordinyana
        }
356 2083 jjdelcerro
      }
357
    }
358
    throw new IllegalArgumentException(
359 653 cordinyana
            "There is not any registered data type with the class or a "
360 2083 jjdelcerro
            + "parent of the class: " + defaultClass);
361
  }
362
363 112 jjdelcerro
}