gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dataTypes / impl / DefaultDataType.java @ 1844
History | View | Annotate | Download (6.07 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 |
|
48 |
DefaultDataType(int type, String subtype, String name, Class defaultClass, Coercion coercion) { |
49 |
|
50 |
if( name == null ) { |
51 |
LOG.trace("Can't register null type name for type {}.", new Object[] { Integer.toHexString(type).toUpperCase()}); |
52 |
throw new IllegalArgumentException(); |
53 |
} |
54 |
this.type = type;
|
55 |
this.subtype = subtype;
|
56 |
this.name = name;
|
57 |
this.defaultClass = defaultClass;
|
58 |
this.coercion = coercion;
|
59 |
} |
60 |
|
61 |
public Object coerce(Object value) throws CoercionException { |
62 |
// http://en.wikipedia.org/wiki/Type_conversion#Implicit_type_conversion
|
63 |
|
64 |
if( this.coercion != null ) { |
65 |
return this.coercion.coerce(value); |
66 |
} |
67 |
if( defaultClass == null ) { |
68 |
return value; // �? |
69 |
} |
70 |
if( defaultClass.isInstance(value) ) {
|
71 |
return value;
|
72 |
} |
73 |
throw new CoercionException(); |
74 |
} |
75 |
|
76 |
public Coercion getCoercion() {
|
77 |
return this.coercion; |
78 |
} |
79 |
|
80 |
public Class getDefaultClass() { |
81 |
return this.defaultClass; |
82 |
} |
83 |
|
84 |
public String getSubtype() { |
85 |
return this.subtype; |
86 |
} |
87 |
|
88 |
public int getType() { |
89 |
return this.type; |
90 |
} |
91 |
|
92 |
public String getName() { |
93 |
return this.name; |
94 |
} |
95 |
|
96 |
public boolean isContainer() { |
97 |
return (type & DataTypes.CONTAINER) == DataTypes.CONTAINER;
|
98 |
} |
99 |
|
100 |
public boolean isObject() { |
101 |
return (type & DataTypes.OBJECT) == DataTypes.OBJECT;
|
102 |
} |
103 |
|
104 |
public boolean isDynObject() { |
105 |
return type == DataTypes.DYNOBJECT;
|
106 |
} |
107 |
|
108 |
public void setCoercion(Coercion coercion) { |
109 |
this.coercion = coercion;
|
110 |
LOG.trace("Add coercion operation for data type {}.", new Object[] { name }); |
111 |
} |
112 |
|
113 |
public void addCoercion(Coercion coercion) { |
114 |
if( this.coercion==null ) { |
115 |
this.setCoercion(coercion);
|
116 |
return;
|
117 |
} |
118 |
LinkedCoercions coercions = null;
|
119 |
if (this.coercion instanceof LinkedCoercions) { |
120 |
coercions = (LinkedCoercions) this.coercion;
|
121 |
} else {
|
122 |
coercions = new LinkedCoercions();
|
123 |
coercions.add(this.coercion);
|
124 |
this.coercion = coercions;
|
125 |
} |
126 |
coercions.add(coercion); |
127 |
LOG.trace("Add coercion operation for data type {}.", new Object[] { name }); |
128 |
} |
129 |
|
130 |
public String toString() { |
131 |
return MessageFormat.format( |
132 |
"type=0x{0};subtype={1};name={2};class={3};coercion={4}",
|
133 |
new Object[] { |
134 |
Integer.toHexString(type).toUpperCase(),
|
135 |
subtype, |
136 |
name, |
137 |
defaultClass==null? null:defaultClass.getName(), |
138 |
coercion==null? null:coercion.getClass().getName() |
139 |
} |
140 |
); |
141 |
} |
142 |
|
143 |
public boolean isNumeric() { |
144 |
if( type == DataTypes.DOUBLE ||
|
145 |
type == DataTypes.FLOAT || |
146 |
type == DataTypes.INT || |
147 |
type == DataTypes.LONG){ |
148 |
return true; |
149 |
} |
150 |
return false; |
151 |
} |
152 |
|
153 |
public class LinkedCoercions implements DataTypesManager.CoercionWithLocale { |
154 |
|
155 |
public class LinkedCoercion { |
156 |
Coercion coercion; |
157 |
LinkedCoercion theNext; |
158 |
|
159 |
LinkedCoercion(Coercion coercion, LinkedCoercion next) { |
160 |
this.theNext = next;
|
161 |
this.coercion = coercion;
|
162 |
} |
163 |
|
164 |
public Object coerce(Object value) throws CoercionException { |
165 |
return this.coercion.coerce(value); |
166 |
} |
167 |
|
168 |
public boolean is(Coercion coercion) { |
169 |
return coercion.getClass().getName().equals(this.coercion.getClass().getName()); |
170 |
} |
171 |
|
172 |
public boolean hasNext() { |
173 |
return this.theNext != null; |
174 |
} |
175 |
|
176 |
public LinkedCoercion next() {
|
177 |
return this.theNext; |
178 |
} |
179 |
} |
180 |
|
181 |
LinkedCoercion head = null;
|
182 |
|
183 |
public LinkedCoercions() {
|
184 |
super();
|
185 |
} |
186 |
|
187 |
public void add(Coercion coercion) { |
188 |
if( !this.contains(coercion)) { |
189 |
head = new LinkedCoercion(coercion, head);
|
190 |
} |
191 |
} |
192 |
|
193 |
public boolean contains(Coercion coercion) { |
194 |
LinkedCoercion x = head; |
195 |
while( x!=null ) { |
196 |
if( x.is(coercion)) {
|
197 |
return true; |
198 |
} |
199 |
x = x.next(); |
200 |
} |
201 |
return false; |
202 |
} |
203 |
@Override
|
204 |
public Object coerce(Object value) throws CoercionException { |
205 |
return coerce(value, null); |
206 |
} |
207 |
|
208 |
@Override
|
209 |
public Object coerce(Object value, Locale locale) throws CoercionException { |
210 |
LinkedCoercion x = head; |
211 |
while( x!=null ) { |
212 |
try {
|
213 |
if (x instanceof CoercionWithLocale) { |
214 |
return ((CoercionWithLocale) x).coerce(value, locale);
|
215 |
} |
216 |
return x.coerce(value);
|
217 |
} catch (CoercionException e) {
|
218 |
x = x.next(); |
219 |
if( x==null ) { |
220 |
throw e;
|
221 |
} |
222 |
} |
223 |
} |
224 |
throw new CoercionException(); |
225 |
} |
226 |
|
227 |
} |
228 |
|
229 |
|
230 |
} |