Revision 2077

View differences:

org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/DefaultDataType.java
24 24
package org.gvsig.tools.dataTypes.impl;
25 25

  
26 26
import java.text.MessageFormat;
27
import java.util.ArrayList;
28
import java.util.List;
27 29
import java.util.Locale;
28 30

  
29 31
import org.gvsig.tools.dataTypes.CoercionException;
30 32
import org.gvsig.tools.dataTypes.DataType;
31 33
import org.gvsig.tools.dataTypes.DataTypes;
32
import org.gvsig.tools.dataTypes.DataTypesManager;
33 34
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
34 35
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale;
35 36
import org.slf4j.Logger;
......
67 68
        @Override
68 69
        public DataType clone() throws CloneNotSupportedException {
69 70
            DefaultDataType other = (DefaultDataType) super.clone();
71
            if( other.coercion instanceof Coercions ) {
72
                this.coercion = ((Coercions)this.coercion).clone();
73
            }
70 74
            return other;
71 75
        }
72 76

  
......
153 157
                    this.setCoercion(coercion);
154 158
                    return;
155 159
                }
156
		LinkedCoercions coercions;
157
		if (this.coercion instanceof LinkedCoercions) {
158
			coercions = (LinkedCoercions) this.coercion;
160
		Coercions coercions;
161
		if (this.coercion instanceof Coercions) {
162
                    coercions = (Coercions) this.coercion;
159 163
		} else {
160
			coercions = new LinkedCoercions();
161
			coercions.add(this.coercion);
162
			this.coercion = coercions;
164
                    coercions = new Coercions();
165
                    coercions.add(this.coercion);
166
                    this.coercion = coercions;
163 167
		}
164 168
		coercions.add(coercion);
165 169
		LOG.trace("Add coercion operation for data type {}.", new Object[] { name });
......
190 194
		return false;
191 195
	}
192 196

  
193
	public class LinkedCoercions implements DataTypesManager.CoercionWithLocale {
194
	
195
		public class LinkedCoercion {
196
			Coercion coercion;
197
			LinkedCoercion theNext;
198
			
199
			LinkedCoercion(Coercion coercion, LinkedCoercion next) {
200
				this.theNext = next; 
201
				this.coercion = coercion;
202
			}
203
			
204
			public Object coerce(Object value) throws CoercionException {
205
				return this.coercion.coerce(value);
206
			}
207
			
208
			public boolean is(Coercion coercion) {
209
				return coercion.getClass().getName().equals(this.coercion.getClass().getName());
210
			}
211
			
212
			public boolean hasNext() {
213
				return this.theNext != null;
214
			}
215
			
216
			public LinkedCoercion next() {
217
				return this.theNext;
218
			}
219
		}
220
		
221
		LinkedCoercion head = null;
222
		
223
		public LinkedCoercions() {
224
			super();
225
		}
226
		
227
		public void add(Coercion coercion) {
228
			if( !this.contains(coercion)) {
229
				head = new LinkedCoercion(coercion, head);
230
			}
231
		}
197
        private static class Coercions 
198
                implements CoercionWithLocale, org.gvsig.tools.lang.Cloneable
199
            {
232 200

  
233
		public boolean contains(Coercion coercion) {
234
			LinkedCoercion x = head;
235
			while( x!=null ) {
236
				if( x.is(coercion)) {
237
					return true;
238
				}
239
				x = x.next();
240
			}
241
			return false;
242
		}
243
                @Override
244
		public Object coerce(Object value) throws CoercionException {
245
                    return coerce(value, null);
201
            private List<Coercion> coercions; 
202
            
203
            public Coercions() {
204
                this.coercions = new ArrayList<>();
205
            }
206
            
207
            public void add(Coercion coercion) {
208
                this.coercions.add(coercion);
209
            }
210

  
211
            @Override
212
            public Coercions clone() throws CloneNotSupportedException {
213
                Coercions other = (Coercions) super.clone();
214
                other.coercions = new ArrayList<>();
215
                other.coercions.addAll(this.coercions);
216
                return other;
217
            }
218
            
219
            @Override
220
            public Object coerce(Object value) throws CoercionException {
221
                return coerce(value, Locale.getDefault());
222
            }
223
            
224
            @Override
225
            public Object coerce(Object value, Locale locale) throws CoercionException {
226
                for (int i = this.coercions.size(); --i >= 0;) {
227
                    Coercion coercion = this.coercions.get(i);
228
                    try {
229
                        if (coercion instanceof CoercionWithLocale) {
230
                            return ((CoercionWithLocale) coercion).coerce(value, locale);
231
                        }
232
                        return coercion.coerce(value);
233
                    } catch (CoercionException e) {
234
                        // Do nothing, go to next coercion
235
                        coercion = null; // To allow set break point
236
                    }
246 237
                }
247
                
248
                @Override
249
		public Object coerce(Object value, Locale locale) throws CoercionException {
250
			LinkedCoercion x = head;
251
			while( x!=null ) {
252
				try {
253
                                    if (x instanceof CoercionWithLocale) {
254
					return ((CoercionWithLocale) x).coerce(value, locale);
255
                                    }
256
                                    return x.coerce(value);
257
				} catch (CoercionException e) {
258
                                    x = x.next();
259
                                    if( x==null ) {
260
                                        throw e;
261
                                    }
262
				}
263
			}
264
			throw new CoercionException();
265
		}
238
                throw new CoercionException();
239
            }
266 240

  
267
	}
268

  
241
        }
269 242
	
270 243
}

Also available in: Unified diff