Revision 2080 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
26 26
import java.text.MessageFormat;
27 27
import java.util.ArrayList;
28 28
import java.util.List;
29
import java.util.Locale;
30 29

  
31 30
import org.gvsig.tools.dataTypes.CoercionException;
32 31
import org.gvsig.tools.dataTypes.DataType;
33 32
import org.gvsig.tools.dataTypes.DataTypes;
34
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
35
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale;
33
import org.gvsig.tools.dataTypes.Coercion;
36 34
import org.slf4j.Logger;
37 35
import org.slf4j.LoggerFactory;
36
import org.gvsig.tools.dataTypes.CoercionContext;
38 37

  
39 38
public class DefaultDataType implements DataType {
40 39

  
41
	private static final Logger LOG = LoggerFactory.getLogger(DefaultDataTypesManager.class);
42
	
43
	private Coercion coercion;
44
	private Class defaultClass;
45
	private String subtype;
46
	private int type;
47
	private String name;
48
        private String iconName;
49
        
50
        DefaultDataType(int type, String subtype, String name, Class defaultClass, Coercion coercion) {
51
            this(type, subtype, name, defaultClass, coercion, "datatype-any");
52
        }
53
	
54
        DefaultDataType(int type, String subtype, String name, Class defaultClass, Coercion coercion, String iconName) {
40
  private static final Logger LOG = LoggerFactory.getLogger(DefaultDataTypesManager.class);
55 41

  
56
		if( name == null ) {
57
			LOG.trace("Can't register null type name for type {}.", new Object[] { Integer.toHexString(type).toUpperCase()});
58
			throw new IllegalArgumentException();
59
		}
60
		this.type = type;
61
		this.subtype = subtype;
62
		this.name = name;
63
		this.defaultClass  = defaultClass;
64
		this.coercion = coercion;
65
                this.iconName = iconName==null? "datatype-any":iconName;
66
	}
42
  private Coercion coercion;
43
  private Class defaultClass;
44
  private String subtype;
45
  private int type;
46
  private String name;
47
  private String iconName;
67 48

  
68
        @Override
69
        public DataType clone() throws CloneNotSupportedException {
70
            DefaultDataType other = (DefaultDataType) super.clone();
71
            if( other.coercion instanceof Coercions ) {
72
                this.coercion = ((Coercions)this.coercion).clone();
73
            }
74
            return other;
75
        }
49
  DefaultDataType(int type, String subtype, String name, Class defaultClass, Coercion coercion) {
50
    this(type, subtype, name, defaultClass, coercion, "datatype-any");
51
  }
76 52

  
77
        @Override
78
        public String getLabel() {
79
            return this.getName();
80
        }
53
  DefaultDataType(int type, String subtype, String name, Class defaultClass, Coercion coercion, String iconName) {
81 54

  
82
        @Override
83
        public DataType getValue() {
84
            return this;
85
        }
86
    
87
        @Override
88
        public Object coerce(Object value) throws CoercionException {
89
		// http://en.wikipedia.org/wiki/Type_conversion#Implicit_type_conversion
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
    this.iconName = iconName == null ? "datatype-any" : iconName;
65
  }
90 66

  
91
		if( this.coercion != null ) {
92
			return this.coercion.coerce(value);
93
		}
94
		if( defaultClass == null ) {
95
			return value; // �?
96
		}
97
		if( defaultClass.isInstance(value) ) {
98
			return value;
99
		}
100
		throw new CoercionException();
101
	}
67
  @Override
68
  public DataType clone() throws CloneNotSupportedException {
69
    DefaultDataType other = (DefaultDataType) super.clone();
70
    if (other.coercion instanceof Coercions) {
71
      this.coercion = ((Coercions) this.coercion).clone();
72
    }
73
    return other;
74
  }
102 75

  
103
        @Override
104
	public Coercion getCoercion() {
105
		return this.coercion;
106
	}
76
  @Override
77
  public String getLabel() {
78
    return this.getName();
79
  }
107 80

  
108
        @Override
109
	public Class getDefaultClass() {
110
		return this.defaultClass;
111
	}
81
  @Override
82
  public DataType getValue() {
83
    return this;
84
  }
112 85

  
113
        @Override
114
	public String getSubtype() {
115
		return this.subtype;
116
	}
86
  @Override
87
  public Object coerce(Object value) throws CoercionException {
88
    // http://en.wikipedia.org/wiki/Type_conversion#Implicit_type_conversion
117 89

  
118
        @Override
119
	public int getType() {
120
		return this.type;
121
	}
90
    if (this.coercion != null) {
91
      return this.coercion.coerce(value);
92
    }
93
    if (defaultClass == null) {
94
      return value; // ??
95
    }
96
    if (defaultClass.isInstance(value)) {
97
      return value;
98
    }
99
    throw new CoercionException();
100
  }
122 101

  
123
        @Override
124
	public String getName() {
125
		return this.name;
126
	}
102
  @Override
103
  public Coercion getCoercion() {
104
    return this.coercion;
105
  }
127 106

  
128
        @Override
129
        public String getIconName() {
130
            return this.iconName;
131
        }
107
  @Override
108
  public Class getDefaultClass() {
109
    return this.defaultClass;
110
  }
132 111

  
133
        @Override
134
        public boolean isContainer() {
135
		return (type & DataTypes.CONTAINER) == DataTypes.CONTAINER;
136
	}
112
  @Override
113
  public String getSubtype() {
114
    return this.subtype;
115
  }
137 116

  
138
        @Override
139
	public boolean isObject() {
140
		return (type & DataTypes.OBJECT) == DataTypes.OBJECT;
141
	}
117
  @Override
118
  public int getType() {
119
    return this.type;
120
  }
142 121

  
143
        @Override
144
	public boolean isDynObject() {
145
		return type == DataTypes.DYNOBJECT;
146
	}
122
  @Override
123
  public String getName() {
124
    return this.name;
125
  }
147 126

  
148
        @Override
149
	public void setCoercion(Coercion coercion) {
150
		this.coercion = coercion;
151
		LOG.trace("Add coercion operation for data type {}.", new Object[] { name });
152
	}
127
  @Override
128
  public String getIconName() {
129
    return this.iconName;
130
  }
153 131

  
154
        @Override
155
	public void addCoercion(Coercion coercion) {
156
                if( this.coercion==null ) {
157
                    this.setCoercion(coercion);
158
                    return;
159
                }
160
		Coercions coercions;
161
		if (this.coercion instanceof Coercions) {
162
                    coercions = (Coercions) this.coercion;
163
		} else {
164
                    coercions = new Coercions();
165
                    coercions.add(this.coercion);
166
                    this.coercion = coercions;
167
		}
168
		coercions.add(coercion);
169
		LOG.trace("Add coercion operation for data type {}.", new Object[] { name });
170
	}
132
  @Override
133
  public boolean isContainer() {
134
    return (type & DataTypes.CONTAINER) == DataTypes.CONTAINER;
135
  }
171 136

  
172
        @Override
173
	public String toString() {
174
		return MessageFormat.format(
175
				"type=0x{0};subtype={1};name={2};class={3};coercion={4}", 
176
				new Object[] {
177
					Integer.toHexString(type).toUpperCase(),
178
					subtype,
179
					name,
180
					defaultClass==null? null:defaultClass.getName(),
181
					coercion==null? null:coercion.getClass().getName()
182
				}
183
		);
184
	}
137
  @Override
138
  public boolean isObject() {
139
    return (type & DataTypes.OBJECT) == DataTypes.OBJECT;
140
  }
185 141

  
186
        @Override
187
	public boolean isNumeric() {
188
		if( type == DataTypes.DOUBLE ||
189
                    type == DataTypes.FLOAT ||
190
		    type == DataTypes.INT ||
191
                    type == DataTypes.LONG){
192
                    return true;
193
		}
194
		return false;
195
	}
142
  @Override
143
  public boolean isDynObject() {
144
    return type == DataTypes.DYNOBJECT;
145
  }
196 146

  
197
        private static class Coercions 
198
                implements CoercionWithLocale, org.gvsig.tools.lang.Cloneable
199
            {
147
//  @Override
148
  public void setCoercion(Coercion coercion) {
149
    this.coercion = coercion;
150
    LOG.trace("Add coercion operation for data type {}.", new Object[]{name});
151
  }
200 152

  
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
            }
153
  @Override
154
  public void addCoercion(Coercion coercion) {
155
    if (this.coercion == null) {
156
      this.setCoercion(coercion);
157
      return;
158
    }
159
    Coercions coercions;
160
    if (this.coercion instanceof Coercions) {
161
      coercions = (Coercions) this.coercion;
162
    } else {
163
      coercions = new Coercions();
164
      coercions.add(this.coercion);
165
      this.coercion = coercions;
166
    }
167
    coercions.add(coercion);
168
    LOG.trace("Add coercion operation for data type {}.", new Object[]{name});
169
  }
210 170

  
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;
171
  @Override
172
  public String toString() {
173
    return MessageFormat.format(
174
            "type=0x{0};subtype={1};name={2};class={3};coercion={4}",
175
            new Object[]{
176
              Integer.toHexString(type).toUpperCase(),
177
              subtype,
178
              name,
179
              defaultClass == null ? null : defaultClass.getName(),
180
              coercion == null ? null : coercion.getClass().getName()
217 181
            }
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
                    }
237
                }
238
                throw new CoercionException();
239
            }
