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 @ 2083

History | View | Annotate | Download (12.1 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 static org.gvsig.tools.dataTypes.DataType.BYTE_DEFAULT_PRECISION;
42
import static org.gvsig.tools.dataTypes.DataType.BYTE_MAX_PRECISION;
43
import static org.gvsig.tools.dataTypes.DataType.DECIMAL_DEFAULT_PRECISION;
44
import static org.gvsig.tools.dataTypes.DataType.DECIMAL_DEFAULT_SCALE;
45
import static org.gvsig.tools.dataTypes.DataType.DECIMAL_MAX_PRECISION;
46
import static org.gvsig.tools.dataTypes.DataType.DOUBLE_DEFAULT_PRECISION;
47
import static org.gvsig.tools.dataTypes.DataType.DOUBLE_DEFAULT_SCALE;
48
import static org.gvsig.tools.dataTypes.DataType.DOUBLE_MAX_PRECISION;
49
import static org.gvsig.tools.dataTypes.DataType.FLOAT_DEFAULT_PRECISION;
50
import static org.gvsig.tools.dataTypes.DataType.FLOAT_DEFAULT_SCALE;
51
import static org.gvsig.tools.dataTypes.DataType.FLOAT_MAX_PRECISION;
52
import static org.gvsig.tools.dataTypes.DataType.INT_DEFAULT_PRECISION;
53
import static org.gvsig.tools.dataTypes.DataType.INT_MAX_PRECISION;
54
import static org.gvsig.tools.dataTypes.DataType.LONG_DEFAULT_PRECISION;
55
import static org.gvsig.tools.dataTypes.DataType.LONG_MAX_PRECISION;
56
import org.gvsig.tools.dataTypes.DataTypes;
57
import org.gvsig.tools.dataTypes.DataTypesManager;
58
import static org.gvsig.tools.dataTypes.impl.DefaultDataType.NO_SUPPORT_SCALE;
59
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDecimal;
60
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToBoolean;
61
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToByte;
62
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToBytearray;
63
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDate;
64
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDateTime;
65
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDouble;
66
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToFile;
67
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToFloat;
68
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToInt;
69
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToLong;
70
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToObject;
71
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToString;
72
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToTime;
73
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToURI;
74
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToURL;
75
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToVersion;
76
import org.gvsig.tools.dynobject.DynObject;
77
import org.slf4j.Logger;
78
import org.slf4j.LoggerFactory;
79

    
80
public class DefaultDataTypesManager implements DataTypesManager, DataTypes {
81

    
82
  private static final Logger LOG = LoggerFactory
83
          .getLogger(DefaultDataTypesManager.class);
84

    
85
  private static final int NEWINDEXES_AT = OBJECT + 1;
86

    
87
  private final DataType[] types = new DefaultDataType[MAX_TYPE_VALUE];
88

    
89
  public DefaultDataTypesManager() {
90

    
91
    this.addtype(BOOLEAN, null, "Boolean", Boolean.class, new CoerceToBoolean(), 
92
            "datatype-boolean"
93
    );
94
    this.addtype(BYTE, null, "Byte", Byte.class, new CoerceToByte(),
95
            "datatype-byte",
96
            true, BYTE_MAX_PRECISION, BYTE_DEFAULT_PRECISION
97
    );
98
    this.addtype(CHAR, null, "Char", Character.class, new CoerceToString());
99
    this.addtype(INT, null, "Integer", Integer.class, new CoerceToInt(), 
100
            "datatype-integer", 
101
            true, INT_MAX_PRECISION, INT_DEFAULT_PRECISION
102
    );
103
    this.addtype(LONG, null, "Long", Long.class, new CoerceToLong(), 
104
            "datatype-long", 
105
            true, LONG_MAX_PRECISION, LONG_DEFAULT_PRECISION
106
    );
107
    this.addtype(FLOAT, null, "Float", Float.class, new CoerceToFloat(), 
108
            "datatype-float", 
109
            false, FLOAT_MAX_PRECISION, FLOAT_DEFAULT_PRECISION, FLOAT_DEFAULT_SCALE
110
    );
111
    this.addtype(DOUBLE, null, "Double", Double.class, new CoerceToDouble(), 
112
            "datatype-double", 
113
            false, DOUBLE_MAX_PRECISION, DOUBLE_DEFAULT_PRECISION, DOUBLE_DEFAULT_SCALE
114
    );
115
    this.addtype(DECIMAL, null, "Decimal", BigDecimal.class, new CoerceToDecimal(), 
116
            "datatype-decimal", 
117
            false, DECIMAL_MAX_PRECISION, DECIMAL_DEFAULT_PRECISION, DECIMAL_DEFAULT_SCALE
118
    );
119
    this.addtype(STRING, null, "String", String.class, new CoerceToString(), "datatype-string");
120
    this.addtype(DATE, SUBTYPE_DATE, "Date", Date.class, new CoerceToDate(), "datatype-date");
121
    this.addtype(TIME, SUBTYPE_DATE, "Time", Date.class, new CoerceToTime(), "datatype-time");
122
//        this.addtype(TIMESTAMP, null, "Timestamp", Timestamp.class, new CoerceToTimestamp(),"datatype-timestamp");
123
    this.addtype(TIMESTAMP, null, "Timestamp", Date.class, new CoerceToDateTime(), "datatype-timestamp");
124
    this.addtype(VERSION, null, "Version", Version.class, new CoerceToVersion());
125

    
126
    this.addtype(BYTEARRAY, null, "ByteArray",
127
            (new byte[1]).getClass(),
128
            new CoerceToBytearray(),
129
            "datatype-bytearray"
130
    );
131
    this.addtype(FILE, SUBTYPE_FILE, "File", File.class, new CoerceToFile());
132
    this.addtype(FOLDER, SUBTYPE_FOLDER, "Folder", File.class, new CoerceToFile());
133
    this.addtype(DYNOBJECT, null, "DynObject", DynObject.class, null);
134
    this.addtype(URL, null, "URL", URL.class, new CoerceToURL());
135
    this.addtype(URI, null, "URI", URI.class, new CoerceToURI());
136

    
137
    this.addtype(ARRAY, null, "Array", (new Object[1]).getClass(), null);
138
    this.addtype(LIST, null, "List", List.class, null);
139
    this.addtype(SET, null, "Set", Set.class, null);
140
    this.addtype(MAP, null, "Map", Map.class, null);
141

    
142
    this.addtype(OBJECT, null, "Object", Object.class, new CoerceToObject(), "datatype-object");
143
  }
144

    
145
  @Override
146
  public final synchronized int addtype(int type, String subtype, String name,
147
          Class defaultClass, Coercion coercion) {
148
    return addtype(type, subtype, name, defaultClass, coercion, null, false, 0, 0, 0);
149
  }
150

    
151
  public final synchronized int addtype(int type, String subtype, String name,
152
          Class defaultClass, Coercion coercion, String iconName) {
153
    return addtype(type, subtype, name, defaultClass, coercion, iconName, false, 0, 0, 0);
154
  }
155

    
156
  public final synchronized int addtype(int type, String subtype, String name,
157
          Class defaultClass, Coercion coercion, String iconName,
158
          boolean fixedPrecision, int max_precision, int default_precision) {
159
    return addtype(type, subtype, name, defaultClass, coercion, iconName,
160
            fixedPrecision, max_precision, default_precision, NO_SUPPORT_SCALE
161
    );
162
  }
163

    
164
  @Override
165
  public final synchronized int addtype(int type, String subtype, String name,
166
          Class defaultClass, Coercion coercion, String iconName,
167
          boolean fixedPrecision, int max_precision, int default_precision, int default_scale) {
168
    if (type == INVALID) {
169
      type = getNewObjectIndexType();
170
    }
171
    if (type < 1 || type > MAX_TYPE_VALUE) {
172
      throw new IllegalArgumentException("Wrong type "
173
              + Integer.toHexString(type).toUpperCase() + ".");
174
    }
175

    
176
    try {
177
      // Check if the type has been already registereds
178
      get(type);
179
      throw new IllegalArgumentException("type "
180
              + Integer.toHexString(type).toUpperCase() + ", name " + name
181
              + ", already registered as " + getTypeName(type) + ".");
182
    } catch (IllegalArgumentException e) {
183
      // OK, the type is still not registered.
184
    }
185

    
186
    DataType dataType = new DefaultDataType(
187
            type, subtype, name, defaultClass, coercion, iconName,
188
            fixedPrecision, max_precision, default_precision, default_scale
189
    );
190
    types[type] = dataType;
191
    LOG.info("Registered data type {}.", dataType.toString());
192
    return type;
193
  }
194

    
195
  private int getNewObjectIndexType() {
196
    for (int i = NEWINDEXES_AT; i <= MAX_TYPE_VALUE; i++) {
197
      if (types[i] == null) {
198
        return i;
199
      }
200
    }
201
    return DataTypes.INVALID;
202
  }
203

    
204
  @Override
205
  public DataType get(int type) {
206
    DataType dataType = null;
207
    if (type > 0 && type <= MAX_TYPE_VALUE) {
208
      dataType = types[type];
209
    }
210

    
211
    if (dataType == null) {
212
      throw new IllegalArgumentException("DataType " + type
213
              + " not registered");
214
    }
215

    
216
    return dataType;
217
  }
218

    
219
  @Override
220
  public boolean isValidType(int type) {
221
    return type > 0 && type <= MAX_TYPE_VALUE && this.types[type] != null;
222
  }
223

    
224
  @Override
225
  public boolean isObject(int type) {
226
    return (type & DataTypes.OBJECT) == DataTypes.OBJECT;
227
  }
228

    
229
  @Override
230
  public boolean isContainer(int type) {
231
    return (type & DataTypes.CONTAINER) == DataTypes.CONTAINER;
232
  }
233

    
234
  @Override
235
  public int getType(String name) {
236
    if (name != null) {
237
      for (int i = 0; i < types.length; i++) {
238
        if (types[i] != null
239
                && name.equalsIgnoreCase(types[i].getName())) {
240
          return i;
241
        }
242
      }
243
    }
244
    return DataTypes.INVALID;
245
  }
246

    
247
  @Override
248
  public String getTypeName(int type) {
249
    DataType dataType = get(type);
250
    String name = dataType.getName();
251
    if (name == null) {
252
      return "unknow";
253
    }
254
    return name;
255
  }
256

    
257
  @Override
258
  public Class getDefaultClass(int type) {
259
    DataType dataType = get(type);
260
    return dataType.getDefaultClass();
261
  }
262

    
263
  @Override
264
  public String getSubtype(int type) {
265
    DataType dataType = get(type);
266
    return dataType.getSubtype();
267
  }
268

    
269
  @Override
270
  public Coercion getCoercion(int type) {
271
    DataType dataType = get(type);
272
    return dataType.getCoercion();
273
  }
274

    
275
//    @Override
276
//    public void setCoercion(int type, Coercion coercion) {
277
//        DataType dataType = get(type);
278
//        dataType.setCoercion(coercion);
279
//    }
280
  @Override
281
  public void addCoercion(int type, Coercion coercion) {
282
    DataType dataType = get(type);
283
    dataType.addCoercion(coercion);
284
  }
285

    
286
  @Override
287
  public Object coerce(int type, Object value) throws CoercionException {
288
    DataType dataType = get(type);
289
    return dataType.coerce(value);
290
  }
291

    
292
  @Override
293
  public Iterator iterator() {
294
    List list = new ArrayList();
295
    DataType type;
296
    for (int i = 0; i < this.types.length; i++) {
297
      type = this.types[i];
298
      if (type != null) {
299
        list.add(this.types[i]);
300
      }
301
    }
302
    return list.iterator();
303
  }
304

    
305
  @Override
306
  public DataType getDataType(Class defaultClass) {
307
    // First look for a class with exact match
308
    for (DataType type : types) {
309
      if (type != null) {
310
        Class typeClass = type.getDefaultClass();
311
        if (typeClass != null && typeClass.equals(defaultClass)) {
312
          return type;
313
        }
314
      }
315
    }
316
    // Then look for a class which is an interface or parent
317
    for (DataType type : types) {
318
      if (type != null) {
319
        Class typeClass = type.getDefaultClass();
320
        if (typeClass != null
321
                && typeClass.isAssignableFrom(defaultClass)) {
322
          return type;
323
        }
324
      }
325
    }
326
    throw new IllegalArgumentException(
327
            "There is not any registered data type with the class or a "
328
            + "parent of the class: " + defaultClass);
329
  }
330

    
331
}