comparison org.eclipse.core.databinding/src/org/eclipse/core/databinding/conversion/StringToNumberConverter.d @ 88:9e0ab372d5d8

Revert from TypeInfo/ClassInfo to java.lang.Class
author Frank Benoit <benoit@tionex.de>
date Sun, 19 Apr 2009 11:10:09 +0200
parents 6be48cf9f95c
children
comparison
equal deleted inserted replaced
87:8594250b1d1c 88:9e0ab372d5d8
10 ******************************************************************************/ 10 ******************************************************************************/
11 11
12 module org.eclipse.core.databinding.conversion.StringToNumberConverter; 12 module org.eclipse.core.databinding.conversion.StringToNumberConverter;
13 13
14 import java.lang.all; 14 import java.lang.all;
15 import java.nonstandard.RuntimeTraits;
16 15
17 import java.math.BigDecimal; 16 import java.math.BigDecimal;
18 import java.math.BigInteger; 17 import java.math.BigInteger;
19 18
20 import org.eclipse.core.internal.databinding.conversion.StringToNumberParser; 19 import org.eclipse.core.internal.databinding.conversion.StringToNumberParser;
27 * This class is thread safe. 26 * This class is thread safe.
28 * 27 *
29 * @since 1.0 28 * @since 1.0
30 */ 29 */
31 public class StringToNumberConverter : NumberFormatConverter { 30 public class StringToNumberConverter : NumberFormatConverter {
32 private TypeInfo toType; 31 private Class toType;
33 /** 32 /**
34 * NumberFormat instance to use for conversion. Access must be synchronized. 33 * NumberFormat instance to use for conversion. Access must be synchronized.
35 */ 34 */
36 private NumberFormat numberFormat; 35 private NumberFormat numberFormat;
37 36
47 private final Number max; 46 private final Number max;
48 47
49 /** 48 /**
50 * The boxed type of the toType; 49 * The boxed type of the toType;
51 */ 50 */
52 private final TypeInfo boxedType; 51 private final Class boxedType;
53 52
54 private static const Integer MIN_INTEGER; 53 private static const Integer MIN_INTEGER;
55 private static const Integer MAX_INTEGER; 54 private static const Integer MAX_INTEGER;
56 55
57 private static const Double MIN_DOUBLE; 56 private static const Double MIN_DOUBLE;
88 * as BigInteger doesn't have bounds 87 * as BigInteger doesn't have bounds
89 * @param boxedType 88 * @param boxedType
90 * a convenience that allows for the checking against one type 89 * a convenience that allows for the checking against one type
91 * rather than boxed and unboxed types 90 * rather than boxed and unboxed types
92 */ 91 */
93 private this(NumberFormat numberFormat, TypeInfo toType, 92 private this(NumberFormat numberFormat, Class toType,
94 Number min, Number max, TypeInfo boxedType) { 93 Number min, Number max, Class boxedType) {
95 super(typeid(StringCls), toType, numberFormat); 94 super(Class.fromType!(StringCls), toType, numberFormat);
96 95
97 this.toType = toType; 96 this.toType = toType;
98 this.numberFormat = numberFormat; 97 this.numberFormat = numberFormat;
99 this.min = min; 98 this.min = min;
100 this.max = max; 99 this.max = max;
113 * @throws IllegalArgumentException 112 * @throws IllegalArgumentException
114 * if conversion was not possible 113 * if conversion was not possible
115 */ 114 */
116 public Object convert(Object fromObject) { 115 public Object convert(Object fromObject) {
117 StringToNumberParser.ParseResult result = StringToNumberParser.parse(fromObject, 116 StringToNumberParser.ParseResult result = StringToNumberParser.parse(fromObject,
118 numberFormat, isJavaPrimitive(toType)); 117 numberFormat, toType.isPrimitive());
119 118
120 if (result.getPosition() !is null) { 119 if (result.getPosition() !is null) {
121 // this shouldn't happen in the pipeline as validation should catch 120 // this shouldn't happen in the pipeline as validation should catch
122 // it but anyone can call convert so we should return a properly 121 // it but anyone can call convert so we should return a properly
123 // formatted message in an exception 122 // formatted message in an exception
133 /* 132 /*
134 * Technically the checks for ranges aren't needed here because the 133 * Technically the checks for ranges aren't needed here because the
135 * validator should have validated this already but we shouldn't assume 134 * validator should have validated this already but we shouldn't assume
136 * this has occurred. 135 * this has occurred.
137 */ 136 */
138 if (typeid(Integer) is (boxedType)) { 137 if (Class.fromType!(Integer) is boxedType) {
139 if (StringToNumberParser.inIntegerRange(result.getNumber())) { 138 if (StringToNumberParser.inIntegerRange(result.getNumber())) {
140 return new Integer(result.getNumber().intValue()); 139 return new Integer(result.getNumber().intValue());
141 } 140 }
142 } else if (typeid(Double) is (boxedType)) { 141 } else if (Class.fromType!(Double) is boxedType) {
143 if (StringToNumberParser.inDoubleRange(result.getNumber())) { 142 if (StringToNumberParser.inDoubleRange(result.getNumber())) {
144 return new Double(result.getNumber().doubleValue()); 143 return new Double(result.getNumber().doubleValue());
145 } 144 }
146 } else if (typeid(Long) is (boxedType)) { 145 } else if (Class.fromType!(Long) is boxedType) {
147 if (StringToNumberParser.inLongRange(result.getNumber())) { 146 if (StringToNumberParser.inLongRange(result.getNumber())) {
148 return new Long(result.getNumber().longValue()); 147 return new Long(result.getNumber().longValue());
149 } 148 }
150 } else if (typeid(Float) is (boxedType)) { 149 } else if (Class.fromType!(Float) is boxedType) {
151 if (StringToNumberParser.inFloatRange(result.getNumber())) { 150 if (StringToNumberParser.inFloatRange(result.getNumber())) {
152 return new Float(result.getNumber().floatValue()); 151 return new Float(result.getNumber().floatValue());
153 } 152 }
154 } else if (typeid(BigInteger) is (boxedType)) { 153 } else if (Class.fromType!(BigInteger) is boxedType) {
155 return (new BigDecimal(result.getNumber().doubleValue())) 154 return (new BigDecimal(result.getNumber().doubleValue()))
156 .toBigInteger(); 155 .toBigInteger();
157 } 156 }
158 157
159 if (min !is null && max !is null) { 158 if (min !is null && max !is null) {
184 * @return to Integer converter with the provided numberFormat 183 * @return to Integer converter with the provided numberFormat
185 */ 184 */
186 public static StringToNumberConverter toInteger(NumberFormat numberFormat, 185 public static StringToNumberConverter toInteger(NumberFormat numberFormat,
187 bool primitive) { 186 bool primitive) {
188 return new StringToNumberConverter(numberFormat, 187 return new StringToNumberConverter(numberFormat,
189 (primitive) ? Integer.TYPE : typeid(Integer), MIN_INTEGER, 188 (primitive) ? Integer.TYPE : Class.fromType!(Integer), MIN_INTEGER,
190 MAX_INTEGER, typeid(Integer)); 189 MAX_INTEGER, Class.fromType!(Integer));
191 } 190 }
192 191
193 /** 192 /**
194 * @param primitive 193 * @param primitive
195 * <code>true</code> if the convert to type is a double 194 * <code>true</code> if the convert to type is a double
205 * @return to Double converter with the provided numberFormat 204 * @return to Double converter with the provided numberFormat
206 */ 205 */
207 public static StringToNumberConverter toDouble(NumberFormat numberFormat, 206 public static StringToNumberConverter toDouble(NumberFormat numberFormat,
208 bool primitive) { 207 bool primitive) {
209 return new StringToNumberConverter(numberFormat, 208 return new StringToNumberConverter(numberFormat,
210 (primitive) ? Double.TYPE : typeid(Double), MIN_DOUBLE, 209 (primitive) ? Double.TYPE : Class.fromType!(Double), MIN_DOUBLE,
211 MAX_DOUBLE, typeid(Double)); 210 MAX_DOUBLE, Class.fromType!(Double));
212 } 211 }
213 212
214 /** 213 /**
215 * @param primitive 214 * @param primitive
216 * <code>true</code> if the convert to type is a long 215 * <code>true</code> if the convert to type is a long
226 * @return to Long converter with the provided numberFormat 225 * @return to Long converter with the provided numberFormat
227 */ 226 */
228 public static StringToNumberConverter toLong(NumberFormat numberFormat, 227 public static StringToNumberConverter toLong(NumberFormat numberFormat,
229 bool primitive) { 228 bool primitive) {
230 return new StringToNumberConverter(numberFormat, 229 return new StringToNumberConverter(numberFormat,
231 (primitive) ? Long.TYPE : typeid(Long), MIN_LONG, MAX_LONG, 230 (primitive) ? Long.TYPE : Class.fromType!(Long), MIN_LONG, MAX_LONG,
232 typeid(Long)); 231 Class.fromType!(Long));
233 } 232 }
234 233
235 /** 234 /**
236 * @param primitive 235 * @param primitive
237 * <code>true</code> if the convert to type is a float 236 * <code>true</code> if the convert to type is a float
247 * @return to Float converter with the provided numberFormat 246 * @return to Float converter with the provided numberFormat
248 */ 247 */
249 public static StringToNumberConverter toFloat(NumberFormat numberFormat, 248 public static StringToNumberConverter toFloat(NumberFormat numberFormat,
250 bool primitive) { 249 bool primitive) {
251 return new StringToNumberConverter(numberFormat, 250 return new StringToNumberConverter(numberFormat,
252 (primitive) ? Float.TYPE : typeid(Float), MIN_FLOAT, MAX_FLOAT, 251 (primitive) ? Float.TYPE : Class.fromType!(Float), MIN_FLOAT, MAX_FLOAT,
253 typeid(Float)); 252 Class.fromType!(Float));
254 } 253 }
255 254
256 /** 255 /**
257 * @return to BigInteger converter for the default locale 256 * @return to BigInteger converter for the default locale
258 */ 257 */
263 /** 262 /**
264 * @param numberFormat 263 * @param numberFormat
265 * @return to BigInteger converter with the provided numberFormat 264 * @return to BigInteger converter with the provided numberFormat
266 */ 265 */
267 public static StringToNumberConverter toBigInteger(NumberFormat numberFormat) { 266 public static StringToNumberConverter toBigInteger(NumberFormat numberFormat) {
268 return new StringToNumberConverter(numberFormat, typeid(BigInteger), 267 return new StringToNumberConverter(numberFormat, Class.fromType!(BigInteger),
269 null, null, typeid(BigInteger)); 268 null, null, Class.fromType!(BigInteger));
270 } 269 }
271 } 270 }