182
    );
183
  }
240 184

  
185
  @Override
186
  public boolean isNumeric() {
187
    if (type == DataTypes.DOUBLE
188
            || type == DataTypes.FLOAT
189
            || type == DataTypes.INT
190
            || type == DataTypes.LONG
191
            || type == DataTypes.DECIMAL) {
192
      return true;
193
    }
194
    return false;
195
  }
196

  
197
  private static class Coercions
198
          implements Coercion, org.gvsig.tools.lang.Cloneable {
199

  
200
    List<Coercion> coercions;
201

  
202
    public Coercions() {
203
      this.coercions = new ArrayList<>();
204
    }
205

  
206
    public void add(Coercion coercion) {
207
      this.coercions.add(coercion);
208
    }
209

  
210
    @Override
211
    public Coercions clone() throws CloneNotSupportedException {
212
      Coercions other = (Coercions) super.clone();
213
      other.coercions = new ArrayList<>();
214
      other.coercions.addAll(this.coercions);
215
      return other;
216
    }
217

  
218
    @Override
219
    public Object coerce(Object value) throws CoercionException {
220
      return coerce(value, null);
221
    }
222

  
223
    @Override
224
    @SuppressWarnings("UnusedAssignment")
225
    public Object coerce(Object value, CoercionContext context) throws CoercionException {
226
      for (int i = this.coercions.size(); --i >= 0;) {
227
        Coercion coercion = this.coercions.get(i);
228
        try {
229
          return coercion.coerce(value, context);
230
        } catch (CoercionException e) {
231
          // Do nothing, go to next coercion
232
          coercion = null; // To allow set break point
241 233
        }
242
	
234
      }
235
      throw new CoercionException();
236
    }
237

  
238
  }
239

  
243 240
}

Also available in: Unified diff