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

History | View | Annotate | Download (7.93 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
import java.sql.Timestamp;
13

    
14

    
15
import org.slf4j.Logger;
16
import org.slf4j.LoggerFactory;
17

    
18
import org.gvsig.tools.dataTypes.CoercionException;
19
import org.gvsig.tools.dataTypes.DataType;
20
import org.gvsig.tools.dataTypes.DataTypes;
21
import org.gvsig.tools.dataTypes.DataTypesManager;
22
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToBoolean;
23
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToByte;
24
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDate;
25
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDateTime;
26
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDouble;
27
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToFile;
28
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToFloat;
29
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToInt;
30
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToLong;
31
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToString;
32
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToTime;
33
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToURI;
34
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToURL;
35
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToTimestamp;
36
import org.gvsig.tools.dynobject.DynObject;
37

    
38
public class DefaultDataTypesManager implements DataTypesManager, DataTypes {
39

    
40
    private static final Logger LOG = LoggerFactory
41
        .getLogger(DefaultDataTypesManager.class);
42

    
43
    private static final int NEWINDEXES_AT = OBJECT + 1;
44

    
45
    private DataType[] types = new DefaultDataType[MAX_TYPE_VALUE];
46

    
47
    public DefaultDataTypesManager() {
48

    
49
        this.addtype(BOOLEAN, null, "Boolean", Boolean.class,
50
            new CoerceToBoolean());
51
        this.addtype(BYTE, null, "Byte", Byte.class, new CoerceToByte());
52
        this.addtype(CHAR, null, "Char", Character.class, new CoerceToString());
53
        this.addtype(INT, null, "Integer", Integer.class, new CoerceToInt());
54
        this.addtype(LONG, null, "Long", Long.class, new CoerceToLong());
55
        this.addtype(FLOAT, null, "Float", Float.class, new CoerceToFloat());
56
        this.addtype(DOUBLE, null, "Double", Double.class, new CoerceToDouble());
57
        this.addtype(STRING, null, "String", String.class, new CoerceToString());
58
        this.addtype(DATE, SUBTYPE_DATE, "Date", Date.class, new CoerceToDate());
59
        this.addtype(TIME, SUBTYPE_DATE, "Time", Date.class, new CoerceToTime());
60
        this.addtype(TIMESTAMP, null, "Timestamp", Timestamp.class, new CoerceToTimestamp());
61

    
62
        this.addtype(BYTEARRAY, null, "ByteArray", null, null);
63
        this.addtype(FILE, SUBTYPE_FILE, "File", File.class, new CoerceToFile());
64
        this.addtype(FOLDER, SUBTYPE_FOLDER, "Folder", File.class,
65
            new CoerceToFile());
66
        this.addtype(DYNOBJECT, null, "DynObject", DynObject.class, null);
67
        this.addtype(URL, null, "URL", URL.class, new CoerceToURL());
68
        this.addtype(URI, null, "URI", URI.class, new CoerceToURI());
69

    
70
        this.addtype(ARRAY, null, "Array", null, null);
71
        this.addtype(LIST, null, "List", List.class, null);
72
        this.addtype(SET, null, "Set", Set.class, null);
73
        this.addtype(MAP, null, "Map", Map.class, null);
74

    
75
        this.addtype(OBJECT, null, "Object", null, null);
76
    }
77

    
78
    public synchronized int addtype(int type, String subtype, String name,
79
        Class defaultClass, Coercion coercion) {
80
        if (type == INVALID) {
81
            type = getNewObjectIndexType();
82
        }
83
        if (type < 1 || type > MAX_TYPE_VALUE) {
84
            throw new IllegalArgumentException("Wrong type "
85
                + Integer.toHexString(type).toUpperCase() + ".");
86
        }
87

    
88
        try {
89
            // Check if the type has been already registereds
90
            get(type);
91
            throw new IllegalArgumentException("type "
92
                + Integer.toHexString(type).toUpperCase() + ", name " + name
93
                + ", already registered as " + getTypeName(type) + ".");
94
        } catch (IllegalArgumentException e) {
95
            // OK, the type is still not registered.
96
        }
97

    
98
        DataType dataType =
99
            new DefaultDataType(type, subtype, name, defaultClass, coercion);
100
        types[type] = dataType;
101
        LOG.debug("Registered data type {}.", dataType.toString());
102
        return type;
103
    }
104

    
105
    private int getNewObjectIndexType() {
106
        for (int i = NEWINDEXES_AT; i <= MAX_TYPE_VALUE; i++) {
107
            if (types[i] == null) {
108
                return i;
109
            }
110
        }
111
        return DataTypes.INVALID;
112
    }
113

    
114
    public DataType get(int type) {
115
        DataType dataType = null;
116
        if (type > 0 && type <= MAX_TYPE_VALUE) {
117
            dataType = types[type];
118
        }
119

    
120
        if (dataType == null) {
121
            throw new IllegalArgumentException("DataType " + type
122
                + " not registered");
123
        }
124

    
125
        return dataType;
126
    }
127

    
128
    public boolean isValidType(int type) {
129
        return type > 0 && type <= MAX_TYPE_VALUE && this.types[type] != null;
130
    }
131

    
132
    public boolean isObject(int type) {
133
        return (type & DataTypes.OBJECT) == DataTypes.OBJECT;
134
    }
135

    
136
    public boolean isContainer(int type) {
137
        return (type & DataTypes.CONTAINER) == DataTypes.CONTAINER;
138
    }
139

    
140
    public int getType(String name) {
141
        if (name != null) {
142
            for (int i = 0; i < types.length; i++) {
143
                if (types[i] != null
144
                    && name.equalsIgnoreCase(types[i].getName())) {
145
                    return i;
146
                }
147
            }
148
        }
149
        return DataTypes.INVALID;
150
    }
151

    
152
    public String getTypeName(int type) {
153
        DataType dataType = get(type);
154
        String name = dataType.getName();
155
        if (name == null) {
156
            return "unknow";
157
        }
158
        return name;
159
    }
160

    
161
    public Class getDefaultClass(int type) {
162
        DataType dataType = get(type);
163
        return dataType.getDefaultClass();
164
    }
165

    
166
    public String getSubtype(int type) {
167
        DataType dataType = get(type);
168
        return dataType.getSubtype();
169
    }
170

    
171
    public Coercion getCoercion(int type) {
172
        DataType dataType = get(type);
173
        return dataType.getCoercion();
174
    }
175

    
176
    public void setCoercion(int type, Coercion coercion) {
177
        DataType dataType = get(type);
178
        dataType.setCoercion(coercion);
179
    }
180

    
181
    public void addCoercion(int type, Coercion coercion) {
182
        DataType dataType = get(type);
183
        dataType.addCoercion(coercion);
184
    }
185

    
186
    public Object coerce(int type, Object value) throws CoercionException {
187
        DataType dataType = get(type);
188
        return dataType.coerce(value);
189
    }
190

    
191
    public Iterator iterator() {
192
        List list = new ArrayList();
193
        DataType type = null;
194
        for (int i = 0; i < this.types.length; i++) {
195
            type = this.types[i];
196
            if (type != null)
197
                list.add(this.types[i]);
198
        }
199
        return list.iterator();
200
    }
201

    
202
    public DataType getDataType(Class defaultClass) {
203
        // First look for a class with exact match
204
        for (int i = 0; i < types.length; i++) {
205
            if (types[i] != null) {
206
                Class typeClass = types[i].getDefaultClass();
207
                if (typeClass != null && typeClass.equals(defaultClass)) {
208
                    return types[i];
209
                }
210
            }
211
        }
212
        // Then look for a class which is an interface or parent
213
        for (int i = 0; i < types.length; i++) {
214
            if (types[i] != null) {
215
                Class typeClass = types[i].getDefaultClass();
216
                if (typeClass != null
217
                    && typeClass.isAssignableFrom(defaultClass)) {
218
                    return types[i];
219
                }
220
            }
221
        }
222
        throw new IllegalArgumentException(
223
            "There is not any registered data type with the class or a "
224
                + "parent of the class: " + defaultClass);
225
    }
226
}