78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2007, 2008 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.internal.databinding.conversion.StringToNumberParser;
|
|
13
|
|
14 import java.lang.all;
|
|
15
|
|
16 import java.math.BigDecimal;
|
|
17 import java.math.BigInteger;
|
|
18 import java.text.ParsePosition;
|
|
19
|
|
20 import org.eclipse.core.internal.databinding.BindingMessages;
|
|
21
|
|
22 import com.ibm.icu.text.NumberFormat;
|
|
23
|
|
24 /**
|
|
25 * Utility class for the parsing of strings to numbers.
|
|
26 *
|
|
27 * @since 1.0
|
|
28 */
|
|
29 public class StringToNumberParser {
|
85
|
30 private static BigDecimal FLOAT_MAX_BIG_DECIMAL;
|
|
31 private static BigDecimal FLOAT_MIN_BIG_DECIMAL;
|
|
32 private static BigDecimal DOUBLE_MAX_BIG_DECIMAL;
|
|
33 private static BigDecimal DOUBLE_MIN_BIG_DECIMAL;
|
|
34 static this(){
|
|
35 FLOAT_MAX_BIG_DECIMAL = new BigDecimal(
|
|
36 Float.MAX_VALUE);
|
|
37 FLOAT_MIN_BIG_DECIMAL = new BigDecimal(
|
|
38 -Float.MAX_VALUE);
|
78
|
39
|
85
|
40 DOUBLE_MAX_BIG_DECIMAL = new BigDecimal(
|
|
41 Double.MAX_VALUE);
|
|
42 DOUBLE_MIN_BIG_DECIMAL = new BigDecimal(
|
|
43 -Double.MAX_VALUE);
|
|
44 }
|
78
|
45
|
|
46 /**
|
|
47 * @param value
|
|
48 * @param numberFormat
|
|
49 * @param primitive
|
|
50 * @return result
|
|
51 */
|
|
52 public static ParseResult parse(Object value, NumberFormat numberFormat,
|
|
53 bool primitive) {
|
85
|
54 if (!( null !is cast(ArrayWrapperString)value )) {
|
78
|
55 throw new IllegalArgumentException(
|
|
56 "Value to convert is not a String"); //$NON-NLS-1$
|
|
57 }
|
|
58
|
85
|
59 String source = stringcast( value );
|
78
|
60 ParseResult result = new ParseResult();
|
|
61 if (!primitive && source.trim().length() is 0) {
|
|
62 return result;
|
|
63 }
|
|
64
|
|
65 synchronized (numberFormat) {
|
|
66 ParsePosition position = new ParsePosition(0);
|
|
67 Number parseResult = null;
|
|
68 parseResult = numberFormat.parse(source, position);
|
|
69
|
|
70 if (position.getIndex() !is source.length()
|
|
71 || position.getErrorIndex() > -1) {
|
|
72
|
|
73 result.position = position;
|
|
74 } else {
|
|
75 result.number = parseResult;
|
|
76 }
|
|
77 }
|
|
78
|
|
79 return result;
|
|
80 }
|
|
81
|
|
82 /**
|
|
83 * The result of a parse operation.
|
|
84 *
|
|
85 * @since 1.0
|
|
86 */
|
|
87 public static class ParseResult {
|
|
88 /* package */Number number;
|
|
89 /* package */ParsePosition position;
|
|
90
|
|
91 /**
|
|
92 * The number as a result of the conversion. <code>null</code> if the
|
|
93 * value could not be converted or if the type is not a primitive and
|
|
94 * the value was an empty string.
|
|
95 *
|
|
96 * @return number
|
|
97 */
|
|
98 public Number getNumber() {
|
|
99 return number;
|
|
100 }
|
|
101
|
|
102 /**
|
|
103 * ParsePosition if an error occurred while parsing. <code>null</code>
|
|
104 * if no error occurred.
|
|
105 *
|
|
106 * @return parse position
|
|
107 */
|
|
108 public ParsePosition getPosition() {
|
|
109 return position;
|
|
110 }
|
|
111 }
|
|
112
|
|
113 /**
|
|
114 * Formats an appropriate message for a parsing error.
|
|
115 *
|
|
116 * @param value
|
|
117 * @param position
|
|
118 * @return message
|
|
119 */
|
|
120 public static String createParseErrorMessage(String value,
|
|
121 ParsePosition position) {
|
|
122 int errorIndex = (position.getErrorIndex() > -1) ? position
|
|
123 .getErrorIndex() : position.getIndex();
|
|
124
|
|
125 if (errorIndex < value.length()) {
|
|
126 return BindingMessages.formatString(BindingMessages.VALIDATE_NUMBER_PARSE_ERROR,
|
|
127 [cast(Object) stringcast(value), new Integer(errorIndex + 1),
|
|
128 new Character(value.charAt(errorIndex)) ]);
|
|
129 }
|
|
130 return BindingMessages.formatString(BindingMessages.VALIDATE_NUMBER_PARSE_ERROR_NO_CHARACTER,
|
|
131 [cast(Object) stringcast(value), new Integer(errorIndex + 1) ]);
|
|
132 }
|
|
133
|
|
134 /**
|
|
135 * Formats an appropriate message for an out of range error.
|
|
136 *
|
|
137 * @param minValue
|
|
138 * @param maxValue
|
|
139 * @param numberFormat when accessed method synchronizes on instance
|
|
140 * @return message
|
|
141 */
|
|
142 public static String createOutOfRangeMessage(Number minValue,
|
|
143 Number maxValue, NumberFormat numberFormat) {
|
|
144 String min = null;
|
|
145 String max = null;
|
|
146
|
|
147 synchronized (numberFormat) {
|
|
148 min = numberFormat.format(minValue);
|
|
149 max = numberFormat.format(maxValue);
|
|
150 }
|
|
151
|
|
152 return BindingMessages.formatString(
|
85
|
153 "Validate_NumberOutOfRangeError", [ cast(Object)stringcast(min), stringcast(max) ]); //$NON-NLS-1$
|
78
|
154 }
|
|
155
|
|
156 /**
|
|
157 * Returns <code>true</code> if the provided <code>number</code> is in
|
|
158 * the range of a integer.
|
|
159 *
|
|
160 * @param number
|
|
161 * @return <code>true</code> if a valid integer
|
|
162 * @throws IllegalArgumentException
|
|
163 * if the number type is unsupported
|
|
164 */
|
|
165 public static bool inIntegerRange(Number number) {
|
|
166 return checkInteger(number, 31);
|
|
167 }
|
|
168
|
|
169 /**
|
|
170 * Validates the range of the provided <code>number</code>.
|
|
171 *
|
|
172 * @param number
|
|
173 * @param bitLength number of bits allowed to be in range
|
|
174 * @return <code>true</code> if in range
|
|
175 */
|
|
176 private static bool checkInteger(Number number, int bitLength) {
|
|
177 BigInteger bigInteger = null;
|
|
178
|
|
179 if ( null !is cast(Integer )number || null !is cast(Long)number ) {
|
|
180 bigInteger = BigInteger.valueOf(number.longValue());
|
|
181 } else if ( null !is cast(Float )number || null !is cast(Double)number ) {
|
|
182 double doubleValue = number.doubleValue();
|
|
183 /*
|
|
184 * doubleValue is doubleValue is used to check for NaN because NaN !is
|
|
185 * NaN. The only way to check for NaN is to compare that the value
|
|
186 * is equal to itself.
|
|
187 */
|
|
188 if (doubleValue is doubleValue
|
|
189 && doubleValue !is Double.NEGATIVE_INFINITY
|
|
190 && doubleValue !is Double.POSITIVE_INFINITY) {
|
|
191 bigInteger = (new BigDecimal(doubleValue)).toBigInteger();
|
|
192 } else {
|
|
193 return false;
|
|
194 }
|
|
195 } else if ( null !is cast(BigInteger)number ) {
|
|
196 bigInteger = cast(BigInteger) number;
|
|
197 } else if ( null !is cast(BigDecimal)number ) {
|
|
198 bigInteger = (cast(BigDecimal) number).toBigInteger();
|
|
199 } else {
|
|
200 /*
|
|
201 * The else is necessary as the ICU4J plugin has it's own BigDecimal
|
|
202 * implementation which isn't part of the replacement plugin. So
|
|
203 * that this will work we fall back on the double value of the
|
|
204 * number.
|
|
205 */
|
|
206 bigInteger = (new BigDecimal(number.doubleValue())).toBigInteger();
|
|
207 }
|
|
208
|
|
209 if (bigInteger !is null) {
|
|
210 return bigInteger.bitLength() <= bitLength;
|
|
211 }
|
|
212
|
|
213 throw new IllegalArgumentException(
|
85
|
214 Format("Number of type [{}] is not supported.", number.classinfo.name )); //$NON-NLS-1$ //$NON-NLS-2$
|
78
|
215 }
|
|
216
|
|
217 /**
|
|
218 * Returns <code>true</code> if the provided <code>number</code> is in
|
|
219 * the range of a long.
|
|
220 *
|
|
221 * @param number
|
|
222 * @return <code>true</code> if in range
|
|
223 * @throws IllegalArgumentException
|
|
224 * if the number type is unsupported
|
|
225 */
|
|
226 public static bool inLongRange(Number number) {
|
|
227 return checkInteger(number, 63);
|
|
228 }
|
|
229
|
|
230 /**
|
|
231 * Returns <code>true</code> if the provided <code>number</code> is in
|
|
232 * the range of a float.
|
|
233 *
|
|
234 * @param number
|
|
235 * @return <code>true</code> if in range
|
|
236 * @throws IllegalArgumentException
|
|
237 * if the number type is unsupported
|
|
238 */
|
|
239 public static bool inFloatRange(Number number) {
|
|
240 return checkDecimal(number, FLOAT_MIN_BIG_DECIMAL, FLOAT_MAX_BIG_DECIMAL);
|
|
241 }
|
|
242
|
|
243 private static bool checkDecimal(Number number, BigDecimal min, BigDecimal max) {
|
|
244 BigDecimal bigDecimal = null;
|
|
245 if ( null !is cast(Integer )number || null !is cast(Long)number ) {
|
|
246 bigDecimal = new BigDecimal(number.doubleValue());
|
|
247 } else if ( null !is cast(Float )number || null !is cast(Double)number ) {
|
|
248 double doubleValue = number.doubleValue();
|
|
249
|
|
250 /*
|
|
251 * doubleValue is doubleValue is used to check for NaN because NaN !is
|
|
252 * NaN. The only way to check for NaN is to compare that the value
|
|
253 * is equal to itself.
|
|
254 */
|
|
255 if (doubleValue is doubleValue
|
|
256 && doubleValue !is Double.NEGATIVE_INFINITY
|
|
257 && doubleValue !is Double.POSITIVE_INFINITY) {
|
|
258 bigDecimal = new BigDecimal(doubleValue);
|
|
259 } else {
|
|
260 return false;
|
|
261 }
|
|
262 } else if ( null !is cast(BigInteger)number ) {
|
|
263 bigDecimal = new BigDecimal(cast(BigInteger) number);
|
|
264 } else if ( null !is cast(BigDecimal)number ) {
|
|
265 bigDecimal = cast(BigDecimal) number;
|
|
266 } else {
|
|
267 /*
|
|
268 * The else is necessary as the ICU4J plugin has it's own BigDecimal
|
|
269 * implementation which isn't part of the replacement plugin. So
|
|
270 * that this will work we fall back on the double value of the
|
|
271 * number.
|
|
272 */
|
|
273 bigDecimal = new BigDecimal(number.doubleValue());
|
|
274 }
|
|
275
|
|
276 if (bigDecimal !is null) {
|
|
277 return max.compareTo(bigDecimal) >= 0
|
|
278 && min.compareTo(bigDecimal) <= 0;
|
|
279 }
|
|
280
|
|
281 throw new IllegalArgumentException(
|
85
|
282 Format("Number of type [{}] is not supported.", number.classinfo.name)); //$NON-NLS-1$ //$NON-NLS-2$
|
78
|
283 }
|
|
284
|
|
285 /**
|
|
286 * Returns <code>true</code> if the provided <code>number</code> is in
|
|
287 * the range of a double.
|
|
288 *
|
|
289 * @param number
|
|
290 * @return <code>true</code> if in range
|
|
291 * @throws IllegalArgumentException
|
|
292 * if the number type is unsupported
|
|
293 */
|
|
294 public static bool inDoubleRange(Number number) {
|
|
295 return checkDecimal(number, DOUBLE_MIN_BIG_DECIMAL, DOUBLE_MAX_BIG_DECIMAL);
|
|
296 }
|
|
297
|
|
298 /**
|
|
299 * Returns <code>true</code> if the provided <code>number</code> is in
|
|
300 * the range of a short.
|
|
301 *
|
|
302 * @param number
|
|
303 * @return <code>true</code> if in range
|
|
304 */
|
|
305 public static bool inShortRange(Number number) {
|
|
306 return checkInteger(number, 15);
|
|
307 }
|
|
308
|
|
309 /**
|
|
310 * Returns <code>true</code> if the provided <code>number</code> is in
|
|
311 * the range of a byte.
|
|
312 *
|
|
313 * @param number
|
|
314 * @return <code>true</code> if in range
|
|
315 */
|
|
316 public static bool inByteRange(Number number) {
|
|
317 return checkInteger(number, 7);
|
|
318 }
|
|
319 }
|