Mercurial > projects > dwt2
annotate org.eclipse.core.databinding/src/org/eclipse/core/databinding/conversion/NumberToStringConverter.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.NumberToStringConverter; | |
81 | 13 import org.eclipse.core.databinding.conversion.Converter; |
78 | 14 |
15 import java.lang.all; | |
16 | |
17 import java.math.BigInteger; | |
18 | |
19 import com.ibm.icu.text.NumberFormat; | |
20 | |
21 /** | |
22 * Converts a Number to a String using <code>NumberFormat.format(...)</code>. | |
23 * This class is thread safe. | |
24 * | |
25 * @since 1.0 | |
26 */ | |
27 public class NumberToStringConverter : Converter { | |
28 private final NumberFormat numberFormat; | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
29 private final Class fromType; |
78 | 30 private bool fromTypeIsLong; |
31 private bool fromTypeIsDecimalType; | |
32 private bool fromTypeIsBigInteger; | |
33 | |
34 /** | |
35 * Constructs a new instance. | |
36 * <p> | |
37 * Private to restrict public instantiation. | |
38 * </p> | |
39 * | |
40 * @param numberFormat | |
41 * @param fromType | |
42 */ | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
43 private this(NumberFormat numberFormat, Class fromType) { |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
44 super(fromType, Class.fromType!(StringCls)); |
78 | 45 |
46 this.numberFormat = numberFormat; | |
47 this.fromType = fromType; | |
48 | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
49 if (Class.fromType!(Integer) is fromType || Integer.TYPE is fromType |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
50 || Class.fromType!(Long) is (fromType) || Long.TYPE is (fromType)) { |
78 | 51 fromTypeIsLong = true; |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
52 } else if (Class.fromType!(Float) is (fromType) || Float.TYPE is (fromType) |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
53 || Class.fromType!(Double) is (fromType) |
85 | 54 || Double.TYPE is (fromType)) { |
78 | 55 fromTypeIsDecimalType = true; |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
56 } else if (Class.fromType!(BigInteger) is (fromType)) { |
78 | 57 fromTypeIsBigInteger = true; |
58 } | |
59 } | |
60 | |
61 /** | |
62 * Converts the provided <code>fromObject</code> to a <code>String</code>. | |
63 * If the converter was constructed for an object type, non primitive, a | |
64 * <code>fromObject</code> of <code>null</code> will be converted to an | |
65 * empty string. | |
66 * | |
67 * @param fromObject | |
68 * value to convert. May be <code>null</code> if the converter | |
69 * was constructed for a non primitive type. | |
70 * @see org.eclipse.core.databinding.conversion.IConverter#convert(java.lang.Object) | |
71 */ | |
72 public Object convert(Object fromObject) { | |
73 // Null is allowed when the type is not primitve. | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
74 if (fromObject is null && !fromType.isPrimitive()) { |
85 | 75 return stringcast(""); //$NON-NLS-1$ |
78 | 76 } |
77 | |
78 Number number = cast(Number) fromObject; | |
79 String result = null; | |
80 if (fromTypeIsLong) { | |
81 synchronized (numberFormat) { | |
82 result = numberFormat.format(number.longValue()); | |
83 } | |
84 } else if (fromTypeIsDecimalType) { | |
85 synchronized (numberFormat) { | |
86 result = numberFormat.format(number.doubleValue()); | |
87 } | |
88 } else if (fromTypeIsBigInteger) { | |
89 synchronized (numberFormat) { | |
90 result = numberFormat.format(cast(BigInteger) number); | |
91 } | |
92 } | |
93 | |
85 | 94 return stringcast(result); |
78 | 95 } |
96 | |
97 /** | |
98 * @param primitive | |
99 * <code>true</code> if the type is a double | |
100 * @return Double converter for the default locale | |
101 */ | |
102 public static NumberToStringConverter fromDouble(bool primitive) { | |
103 return fromDouble(NumberFormat.getNumberInstance(), primitive); | |
104 } | |
105 | |
106 /** | |
107 * @param numberFormat | |
108 * @param primitive | |
109 * @return Double converter with the provided numberFormat | |
110 */ | |
111 public static NumberToStringConverter fromDouble(NumberFormat numberFormat, | |
112 bool primitive) { | |
113 return new NumberToStringConverter(numberFormat, | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
114 (primitive) ? Double.TYPE : Class.fromType!(Double)); |
78 | 115 } |
116 | |
117 /** | |
118 * @param primitive | |
119 * <code>true</code> if the type is a long | |
120 * @return Long converter for the default locale | |
121 */ | |
122 public static NumberToStringConverter fromLong(bool primitive) { | |
123 return fromLong(NumberFormat.getIntegerInstance(), primitive); | |
124 } | |
125 | |
126 /** | |
127 * @param numberFormat | |
128 * @param primitive | |
129 * @return Long convert with the provided numberFormat | |
130 */ | |
131 public static NumberToStringConverter fromLong(NumberFormat numberFormat, | |
132 bool primitive) { | |
133 return new NumberToStringConverter(numberFormat, | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
134 (primitive) ? Long.TYPE : Class.fromType!(Long)); |
78 | 135 } |
136 | |
137 /** | |
138 * @param primitive | |
139 * <code>true</code> if the type is a float | |
140 * @return Float converter for the default locale | |
141 */ | |
142 public static NumberToStringConverter fromFloat(bool primitive) { | |
143 return fromFloat(NumberFormat.getNumberInstance(), primitive); | |
144 } | |
145 | |
146 /** | |
147 * @param numberFormat | |
148 * @param primitive | |
149 * @return Float converter with the provided numberFormat | |
150 */ | |
151 public static NumberToStringConverter fromFloat(NumberFormat numberFormat, | |
152 bool primitive) { | |
153 return new NumberToStringConverter(numberFormat, | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
154 (primitive) ? Float.TYPE : Class.fromType!(Float)); |
78 | 155 } |
156 | |
157 /** | |
158 * @param primitive | |
159 * <code>true</code> if the type is a int | |
160 * @return Integer converter for the default locale | |
161 */ | |
162 public static NumberToStringConverter fromInteger(bool primitive) { | |
163 return fromInteger(NumberFormat.getIntegerInstance(), primitive); | |
164 } | |
165 | |
166 /** | |
167 * @param numberFormat | |
168 * @param primitive | |
169 * @return Integer converter with the provided numberFormat | |
170 */ | |
171 public static NumberToStringConverter fromInteger( | |
172 NumberFormat numberFormat, bool primitive) { | |
173 return new NumberToStringConverter(numberFormat, | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
174 (primitive) ? Integer.TYPE : Class.fromType!(Integer)); |
78 | 175 } |
176 | |
177 /** | |
178 * @return BigInteger convert for the default locale | |
179 */ | |
180 public static NumberToStringConverter fromBigInteger() { | |
181 return fromBigInteger(NumberFormat.getIntegerInstance()); | |
182 } | |
183 | |
184 /** | |
185 * @param numberFormat | |
186 * @return BigInteger converter with the provided numberFormat | |
187 */ | |
188 public static NumberToStringConverter fromBigInteger( | |
189 NumberFormat numberFormat) { | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
190 return new NumberToStringConverter(numberFormat, Class.fromType!(BigInteger)); |
78 | 191 } |
192 } |