Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dataTypes / impl / DefaultDataType.java @ 2029

History | View | Annotate | Download (6.97 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.text.MessageFormat;
27
import java.util.Locale;
28

    
29
import org.gvsig.tools.dataTypes.CoercionException;
30
import org.gvsig.tools.dataTypes.DataType;
31
import org.gvsig.tools.dataTypes.DataTypes;
32
import org.gvsig.tools.dataTypes.DataTypesManager;
33
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
34
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale;
35
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37

    
38
public class DefaultDataType implements DataType {
39

    
40
        private static final Logger LOG = LoggerFactory.getLogger(DefaultDataTypesManager.class);
41
        
42
        private Coercion coercion;
43
        private Class defaultClass;
44
        private String subtype;
45
        private int type;
46
        private String name;
47
        private String iconName;
48
        
49
        DefaultDataType(int type, String subtype, String name, Class defaultClass, Coercion coercion) {
50
            this(type, subtype, name, defaultClass, coercion, "datatype-any");
51
        }
52
        
53
        DefaultDataType(int type, String subtype, String name, Class defaultClass, Coercion coercion, String iconName) {
54

    
55
                if( name == null ) {
56
                        LOG.trace("Can't register null type name for type {}.", new Object[] { Integer.toHexString(type).toUpperCase()});
57
                        throw new IllegalArgumentException();
58
                }
59
                this.type = type;
60
                this.subtype = subtype;
61
                this.name = name;
62
                this.defaultClass  = defaultClass;
63
                this.coercion = coercion;
64
                this.iconName = iconName==null? "datatype-any":iconName;
65
        }
66

    
67
        @Override
68
        public String getLabel() {
69
            return this.getName();
70
        }
71

    
72
        @Override
73
        public DataType getValue() {
74
            return this;
75
        }
76
    
77
        @Override
78
        public Object coerce(Object value) throws CoercionException {
79
                // http://en.wikipedia.org/wiki/Type_conversion#Implicit_type_conversion
80

    
81
                if( this.coercion != null ) {
82
                        return this.coercion.coerce(value);
83
                }
84
                if( defaultClass == null ) {
85
                        return value; // �?
86
                }
87
                if( defaultClass.isInstance(value) ) {
88
                        return value;
89
                }
90
                throw new CoercionException();
91
        }
92

    
93
        @Override
94
        public Coercion getCoercion() {
95
                return this.coercion;
96
        }
97

    
98
        @Override
99
        public Class getDefaultClass() {
100
                return this.defaultClass;
101
        }
102

    
103
        @Override
104
        public String getSubtype() {
105
                return this.subtype;
106
        }
107

    
108
        @Override
109
        public int getType() {
110
                return this.type;
111
        }
112

    
113
        @Override
114
        public String getName() {
115
                return this.name;
116
        }
117

    
118
        @Override
119
        public String getIconName() {
120
            return this.iconName;
121
        }
122

    
123
        @Override
124
        public boolean isContainer() {
125
                return (type & DataTypes.CONTAINER) == DataTypes.CONTAINER;
126
        }
127

    
128
        @Override
129
        public boolean isObject() {
130
                return (type & DataTypes.OBJECT) == DataTypes.OBJECT;
131
        }
132

    
133
        @Override
134
        public boolean isDynObject() {
135
                return type == DataTypes.DYNOBJECT;
136
        }
137

    
138
        @Override
139
        public void setCoercion(Coercion coercion) {
140
                this.coercion = coercion;
141
                LOG.trace("Add coercion operation for data type {}.", new Object[] { name });
142
        }
143

    
144
        @Override
145
        public void addCoercion(Coercion coercion) {
146
                if( this.coercion==null ) {
147
                    this.setCoercion(coercion);
148
                    return;
149
                }
150
                LinkedCoercions coercions;
151
                if (this.coercion instanceof LinkedCoercions) {
152
                        coercions = (LinkedCoercions) this.coercion;
153
                } else {
154
                        coercions = new LinkedCoercions();
155
                        coercions.add(this.coercion);
156
                        this.coercion = coercions;
157
                }
158
                coercions.add(coercion);
159
                LOG.trace("Add coercion operation for data type {}.", new Object[] { name });
160
        }
161

    
162
        @Override
163
        public String toString() {
164
                return MessageFormat.format(
165
                                "type=0x{0};subtype={1};name={2};class={3};coercion={4}", 
166
                                new Object[] {
167
                                        Integer.toHexString(type).toUpperCase(),
168
                                        subtype,
169
                                        name,
170
                                        defaultClass==null? null:defaultClass.getName(),
171
                                        coercion==null? null:coercion.getClass().getName()
172
                                }
173
                );
174
        }
175

    
176
        @Override
177
        public boolean isNumeric() {
178
                if( type == DataTypes.DOUBLE ||
179
                    type == DataTypes.FLOAT ||
180
                    type == DataTypes.INT ||
181
                    type == DataTypes.LONG){
182
                    return true;
183
                }
184
                return false;
185
        }
186

    
187
        public class LinkedCoercions implements DataTypesManager.CoercionWithLocale {
188
        
189
                public class LinkedCoercion {
190
                        Coercion coercion;
191
                        LinkedCoercion theNext;
192
                        
193
                        LinkedCoercion(Coercion coercion, LinkedCoercion next) {
194
                                this.theNext = next; 
195
                                this.coercion = coercion;
196
                        }
197
                        
198
                        public Object coerce(Object value) throws CoercionException {
199
                                return this.coercion.coerce(value);
200
                        }
201
                        
202
                        public boolean is(Coercion coercion) {
203
                                return coercion.getClass().getName().equals(this.coercion.getClass().getName());
204
                        }
205
                        
206
                        public boolean hasNext() {
207
                                return this.theNext != null;
208
                        }
209
                        
210
                        public LinkedCoercion next() {
211
                                return this.theNext;
212
                        }
213
                }
214
                
215
                LinkedCoercion head = null;
216
                
217
                public LinkedCoercions() {
218
                        super();
219
                }
220
                
221
                public void add(Coercion coercion) {
222
                        if( !this.contains(coercion)) {
223
                                head = new LinkedCoercion(coercion, head);
224
                        }
225
                }
226

    
227
                public boolean contains(Coercion coercion) {
228
                        LinkedCoercion x = head;
229
                        while( x!=null ) {
230
                                if( x.is(coercion)) {
231
                                        return true;
232
                                }
233
                                x = x.next();
234
                        }
235
                        return false;
236
                }
237
                @Override
238
                public Object coerce(Object value) throws CoercionException {
239
                    return coerce(value, null);
240
                }
241
                
242
                @Override
243
                public Object coerce(Object value, Locale locale) throws CoercionException {
244
                        LinkedCoercion x = head;
245
                        while( x!=null ) {
246
                                try {
247
                                    if (x instanceof CoercionWithLocale) {
248
                                        return ((CoercionWithLocale) x).coerce(value, locale);
249
                                    }
250
                                    return x.coerce(value);
251
                                } catch (CoercionException e) {
252
                                    x = x.next();
253
                                    if( x==null ) {
254
                                        throw e;
255
                                    }
256
                                }
257
                        }
258
                        throw new CoercionException();
259
                }
260

    
261
        }
262

    
263
        
264
}