Mercurial > projects > dwt2
comparison org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToNumberParser.d @ 85:6be48cf9f95c
Work on databinding
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 18 Apr 2009 13:54:50 +0200 |
parents | 0a55d2d5a946 |
children |
comparison
equal
deleted
inserted
replaced
84:fcf926c91ca4 | 85:6be48cf9f95c |
---|---|
25 * Utility class for the parsing of strings to numbers. | 25 * Utility class for the parsing of strings to numbers. |
26 * | 26 * |
27 * @since 1.0 | 27 * @since 1.0 |
28 */ | 28 */ |
29 public class StringToNumberParser { | 29 public class StringToNumberParser { |
30 private static final BigDecimal FLOAT_MAX_BIG_DECIMAL = new BigDecimal( | 30 private static BigDecimal FLOAT_MAX_BIG_DECIMAL; |
31 Float.MAX_VALUE); | 31 private static BigDecimal FLOAT_MIN_BIG_DECIMAL; |
32 private static final BigDecimal FLOAT_MIN_BIG_DECIMAL = new BigDecimal( | 32 private static BigDecimal DOUBLE_MAX_BIG_DECIMAL; |
33 -Float.MAX_VALUE); | 33 private static BigDecimal DOUBLE_MIN_BIG_DECIMAL; |
34 | 34 static this(){ |
35 private static final BigDecimal DOUBLE_MAX_BIG_DECIMAL = new BigDecimal( | 35 FLOAT_MAX_BIG_DECIMAL = new BigDecimal( |
36 Double.MAX_VALUE); | 36 Float.MAX_VALUE); |
37 private static final BigDecimal DOUBLE_MIN_BIG_DECIMAL = new BigDecimal( | 37 FLOAT_MIN_BIG_DECIMAL = new BigDecimal( |
38 -Double.MAX_VALUE); | 38 -Float.MAX_VALUE); |
39 | |
40 DOUBLE_MAX_BIG_DECIMAL = new BigDecimal( | |
41 Double.MAX_VALUE); | |
42 DOUBLE_MIN_BIG_DECIMAL = new BigDecimal( | |
43 -Double.MAX_VALUE); | |
44 } | |
39 | 45 |
40 /** | 46 /** |
41 * @param value | 47 * @param value |
42 * @param numberFormat | 48 * @param numberFormat |
43 * @param primitive | 49 * @param primitive |
44 * @return result | 50 * @return result |
45 */ | 51 */ |
46 public static ParseResult parse(Object value, NumberFormat numberFormat, | 52 public static ParseResult parse(Object value, NumberFormat numberFormat, |
47 bool primitive) { | 53 bool primitive) { |
48 if (!( null !is cast(String)value )) { | 54 if (!( null !is cast(ArrayWrapperString)value )) { |
49 throw new IllegalArgumentException( | 55 throw new IllegalArgumentException( |
50 "Value to convert is not a String"); //$NON-NLS-1$ | 56 "Value to convert is not a String"); //$NON-NLS-1$ |
51 } | 57 } |
52 | 58 |
53 String source = cast(String) value; | 59 String source = stringcast( value ); |
54 ParseResult result = new ParseResult(); | 60 ParseResult result = new ParseResult(); |
55 if (!primitive && source.trim().length() is 0) { | 61 if (!primitive && source.trim().length() is 0) { |
56 return result; | 62 return result; |
57 } | 63 } |
58 | 64 |
142 min = numberFormat.format(minValue); | 148 min = numberFormat.format(minValue); |
143 max = numberFormat.format(maxValue); | 149 max = numberFormat.format(maxValue); |
144 } | 150 } |
145 | 151 |
146 return BindingMessages.formatString( | 152 return BindingMessages.formatString( |
147 "Validate_NumberOutOfRangeError", [ cast(Object)min, max ]); //$NON-NLS-1$ | 153 "Validate_NumberOutOfRangeError", [ cast(Object)stringcast(min), stringcast(max) ]); //$NON-NLS-1$ |
148 } | 154 } |
149 | 155 |
150 /** | 156 /** |
151 * Returns <code>true</code> if the provided <code>number</code> is in | 157 * Returns <code>true</code> if the provided <code>number</code> is in |
152 * the range of a integer. | 158 * the range of a integer. |
203 if (bigInteger !is null) { | 209 if (bigInteger !is null) { |
204 return bigInteger.bitLength() <= bitLength; | 210 return bigInteger.bitLength() <= bitLength; |
205 } | 211 } |
206 | 212 |
207 throw new IllegalArgumentException( | 213 throw new IllegalArgumentException( |
208 "Number of type [" + number.getClass().getName() + "] is not supported."); //$NON-NLS-1$ //$NON-NLS-2$ | 214 Format("Number of type [{}] is not supported.", number.classinfo.name )); //$NON-NLS-1$ //$NON-NLS-2$ |
209 } | 215 } |
210 | 216 |
211 /** | 217 /** |
212 * Returns <code>true</code> if the provided <code>number</code> is in | 218 * Returns <code>true</code> if the provided <code>number</code> is in |
213 * the range of a long. | 219 * the range of a long. |
271 return max.compareTo(bigDecimal) >= 0 | 277 return max.compareTo(bigDecimal) >= 0 |
272 && min.compareTo(bigDecimal) <= 0; | 278 && min.compareTo(bigDecimal) <= 0; |
273 } | 279 } |
274 | 280 |
275 throw new IllegalArgumentException( | 281 throw new IllegalArgumentException( |
276 "Number of type [" + number.getClass().getName() + "] is not supported."); //$NON-NLS-1$ //$NON-NLS-2$ | 282 Format("Number of type [{}] is not supported.", number.classinfo.name)); //$NON-NLS-1$ //$NON-NLS-2$ |
277 } | 283 } |
278 | 284 |
279 /** | 285 /** |
280 * Returns <code>true</code> if the provided <code>number</code> is in | 286 * Returns <code>true</code> if the provided <code>number</code> is in |
281 * the range of a double. | 287 * the range of a double. |