Mercurial > projects > dwt2
comparison org.eclipse.core.databinding/src/org/eclipse/core/databinding/conversion/StringToNumberConverter.d @ 85:6be48cf9f95c
Work on databinding
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 18 Apr 2009 13:54:50 +0200 |
parents | 0a55d2d5a946 |
children | 9e0ab372d5d8 |
comparison
equal
deleted
inserted
replaced
84:fcf926c91ca4 | 85:6be48cf9f95c |
---|---|
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; | |
15 | 16 |
16 import java.math.BigDecimal; | 17 import java.math.BigDecimal; |
17 import java.math.BigInteger; | 18 import java.math.BigInteger; |
18 | 19 |
19 import org.eclipse.core.internal.databinding.conversion.StringToNumberParser; | 20 import org.eclipse.core.internal.databinding.conversion.StringToNumberParser; |
20 import org.eclipse.core.internal.databinding.conversion.StringToNumberParser.ParseResult; | |
21 import org.eclipse.core.internal.databinding.validation.NumberFormatConverter; | 21 import org.eclipse.core.internal.databinding.validation.NumberFormatConverter; |
22 | 22 |
23 import com.ibm.icu.text.NumberFormat; | 23 import com.ibm.icu.text.NumberFormat; |
24 | 24 |
25 /** | 25 /** |
27 * This class is thread safe. | 27 * This class is thread safe. |
28 * | 28 * |
29 * @since 1.0 | 29 * @since 1.0 |
30 */ | 30 */ |
31 public class StringToNumberConverter : NumberFormatConverter { | 31 public class StringToNumberConverter : NumberFormatConverter { |
32 private ClassInfo toType; | 32 private TypeInfo toType; |
33 /** | 33 /** |
34 * NumberFormat instance to use for conversion. Access must be synchronized. | 34 * NumberFormat instance to use for conversion. Access must be synchronized. |
35 */ | 35 */ |
36 private NumberFormat numberFormat; | 36 private NumberFormat numberFormat; |
37 | 37 |
47 private final Number max; | 47 private final Number max; |
48 | 48 |
49 /** | 49 /** |
50 * The boxed type of the toType; | 50 * The boxed type of the toType; |
51 */ | 51 */ |
52 private final ClassInfo boxedType; | 52 private final TypeInfo boxedType; |
53 | 53 |
54 private static final Integer MIN_INTEGER = new Integercast(Integer.MIN_VALUE); | 54 private static const Integer MIN_INTEGER; |
55 private static final Integer MAX_INTEGER = new Integercast(Integer.MAX_VALUE); | 55 private static const Integer MAX_INTEGER; |
56 | 56 |
57 private static final Double MIN_DOUBLE = new Double(-Double.MAX_VALUE); | 57 private static const Double MIN_DOUBLE; |
58 private static final Double MAX_DOUBLE = new Doublecast(Double.MAX_VALUE); | 58 private static const Double MAX_DOUBLE; |
59 | 59 |
60 private static final Long MIN_LONG = new Longcast(Long.MIN_VALUE); | 60 private static const Long MIN_LONG; |
61 private static final Long MAX_LONG = new Longcast(Long.MIN_VALUE); | 61 private static const Long MAX_LONG; |
62 | 62 |
63 private static final Float MIN_FLOAT = new Float(-Float.MAX_VALUE); | 63 private static const Float MIN_FLOAT; |
64 private static final Float MAX_FLOAT = new Floatcast(Float.MAX_VALUE); | 64 private static const Float MAX_FLOAT; |
65 | |
66 static this(){ | |
67 MIN_INTEGER = new Integer(Integer.MIN_VALUE); | |
68 MAX_INTEGER = new Integer(Integer.MAX_VALUE); | |
69 | |
70 MIN_DOUBLE = new Double(-Double.MAX_VALUE); | |
71 MAX_DOUBLE = new Double(Double.MAX_VALUE); | |
72 | |
73 MIN_LONG = new Long(Long.MIN_VALUE); | |
74 MAX_LONG = new Long(Long.MIN_VALUE); | |
75 | |
76 MIN_FLOAT = new Float(-Float.MAX_VALUE); | |
77 MAX_FLOAT = new Float(Float.MAX_VALUE); | |
78 } | |
65 | 79 |
66 /** | 80 /** |
67 * @param numberFormat | 81 * @param numberFormat |
68 * @param toType | 82 * @param toType |
69 * @param min | 83 * @param min |
74 * as BigInteger doesn't have bounds | 88 * as BigInteger doesn't have bounds |
75 * @param boxedType | 89 * @param boxedType |
76 * a convenience that allows for the checking against one type | 90 * a convenience that allows for the checking against one type |
77 * rather than boxed and unboxed types | 91 * rather than boxed and unboxed types |
78 */ | 92 */ |
79 private this(NumberFormat numberFormat, ClassInfo toType, | 93 private this(NumberFormat numberFormat, TypeInfo toType, |
80 Number min, Number max, ClassInfo boxedType) { | 94 Number min, Number max, TypeInfo boxedType) { |
81 super(String.classinfo, toType, numberFormat); | 95 super(typeid(StringCls), toType, numberFormat); |
82 | 96 |
83 this.toType = toType; | 97 this.toType = toType; |
84 this.numberFormat = numberFormat; | 98 this.numberFormat = numberFormat; |
85 this.min = min; | 99 this.min = min; |
86 this.max = max; | 100 this.max = max; |
98 * {@link #getToType() to type}. | 112 * {@link #getToType() to type}. |
99 * @throws IllegalArgumentException | 113 * @throws IllegalArgumentException |
100 * if conversion was not possible | 114 * if conversion was not possible |
101 */ | 115 */ |
102 public Object convert(Object fromObject) { | 116 public Object convert(Object fromObject) { |
103 ParseResult result = StringToNumberParser.parse(fromObject, | 117 StringToNumberParser.ParseResult result = StringToNumberParser.parse(fromObject, |
104 numberFormat, toType.isPrimitive()); | 118 numberFormat, isJavaPrimitive(toType)); |
105 | 119 |
106 if (result.getPosition() !is null) { | 120 if (result.getPosition() !is null) { |
107 // this shouldn't happen in the pipeline as validation should catch | 121 // this shouldn't happen in the pipeline as validation should catch |
108 // it but anyone can call convert so we should return a properly | 122 // it but anyone can call convert so we should return a properly |
109 // formatted message in an exception | 123 // formatted message in an exception |
110 throw new IllegalArgumentException(StringToNumberParser | 124 throw new IllegalArgumentException(StringToNumberParser |
111 .createParseErrorMessage(cast(String) fromObject, result | 125 .createParseErrorMessage(stringcast(fromObject), result |
112 .getPosition())); | 126 .getPosition())); |
113 } else if (result.getNumber() is null) { | 127 } else if (result.getNumber() is null) { |
114 // if an error didn't occur and the number is null then it's a boxed | 128 // if an error didn't occur and the number is null then it's a boxed |
115 // type and null should be returned | 129 // type and null should be returned |
116 return null; | 130 return null; |
119 /* | 133 /* |
120 * Technically the checks for ranges aren't needed here because the | 134 * Technically the checks for ranges aren't needed here because the |
121 * validator should have validated this already but we shouldn't assume | 135 * validator should have validated this already but we shouldn't assume |
122 * this has occurred. | 136 * this has occurred. |
123 */ | 137 */ |
124 if (Integer.classinfo.equals(boxedType)) { | 138 if (typeid(Integer) is (boxedType)) { |
125 if (StringToNumberParser.inIntegerRange(result.getNumber())) { | 139 if (StringToNumberParser.inIntegerRange(result.getNumber())) { |
126 return new Integer(result.getNumber().intValue()); | 140 return new Integer(result.getNumber().intValue()); |
127 } | 141 } |
128 } else if (Double.classinfo.equals(boxedType)) { | 142 } else if (typeid(Double) is (boxedType)) { |
129 if (StringToNumberParser.inDoubleRange(result.getNumber())) { | 143 if (StringToNumberParser.inDoubleRange(result.getNumber())) { |
130 return new Double(result.getNumber().doubleValue()); | 144 return new Double(result.getNumber().doubleValue()); |
131 } | 145 } |
132 } else if (Long.classinfo.equals(boxedType)) { | 146 } else if (typeid(Long) is (boxedType)) { |
133 if (StringToNumberParser.inLongRange(result.getNumber())) { | 147 if (StringToNumberParser.inLongRange(result.getNumber())) { |
134 return new Long(result.getNumber().longValue()); | 148 return new Long(result.getNumber().longValue()); |
135 } | 149 } |
136 } else if (Float.classinfo.equals(boxedType)) { | 150 } else if (typeid(Float) is (boxedType)) { |
137 if (StringToNumberParser.inFloatRange(result.getNumber())) { | 151 if (StringToNumberParser.inFloatRange(result.getNumber())) { |
138 return new Float(result.getNumber().floatValue()); | 152 return new Float(result.getNumber().floatValue()); |
139 } | 153 } |
140 } else if (BigInteger.classinfo.equals(boxedType)) { | 154 } else if (typeid(BigInteger) is (boxedType)) { |
141 return (new BigDecimal(result.getNumber().doubleValue())) | 155 return (new BigDecimal(result.getNumber().doubleValue())) |
142 .toBigInteger(); | 156 .toBigInteger(); |
143 } | 157 } |
144 | 158 |
145 if (min !is null && max !is null) { | 159 if (min !is null && max !is null) { |
150 /* | 164 /* |
151 * Fail safe. I don't think this could even be thrown but throwing the | 165 * Fail safe. I don't think this could even be thrown but throwing the |
152 * exception is better than returning null and hiding the error. | 166 * exception is better than returning null and hiding the error. |
153 */ | 167 */ |
154 throw new IllegalArgumentException( | 168 throw new IllegalArgumentException( |
155 "Could not convert [" + fromObject + "] to type [" + toType + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | 169 Format("Could not convert [{}] to type [{}]", fromObject, toType)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
156 } | 170 } |
157 | 171 |
158 /** | 172 /** |
159 * @param primitive | 173 * @param primitive |
160 * <code>true</code> if the convert to type is an int | 174 * <code>true</code> if the convert to type is an int |
170 * @return to Integer converter with the provided numberFormat | 184 * @return to Integer converter with the provided numberFormat |
171 */ | 185 */ |
172 public static StringToNumberConverter toInteger(NumberFormat numberFormat, | 186 public static StringToNumberConverter toInteger(NumberFormat numberFormat, |
173 bool primitive) { | 187 bool primitive) { |
174 return new StringToNumberConverter(numberFormat, | 188 return new StringToNumberConverter(numberFormat, |
175 (primitive) ? Integer.TYPE : Integer.classinfo, MIN_INTEGER, | 189 (primitive) ? Integer.TYPE : typeid(Integer), MIN_INTEGER, |
176 MAX_INTEGER, Integer.classinfo); | 190 MAX_INTEGER, typeid(Integer)); |
177 } | 191 } |
178 | 192 |
179 /** | 193 /** |
180 * @param primitive | 194 * @param primitive |
181 * <code>true</code> if the convert to type is a double | 195 * <code>true</code> if the convert to type is a double |
191 * @return to Double converter with the provided numberFormat | 205 * @return to Double converter with the provided numberFormat |
192 */ | 206 */ |
193 public static StringToNumberConverter toDouble(NumberFormat numberFormat, | 207 public static StringToNumberConverter toDouble(NumberFormat numberFormat, |
194 bool primitive) { | 208 bool primitive) { |
195 return new StringToNumberConverter(numberFormat, | 209 return new StringToNumberConverter(numberFormat, |
196 (primitive) ? Double.TYPE : Double.classinfo, MIN_DOUBLE, | 210 (primitive) ? Double.TYPE : typeid(Double), MIN_DOUBLE, |
197 MAX_DOUBLE, Double.classinfo); | 211 MAX_DOUBLE, typeid(Double)); |
198 } | 212 } |
199 | 213 |
200 /** | 214 /** |
201 * @param primitive | 215 * @param primitive |
202 * <code>true</code> if the convert to type is a long | 216 * <code>true</code> if the convert to type is a long |
212 * @return to Long converter with the provided numberFormat | 226 * @return to Long converter with the provided numberFormat |
213 */ | 227 */ |
214 public static StringToNumberConverter toLong(NumberFormat numberFormat, | 228 public static StringToNumberConverter toLong(NumberFormat numberFormat, |
215 bool primitive) { | 229 bool primitive) { |
216 return new StringToNumberConverter(numberFormat, | 230 return new StringToNumberConverter(numberFormat, |
217 (primitive) ? Long.TYPE : Long.classinfo, MIN_LONG, MAX_LONG, | 231 (primitive) ? Long.TYPE : typeid(Long), MIN_LONG, MAX_LONG, |
218 Long.classinfo); | 232 typeid(Long)); |
219 } | 233 } |
220 | 234 |
221 /** | 235 /** |
222 * @param primitive | 236 * @param primitive |
223 * <code>true</code> if the convert to type is a float | 237 * <code>true</code> if the convert to type is a float |
233 * @return to Float converter with the provided numberFormat | 247 * @return to Float converter with the provided numberFormat |
234 */ | 248 */ |
235 public static StringToNumberConverter toFloat(NumberFormat numberFormat, | 249 public static StringToNumberConverter toFloat(NumberFormat numberFormat, |
236 bool primitive) { | 250 bool primitive) { |
237 return new StringToNumberConverter(numberFormat, | 251 return new StringToNumberConverter(numberFormat, |
238 (primitive) ? Float.TYPE : Float.classinfo, MIN_FLOAT, MAX_FLOAT, | 252 (primitive) ? Float.TYPE : typeid(Float), MIN_FLOAT, MAX_FLOAT, |
239 Float.classinfo); | 253 typeid(Float)); |
240 } | 254 } |
241 | 255 |
242 /** | 256 /** |
243 * @return to BigInteger converter for the default locale | 257 * @return to BigInteger converter for the default locale |
244 */ | 258 */ |
249 /** | 263 /** |
250 * @param numberFormat | 264 * @param numberFormat |
251 * @return to BigInteger converter with the provided numberFormat | 265 * @return to BigInteger converter with the provided numberFormat |
252 */ | 266 */ |
253 public static StringToNumberConverter toBigInteger(NumberFormat numberFormat) { | 267 public static StringToNumberConverter toBigInteger(NumberFormat numberFormat) { |
254 return new StringToNumberConverter(numberFormat, BigInteger.classinfo, | 268 return new StringToNumberConverter(numberFormat, typeid(BigInteger), |
255 null, null, BigInteger.classinfo); | 269 null, null, typeid(BigInteger)); |
256 } | 270 } |
257 } | 271 } |