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