Mercurial > projects > dwt2
annotate 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 |
rev | line source |
---|---|
78 | 1 /******************************************************************************* |
2 * Copyright (c) 2007 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 ******************************************************************************/ | |
11 | |
12 module org.eclipse.core.databinding.conversion.StringToNumberConverter; | |
13 | |
14 import java.lang.all; | |
15 | |
16 import java.math.BigDecimal; | |
17 import java.math.BigInteger; | |
18 | |
19 import org.eclipse.core.internal.databinding.conversion.StringToNumberParser; | |
20 import org.eclipse.core.internal.databinding.validation.NumberFormatConverter; | |
21 | |
22 import com.ibm.icu.text.NumberFormat; | |
23 | |
24 /** | |
25 * Converts a String to a Number using <code>NumberFormat.parse(...)</code>. | |
26 * This class is thread safe. | |
27 * | |
28 * @since 1.0 | |
29 */ | |
30 public class StringToNumberConverter : NumberFormatConverter { | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
31 private Class toType; |
78 | 32 /** |
33 * NumberFormat instance to use for conversion. Access must be synchronized. | |
34 */ | |
35 private NumberFormat numberFormat; | |
36 | |
37 /** | |
38 * Minimum possible value for the type. Can be <code>null</code> as | |
39 * BigInteger doesn't have bounds. | |
40 */ | |
41 private final Number min; | |
42 /** | |
43 * Maximum possible value for the type. Can be <code>null</code> as | |
44 * BigInteger doesn't have bounds. | |
45 */ | |
46 private final Number max; | |
47 | |
48 /** | |
49 * The boxed type of the toType; | |
50 */ | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
51 private final Class boxedType; |
85 | 52 |
53 private static const Integer MIN_INTEGER; | |
54 private static const Integer MAX_INTEGER; | |
78 | 55 |
85 | 56 private static const Double MIN_DOUBLE; |
57 private static const Double MAX_DOUBLE; | |
58 | |
59 private static const Long MIN_LONG; | |
60 private static const Long MAX_LONG; | |
61 | |
62 private static const Float MIN_FLOAT; | |
63 private static const Float MAX_FLOAT; | |
78 | 64 |
85 | 65 static this(){ |
66 MIN_INTEGER = new Integer(Integer.MIN_VALUE); | |
67 MAX_INTEGER = new Integer(Integer.MAX_VALUE); | |
68 | |
69 MIN_DOUBLE = new Double(-Double.MAX_VALUE); | |
70 MAX_DOUBLE = new Double(Double.MAX_VALUE); | |
78 | 71 |
85 | 72 MIN_LONG = new Long(Long.MIN_VALUE); |
73 MAX_LONG = new Long(Long.MIN_VALUE); | |
78 | 74 |
85 | 75 MIN_FLOAT = new Float(-Float.MAX_VALUE); |
76 MAX_FLOAT = new Float(Float.MAX_VALUE); | |
77 } | |
78 | 78 |
79 /** | |
80 * @param numberFormat | |
81 * @param toType | |
82 * @param min | |
83 * minimum possible value for the type, can be <code>null</code> | |
84 * as BigInteger doesn't have bounds | |
85 * @param max | |
86 * maximum possible value for the type, can be <code>null</code> | |
87 * as BigInteger doesn't have bounds | |
88 * @param boxedType | |
89 * a convenience that allows for the checking against one type | |
90 * rather than boxed and unboxed types | |
91 */ | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
92 private this(NumberFormat numberFormat, Class toType, |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
93 Number min, Number max, Class boxedType) { |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
94 super(Class.fromType!(StringCls), toType, numberFormat); |
78 | 95 |
96 this.toType = toType; | |
97 this.numberFormat = numberFormat; | |
98 this.min = min; | |
99 this.max = max; | |
100 this.boxedType = boxedType; | |
101 } | |
102 | |
103 /** | |
104 * Converts the provided <code>fromObject</code> to the requested | |
105 * {@link #getToType() to type}. | |
106 * | |
107 * @see org.eclipse.core.databinding.conversion.IConverter#convert(java.lang.Object) | |
108 * @throws IllegalArgumentException | |
109 * if the value isn't in the format required by the NumberFormat | |
110 * or the value is out of range for the | |
111 * {@link #getToType() to type}. | |
112 * @throws IllegalArgumentException | |
113 * if conversion was not possible | |
114 */ | |
115 public Object convert(Object fromObject) { | |
85 | 116 StringToNumberParser.ParseResult result = StringToNumberParser.parse(fromObject, |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
117 numberFormat, toType.isPrimitive()); |
78 | 118 |
119 if (result.getPosition() !is null) { | |
120 // this shouldn't happen in the pipeline as validation should catch | |
121 // it but anyone can call convert so we should return a properly | |
122 // formatted message in an exception | |
123 throw new IllegalArgumentException(StringToNumberParser | |
85 | 124 .createParseErrorMessage(stringcast(fromObject), result |
78 | 125 .getPosition())); |
126 } else if (result.getNumber() is null) { | |
127 // if an error didn't occur and the number is null then it's a boxed | |
128 // type and null should be returned | |
129 return null; | |
130 } | |
131 | |
132 /* | |
133 * Technically the checks for ranges aren't needed here because the | |
134 * validator should have validated this already but we shouldn't assume | |
135 * this has occurred. | |
136 */ | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
137 if (Class.fromType!(Integer) is boxedType) { |
78 | 138 if (StringToNumberParser.inIntegerRange(result.getNumber())) { |
139 return new Integer(result.getNumber().intValue()); | |
140 } | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
141 } else if (Class.fromType!(Double) is boxedType) { |
78 | 142 if (StringToNumberParser.inDoubleRange(result.getNumber())) { |
143 return new Double(result.getNumber().doubleValue()); | |
144 } | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
145 } else if (Class.fromType!(Long) is boxedType) { |
78 | 146 if (StringToNumberParser.inLongRange(result.getNumber())) { |
147 return new Long(result.getNumber().longValue()); | |
148 } | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
149 } else if (Class.fromType!(Float) is boxedType) { |
78 | 150 if (StringToNumberParser.inFloatRange(result.getNumber())) { |
151 return new Float(result.getNumber().floatValue()); | |
152 } | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
153 } else if (Class.fromType!(BigInteger) is boxedType) { |
78 | 154 return (new BigDecimal(result.getNumber().doubleValue())) |
155 .toBigInteger(); | |
156 } | |
157 | |
158 if (min !is null && max !is null) { | |
159 throw new IllegalArgumentException(StringToNumberParser | |
160 .createOutOfRangeMessage(min, max, numberFormat)); | |
161 } | |
162 | |
163 /* | |
164 * Fail safe. I don't think this could even be thrown but throwing the | |
165 * exception is better than returning null and hiding the error. | |
166 */ | |
167 throw new IllegalArgumentException( | |
85 | 168 Format("Could not convert [{}] to type [{}]", fromObject, toType)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
78 | 169 } |
170 | |
171 /** | |
172 * @param primitive | |
173 * <code>true</code> if the convert to type is an int | |
174 * @return to Integer converter for the default locale | |
175 */ | |
176 public static StringToNumberConverter toInteger(bool primitive) { | |
177 return toInteger(NumberFormat.getIntegerInstance(), primitive); | |
178 } | |
179 | |
180 /** | |
181 * @param numberFormat | |
182 * @param primitive | |
183 * @return to Integer converter with the provided numberFormat | |
184 */ | |
185 public static StringToNumberConverter toInteger(NumberFormat numberFormat, | |
186 bool primitive) { | |
187 return new StringToNumberConverter(numberFormat, | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
188 (primitive) ? Integer.TYPE : Class.fromType!(Integer), MIN_INTEGER, |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
189 MAX_INTEGER, Class.fromType!(Integer)); |
78 | 190 } |
191 | |
192 /** | |
193 * @param primitive | |
194 * <code>true</code> if the convert to type is a double | |
195 * @return to Double converter for the default locale | |
196 */ | |
197 public static StringToNumberConverter toDouble(bool primitive) { | |
198 return toDouble(NumberFormat.getNumberInstance(), primitive); | |
199 } | |
200 | |
201 /** | |
202 * @param numberFormat | |
203 * @param primitive | |
204 * @return to Double converter with the provided numberFormat | |
205 */ | |
206 public static StringToNumberConverter toDouble(NumberFormat numberFormat, | |
207 bool primitive) { | |
208 return new StringToNumberConverter(numberFormat, | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
209 (primitive) ? Double.TYPE : Class.fromType!(Double), MIN_DOUBLE, |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
210 MAX_DOUBLE, Class.fromType!(Double)); |
78 | 211 } |
212 | |
213 /** | |
214 * @param primitive | |
215 * <code>true</code> if the convert to type is a long | |
216 * @return to Long converter for the default locale | |
217 */ | |
218 public static StringToNumberConverter toLong(bool primitive) { | |
219 return toLong(NumberFormat.getIntegerInstance(), primitive); | |
220 } | |
221 | |
222 /** | |
223 * @param numberFormat | |
224 * @param primitive | |
225 * @return to Long converter with the provided numberFormat | |
226 */ | |
227 public static StringToNumberConverter toLong(NumberFormat numberFormat, | |
228 bool primitive) { | |
229 return new StringToNumberConverter(numberFormat, | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
230 (primitive) ? Long.TYPE : Class.fromType!(Long), MIN_LONG, MAX_LONG, |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
231 Class.fromType!(Long)); |
78 | 232 } |
233 | |
234 /** | |
235 * @param primitive | |
236 * <code>true</code> if the convert to type is a float | |
237 * @return to Float converter for the default locale | |
238 */ | |
239 public static StringToNumberConverter toFloat(bool primitive) { | |
240 return toFloat(NumberFormat.getNumberInstance(), primitive); | |
241 } | |
242 | |
243 /** | |
244 * @param numberFormat | |
245 * @param primitive | |
246 * @return to Float converter with the provided numberFormat | |
247 */ | |
248 public static StringToNumberConverter toFloat(NumberFormat numberFormat, | |
249 bool primitive) { | |
250 return new StringToNumberConverter(numberFormat, | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
251 (primitive) ? Float.TYPE : Class.fromType!(Float), MIN_FLOAT, MAX_FLOAT, |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
252 Class.fromType!(Float)); |
78 | 253 } |
254 | |
255 /** | |
256 * @return to BigInteger converter for the default locale | |
257 */ | |
258 public static StringToNumberConverter toBigInteger() { | |
259 return toBigInteger(NumberFormat.getIntegerInstance()); | |
260 } | |
261 | |
262 /** | |
263 * @param numberFormat | |
264 * @return to BigInteger converter with the provided numberFormat | |
265 */ | |
266 public static StringToNumberConverter toBigInteger(NumberFormat numberFormat) { | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
267 return new StringToNumberConverter(numberFormat, Class.fromType!(BigInteger), |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
268 null, null, Class.fromType!(BigInteger)); |
78 | 269 } |
270 } |