Revision 697 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/DefaultDataType.java

View differences:

DefaultDataType.java
2 2

  
3 3
import java.text.MessageFormat;
4 4

  
5
import org.slf4j.Logger;
6
import org.slf4j.LoggerFactory;
7

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

  
13 12
public class DefaultDataType implements DataType {
14 13

  
......
40 39
			return this.coercion.coerce(value);
41 40
		}
42 41
		if( defaultClass == null ) {
43
			return value; // ??
42
			return value; // ?
44 43
		}
45 44
		if( defaultClass.isInstance(value) ) {
46 45
			return value;
......
85 84
		LOG.trace("Add coercion operation for data type {}.", new Object[] { name });
86 85
	}
87 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

  
88 100
	public String toString() {
89 101
		return MessageFormat.format(
90 102
				"type=0x{0};subtype={1};name={2};class={3};coercion={4}", 
......
108 120
		return false;
109 121
	}
110 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
	
111 192
}

Also available in: Unified diff