comparison org.eclipse.core.databinding/src/org/eclipse/core/databinding/conversion/StringToNumberConverter.d @ 78:0a55d2d5a946

Added file for databinding
author Frank Benoit <benoit@tionex.de>
date Tue, 14 Apr 2009 11:35:29 +0200
parents
children 6be48cf9f95c
comparison
equal deleted inserted replaced
76:f05e6e8b2f2d 78:0a55d2d5a946
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.conversion.StringToNumberParser.ParseResult;
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 {
32 private ClassInfo toType;
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 */
52 private final ClassInfo boxedType;
53
54 private static final Integer MIN_INTEGER = new Integercast(Integer.MIN_VALUE);
55 private static final Integer MAX_INTEGER = new Integercast(Integer.MAX_VALUE);
56
57 private static final Double MIN_DOUBLE = new Double(-Double.MAX_VALUE);
58 private static final Double MAX_DOUBLE = new Doublecast(Double.MAX_VALUE);
59
60 private static final Long MIN_LONG = new Longcast(Long.MIN_VALUE);
61 private static final Long MAX_LONG = new Longcast(Long.MIN_VALUE);
62
63 private static final Float MIN_FLOAT = new Float(-Float.MAX_VALUE);
64 private static final Float MAX_FLOAT = new Floatcast(Float.MAX_VALUE);
65
66 /**
67 * @param numberFormat
68 * @param toType
69 * @param min
70 * minimum possible value for the type, can be <code>null</code>
71 * as BigInteger doesn't have bounds
72 * @param max
73 * maximum possible value for the type, can be <code>null</code>
74 * as BigInteger doesn't have bounds
75 * @param boxedType
76 * a convenience that allows for the checking against one type
77 * rather than boxed and unboxed types
78 */
79 private this(NumberFormat numberFormat, ClassInfo toType,
80 Number min, Number max, ClassInfo boxedType) {
81 super(String.classinfo, toType, numberFormat);
82
83 this.toType = toType;
84 this.numberFormat = numberFormat;
85 this.min = min;
86 this.max = max;
87 this.boxedType = boxedType;
88 }
89
90 /**
91 * Converts the provided <code>fromObject</code> to the requested
92 * {@link #getToType() to type}.
93 *
94 * @see org.eclipse.core.databinding.conversion.IConverter#convert(java.lang.Object)
95 * @throws IllegalArgumentException
96 * if the value isn't in the format required by the NumberFormat
97 * or the value is out of range for the
98 * {@link #getToType() to type}.
99 * @throws IllegalArgumentException
100 * if conversion was not possible
101 */
102 public Object convert(Object fromObject) {
103 ParseResult result = StringToNumberParser.parse(fromObject,
104 numberFormat, toType.isPrimitive());
105
106 if (result.getPosition() !is null) {
107 // this shouldn't happen in the pipeline as validation should catch
108 // it but anyone can call convert so we should return a properly
109 // formatted message in an exception
110 throw new IllegalArgumentException(StringToNumberParser
111 .createParseErrorMessage(cast(String) fromObject, result
112 .getPosition()));
113 } else if (result.getNumber() is null) {
114 // if an error didn't occur and the number is null then it's a boxed
115 // type and null should be returned
116 return null;
117 }
118
119 /*
120 * Technically the checks for ranges aren't needed here because the
121 * validator should have validated this already but we shouldn't assume
122 * this has occurred.
123 */
124 if (Integer.classinfo.equals(boxedType)) {
125 if (StringToNumberParser.inIntegerRange(result.getNumber())) {
126 return new Integer(result.getNumber().intValue());
127 }
128 } else if (Double.classinfo.equals(boxedType)) {
129 if (StringToNumberParser.inDoubleRange(result.getNumber())) {
130 return new Double(result.getNumber().doubleValue());
131 }
132 } else if (Long.classinfo.equals(boxedType)) {
133 if (StringToNumberParser.inLongRange(result.getNumber())) {
134 return new Long(result.getNumber().longValue());
135 }
136 } else if (Float.classinfo.equals(boxedType)) {
137 if (StringToNumberParser.inFloatRange(result.getNumber())) {
138 return new Float(result.getNumber().floatValue());
139 }
140 } else if (BigInteger.classinfo.equals(boxedType)) {
141 return (new BigDecimal(result.getNumber().doubleValue()))
142 .toBigInteger();
143 }
144
145 if (min !is null && max !is null) {
146 throw new IllegalArgumentException(StringToNumberParser
147 .createOutOfRangeMessage(min, max, numberFormat));
148 }
149
150 /*
151 * 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.
153 */
154 throw new IllegalArgumentException(
155 "Could not convert [" + fromObject + "] to type [" + toType + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
156 }
157
158 /**
159 * @param primitive
160 * <code>true</code> if the convert to type is an int
161 * @return to Integer converter for the default locale
162 */
163 public static StringToNumberConverter toInteger(bool primitive) {
164 return toInteger(NumberFormat.getIntegerInstance(), primitive);
165 }
166
167 /**
168 * @param numberFormat
169 * @param primitive
170 * @return to Integer converter with the provided numberFormat
171 */
172 public static StringToNumberConverter toInteger(NumberFormat numberFormat,
173 bool primitive) {
174 return new StringToNumberConverter(numberFormat,
175 (primitive) ? Integer.TYPE : Integer.classinfo, MIN_INTEGER,
176 MAX_INTEGER, Integer.classinfo);
177 }
178
179 /**
180 * @param primitive
181 * <code>true</code> if the convert to type is a double
182 * @return to Double converter for the default locale
183 */
184 public static StringToNumberConverter toDouble(bool primitive) {
185 return toDouble(NumberFormat.getNumberInstance(), primitive);
186 }
187
188 /**
189 * @param numberFormat
190 * @param primitive
191 * @return to Double converter with the provided numberFormat
192 */
193 public static StringToNumberConverter toDouble(NumberFormat numberFormat,
194 bool primitive) {
195 return new StringToNumberConverter(numberFormat,
196 (primitive) ? Double.TYPE : Double.classinfo, MIN_DOUBLE,
197 MAX_DOUBLE, Double.classinfo);
198 }
199
200 /**
201 * @param primitive
202 * <code>true</code> if the convert to type is a long
203 * @return to Long converter for the default locale
204 */
205 public static StringToNumberConverter toLong(bool primitive) {
206 return toLong(NumberFormat.getIntegerInstance(), primitive);
207 }
208
209 /**
210 * @param numberFormat
211 * @param primitive
212 * @return to Long converter with the provided numberFormat
213 */
214 public static StringToNumberConverter toLong(NumberFormat numberFormat,
215 bool primitive) {
216 return new StringToNumberConverter(numberFormat,
217 (primitive) ? Long.TYPE : Long.classinfo, MIN_LONG, MAX_LONG,
218 Long.classinfo);
219 }
220
221 /**
222 * @param primitive
223 * <code>true</code> if the convert to type is a float
224 * @return to Float converter for the default locale
225 */
226 public static StringToNumberConverter toFloat(bool primitive) {
227 return toFloat(NumberFormat.getNumberInstance(), primitive);
228 }
229
230 /**
231 * @param numberFormat
232 * @param primitive
233 * @return to Float converter with the provided numberFormat
234 */
235 public static StringToNumberConverter toFloat(NumberFormat numberFormat,
236 bool primitive) {
237 return new StringToNumberConverter(numberFormat,
238 (primitive) ? Float.TYPE : Float.classinfo, MIN_FLOAT, MAX_FLOAT,
239 Float.classinfo);
240 }
241
242 /**
243 * @return to BigInteger converter for the default locale
244 */
245 public static StringToNumberConverter toBigInteger() {
246 return toBigInteger(NumberFormat.getIntegerInstance());
247 }
248
249 /**
250 * @param numberFormat
251 * @return to BigInteger converter with the provided numberFormat
252 */
253 public static StringToNumberConverter toBigInteger(NumberFormat numberFormat) {
254 return new StringToNumberConverter(numberFormat, BigInteger.classinfo,
255 null, null, BigInteger.classinfo);
256 }
257 }