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

History | View | Annotate | Download (4.3 KB)

1
package org.gvsig.tools.dataTypes.impl;
2

    
3
import java.text.MessageFormat;
4

    
5
import org.gvsig.tools.dataTypes.CoercionException;
6
import org.gvsig.tools.dataTypes.DataType;
7
import org.gvsig.tools.dataTypes.DataTypes;
8
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
9
import org.slf4j.Logger;
10
import org.slf4j.LoggerFactory;
11

    
12
public class DefaultDataType implements DataType {
13

    
14
        private static final Logger LOG = LoggerFactory.getLogger(DefaultDataTypesManager.class);
15
        
16
        private Coercion coercion;
17
        private Class defaultClass;
18
        private String subtype;
19
        private int type;
20
        private String name;
21
        
22
        DefaultDataType(int type, String subtype, String name, Class defaultClass, Coercion coercion) {
23

    
24
                if( name == null ) {
25
                        LOG.trace("Can't register null type name for type {}.", new Object[] { Integer.toHexString(type).toUpperCase()});
26
                        throw new IllegalArgumentException();
27
                }
28
                this.type = type;
29
                this.subtype = subtype;
30
                this.name = name;
31
                this.defaultClass  = defaultClass;
32
                this.coercion = coercion;
33
        }
34
        
35
        public Object coerce(Object value) throws CoercionException {
36
                // http://en.wikipedia.org/wiki/Type_conversion#Implicit_type_conversion
37

    
38
                if( this.coercion != null ) {
39
                        return this.coercion.coerce(value);
40
                }
41
                if( defaultClass == null ) {
42
                        return value; // �?
43
                }
44
                if( defaultClass.isInstance(value) ) {
45
                        return value;
46
                }
47
                throw new CoercionException();
48
        }
49

    
50
        public Coercion getCoercion() {
51
                return this.coercion;
52
        }
53

    
54
        public Class getDefaultClass() {
55
                return this.defaultClass;
56
        }
57

    
58
        public String getSubtype() {
59
                return this.subtype;
60
        }
61

    
62
        public int getType() {
63
                return this.type;
64
        }
65

    
66
        public String getName() {
67
                return this.name;
68
        }
69

    
70
        public boolean isContainer() {
71
                return (type & DataTypes.CONTAINER) == DataTypes.CONTAINER;
72
        }
73

    
74
        public boolean isObject() {
75
                return (type & DataTypes.OBJECT) == DataTypes.OBJECT;
76
        }
77

    
78
        public boolean isDynObject() {
79
                return type == DataTypes.DYNOBJECT;
80
        }
81

    
82
        public void setCoercion(Coercion coercion) {
83
                this.coercion = coercion;
84
                LOG.trace("Add coercion operation for data type {}.", new Object[] { name });
85
        }
86

    
87
        public void addCoercion(Coercion coercion) {
88
                LinkedCoercions coercions = null;
89
                if (this.coercion instanceof LinkedCoercions) {
90
                        coercions = (LinkedCoercions) this.coercion;
91
                } else {
92
                        coercions = new LinkedCoercions();
93
                        coercions.add(this.coercion);
94
                        this.coercion = coercions;
95
                }
96
                coercions.add(coercion);
97
                LOG.trace("Add coercion operation for data type {}.", new Object[] { name });
98
        }
99

    
100
        public String toString() {
101
                return MessageFormat.format(
102
                                "type=0x{0};subtype={1};name={2};class={3};coercion={4}", 
103
                                new Object[] {
104
                                        Integer.toHexString(type).toUpperCase(),
105
                                        subtype,
106
                                        name,
107
                                        defaultClass==null? null:defaultClass.getName(),
108
                                        coercion==null? null:coercion.getClass().getName()
109
                                }
110
                );
111
        }
112

    
113
        public boolean isNumeric() {
114
                if( type == DataTypes.DOUBLE ||
115
                        type == DataTypes.FLOAT ||
116
                    type == DataTypes.INT ||
117
                        type == DataTypes.LONG){
118
                        return true;
119
                }
120
                return false;
121
        }
122

    
123
        public class LinkedCoercions implements Coercion {
124
                
125
                public class LinkedCoercion {
126
                        Coercion coercion;
127
                        LinkedCoercion theNext;
128
                        
129
                        LinkedCoercion(Coercion coercion, LinkedCoercion next) {
130
                                this.theNext = next; 
131
                                this.coercion = coercion;
132
                        }
133
                        
134
                        public Object coerce(Object value) throws CoercionException {
135
                                return this.coercion.coerce(value);
136
                        }
137
                        
138
                        public boolean is(Coercion coercion) {
139
                                return coercion.getClass().getName().equals(this.coercion.getClass().getName());
140
                        }
141
                        
142
                        public boolean hasNext() {
143
                                return this.theNext != null;
144
                        }
145
                        
146
                        public LinkedCoercion next() {
147
                                return this.theNext;
148
                        }
149
                }
150
                
151
                LinkedCoercion head = null;
152
                
153
                public LinkedCoercions() {
154
                        super();
155
                }
156
                
157
                public void add(Coercion coercion) {
158
                        if( !this.contains(coercion)) {
159
                                head = new LinkedCoercion(coercion, head);
160
                        }
161
                }
162

    
163
                public boolean contains(Coercion coercion) {
164
                        LinkedCoercion x = head;
165
                        while( x!=null ) {
166
                                if( x.is(coercion)) {
167
                                        return true;
168
                                }
169
                                x = x.next();
170
                        }
171
                        return false;
172
                }
173
                
174
                public Object coerce(Object value) throws CoercionException {
175
                        LinkedCoercion x = head;
176
                        while( x!=null ) {
177
                                try {
178
                                        return x.coerce(value);
179
                                } catch (CoercionException e) {
180
                                        x = x.next();
181
                                        if( x==null ) {
182
                                                throw e;
183
                                        }
184
                                }
185
                        }
186
                        throw new CoercionException();
187
                }
188

    
189
        }
190

    
191
        
192
}