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 | 802 | cordinyana | /**
|
---|---|---|---|
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 | 145 | jjdelcerro | package org.gvsig.tools.dataTypes.impl; |
25 | |||
26 | import java.text.MessageFormat; |
||
27 | 1844 | omartinez | import java.util.Locale; |
28 | 145 | jjdelcerro | |
29 | import org.gvsig.tools.dataTypes.CoercionException; |
||
30 | import org.gvsig.tools.dataTypes.DataType; |
||
31 | import org.gvsig.tools.dataTypes.DataTypes; |
||
32 | 1844 | omartinez | import org.gvsig.tools.dataTypes.DataTypesManager; |
33 | 145 | jjdelcerro | import org.gvsig.tools.dataTypes.DataTypesManager.Coercion; |
34 | 1844 | omartinez | import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale; |
35 | 697 | jjdelcerro | import org.slf4j.Logger; |
36 | import org.slf4j.LoggerFactory; |
||
37 | 145 | jjdelcerro | |
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 | 1886 | jjdelcerro | 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 | 145 | jjdelcerro | |
53 | 1886 | jjdelcerro | DefaultDataType(int type, String subtype, String name, Class defaultClass, Coercion coercion, String iconName) { |
54 | 145 | jjdelcerro | |
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 | 1886 | jjdelcerro | this.iconName = iconName==null? "datatype-any":iconName; |
65 | 145 | jjdelcerro | } |
66 | 2029 | jjdelcerro | |
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 | 145 | jjdelcerro | // 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 | 697 | jjdelcerro | return value; // �? |
86 | 145 | jjdelcerro | } |
87 | if( defaultClass.isInstance(value) ) {
|
||
88 | return value;
|
||
89 | } |
||
90 | throw new CoercionException(); |
||
91 | } |
||
92 | |||
93 | 2029 | jjdelcerro | @Override
|
94 | 145 | jjdelcerro | public Coercion getCoercion() {
|
95 | return this.coercion; |
||
96 | } |
||
97 | |||
98 | 2029 | jjdelcerro | @Override
|
99 | 145 | jjdelcerro | public Class getDefaultClass() { |
100 | return this.defaultClass; |
||
101 | } |
||
102 | |||
103 | 2029 | jjdelcerro | @Override
|
104 | 145 | jjdelcerro | public String getSubtype() { |
105 | return this.subtype; |
||
106 | } |
||
107 | |||
108 | 2029 | jjdelcerro | @Override
|
109 | 145 | jjdelcerro | public int getType() { |
110 | return this.type; |
||
111 | } |
||
112 | |||
113 | 2029 | jjdelcerro | @Override
|
114 | 145 | jjdelcerro | public String getName() { |
115 | return this.name; |
||
116 | } |
||
117 | |||
118 | 1886 | jjdelcerro | @Override
|
119 | public String getIconName() { |
||
120 | return this.iconName; |
||
121 | } |
||
122 | |||
123 | 2029 | jjdelcerro | @Override
|
124 | 1886 | jjdelcerro | public boolean isContainer() { |
125 | 145 | jjdelcerro | return (type & DataTypes.CONTAINER) == DataTypes.CONTAINER;
|
126 | } |
||
127 | |||
128 | 2029 | jjdelcerro | @Override
|
129 | 145 | jjdelcerro | public boolean isObject() { |
130 | return (type & DataTypes.OBJECT) == DataTypes.OBJECT;
|
||
131 | } |
||
132 | |||
133 | 2029 | jjdelcerro | @Override
|
134 | 630 | cmartin | public boolean isDynObject() { |
135 | return type == DataTypes.DYNOBJECT;
|
||
136 | } |
||
137 | |||
138 | 2029 | jjdelcerro | @Override
|
139 | 145 | jjdelcerro | public void setCoercion(Coercion coercion) { |
140 | this.coercion = coercion;
|
||
141 | LOG.trace("Add coercion operation for data type {}.", new Object[] { name }); |
||
142 | } |
||
143 | |||
144 | 2029 | jjdelcerro | @Override
|
145 | 697 | jjdelcerro | public void addCoercion(Coercion coercion) { |
146 | 1836 | jjdelcerro | if( this.coercion==null ) { |
147 | this.setCoercion(coercion);
|
||
148 | return;
|
||
149 | } |
||
150 | 2029 | jjdelcerro | LinkedCoercions coercions; |
151 | 725 | jldominguez | if (this.coercion instanceof LinkedCoercions) { |
152 | 697 | jjdelcerro | 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 | 2029 | jjdelcerro | @Override
|
163 | 145 | jjdelcerro | 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 | 216 | jjdelcerro | |
176 | 2029 | jjdelcerro | @Override
|
177 | 216 | jjdelcerro | public boolean isNumeric() { |
178 | if( type == DataTypes.DOUBLE ||
|
||
179 | 2029 | jjdelcerro | type == DataTypes.FLOAT || |
180 | 216 | jjdelcerro | type == DataTypes.INT || |
181 | 2029 | jjdelcerro | type == DataTypes.LONG){ |
182 | return true; |
||
183 | 216 | jjdelcerro | } |
184 | return false; |
||
185 | } |
||
186 | |||
187 | 1844 | omartinez | public class LinkedCoercions implements DataTypesManager.CoercionWithLocale { |
188 | |||
189 | 697 | jjdelcerro | 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 | 1844 | omartinez | @Override
|
238 | 697 | jjdelcerro | public Object coerce(Object value) throws CoercionException { |
239 | 1844 | omartinez | return coerce(value, null); |
240 | } |
||
241 | |||
242 | @Override
|
||
243 | public Object coerce(Object value, Locale locale) throws CoercionException { |
||
244 | 697 | jjdelcerro | LinkedCoercion x = head; |
245 | while( x!=null ) { |
||
246 | try {
|
||
247 | 1844 | omartinez | if (x instanceof CoercionWithLocale) { |
248 | return ((CoercionWithLocale) x).coerce(value, locale);
|
||
249 | } |
||
250 | return x.coerce(value);
|
||
251 | 697 | jjdelcerro | } catch (CoercionException e) {
|
252 | 1844 | omartinez | x = x.next(); |
253 | if( x==null ) { |
||
254 | throw e;
|
||
255 | } |
||
256 | 697 | jjdelcerro | } |
257 | } |
||
258 | throw new CoercionException(); |
||
259 | } |
||
260 | |||
261 | } |
||
262 | |||
263 | |||
264 | 145 | jjdelcerro | } |