comparison org.eclipse.core.databinding/src/org/eclipse/core/databinding/conversion/NumberToStringConverter.d @ 85:6be48cf9f95c

Work on databinding
author Frank Benoit <benoit@tionex.de>
date Sat, 18 Apr 2009 13:54:50 +0200
parents 383ce7bd736b
children 9e0ab372d5d8
comparison
equal deleted inserted replaced
84:fcf926c91ca4 85:6be48cf9f95c
11 11
12 module org.eclipse.core.databinding.conversion.NumberToStringConverter; 12 module org.eclipse.core.databinding.conversion.NumberToStringConverter;
13 import org.eclipse.core.databinding.conversion.Converter; 13 import org.eclipse.core.databinding.conversion.Converter;
14 14
15 import java.lang.all; 15 import java.lang.all;
16 import java.nonstandard.RuntimeTraits;
16 17
17 import java.math.BigInteger; 18 import java.math.BigInteger;
18 19
19 import com.ibm.icu.text.NumberFormat; 20 import com.ibm.icu.text.NumberFormat;
20 21
24 * 25 *
25 * @since 1.0 26 * @since 1.0
26 */ 27 */
27 public class NumberToStringConverter : Converter { 28 public class NumberToStringConverter : Converter {
28 private final NumberFormat numberFormat; 29 private final NumberFormat numberFormat;
29 private final ClassInfo fromType; 30 private final TypeInfo fromType;
30 private bool fromTypeIsLong; 31 private bool fromTypeIsLong;
31 private bool fromTypeIsDecimalType; 32 private bool fromTypeIsDecimalType;
32 private bool fromTypeIsBigInteger; 33 private bool fromTypeIsBigInteger;
33 34
34 /** 35 /**
38 * </p> 39 * </p>
39 * 40 *
40 * @param numberFormat 41 * @param numberFormat
41 * @param fromType 42 * @param fromType
42 */ 43 */
43 private this(NumberFormat numberFormat, ClassInfo fromType) { 44 private this(NumberFormat numberFormat, TypeInfo fromType) {
44 super(fromType, String.classinfo); 45 super(fromType, typeid(StringCls));
45 46
46 this.numberFormat = numberFormat; 47 this.numberFormat = numberFormat;
47 this.fromType = fromType; 48 this.fromType = fromType;
48 49
49 if (Integer.classinfo.equals(fromType) || Integer.TYPE.equals(fromType) 50 if (typeid(Integer) is fromType || Integer.TYPE is fromType
50 || Long.classinfo.equals(fromType) || Long.TYPE.equals(fromType)) { 51 || typeid(Long) is (fromType) || Long.TYPE is (fromType)) {
51 fromTypeIsLong = true; 52 fromTypeIsLong = true;
52 } else if (Float.classinfo.equals(fromType) || Float.TYPE.equals(fromType) 53 } else if (typeid(Float) is (fromType) || Float.TYPE is (fromType)
53 || Double.classinfo.equals(fromType) 54 || typeid(Double) is (fromType)
54 || Double.TYPE.equals(fromType)) { 55 || Double.TYPE is (fromType)) {
55 fromTypeIsDecimalType = true; 56 fromTypeIsDecimalType = true;
56 } else if (BigInteger.classinfo.equals(fromType)) { 57 } else if (typeid(BigInteger) is (fromType)) {
57 fromTypeIsBigInteger = true; 58 fromTypeIsBigInteger = true;
58 } 59 }
59 } 60 }
60 61
61 /** 62 /**
69 * was constructed for a non primitive type. 70 * was constructed for a non primitive type.
70 * @see org.eclipse.core.databinding.conversion.IConverter#convert(java.lang.Object) 71 * @see org.eclipse.core.databinding.conversion.IConverter#convert(java.lang.Object)
71 */ 72 */
72 public Object convert(Object fromObject) { 73 public Object convert(Object fromObject) {
73 // Null is allowed when the type is not primitve. 74 // Null is allowed when the type is not primitve.
74 if (fromObject is null && !fromType.isPrimitive()) { 75 if (fromObject is null && !isJavaPrimitive(fromType)) {
75 return ""; //$NON-NLS-1$ 76 return stringcast(""); //$NON-NLS-1$
76 } 77 }
77 78
78 Number number = cast(Number) fromObject; 79 Number number = cast(Number) fromObject;
79 String result = null; 80 String result = null;
80 if (fromTypeIsLong) { 81 if (fromTypeIsLong) {
89 synchronized (numberFormat) { 90 synchronized (numberFormat) {
90 result = numberFormat.format(cast(BigInteger) number); 91 result = numberFormat.format(cast(BigInteger) number);
91 } 92 }
92 } 93 }
93 94
94 return result; 95 return stringcast(result);
95 } 96 }
96 97
97 /** 98 /**
98 * @param primitive 99 * @param primitive
99 * <code>true</code> if the type is a double 100 * <code>true</code> if the type is a double
109 * @return Double converter with the provided numberFormat 110 * @return Double converter with the provided numberFormat
110 */ 111 */
111 public static NumberToStringConverter fromDouble(NumberFormat numberFormat, 112 public static NumberToStringConverter fromDouble(NumberFormat numberFormat,
112 bool primitive) { 113 bool primitive) {
113 return new NumberToStringConverter(numberFormat, 114 return new NumberToStringConverter(numberFormat,
114 (primitive) ? Double.TYPE : Double.classinfo); 115 (primitive) ? Double.TYPE : typeid(Double));
115 } 116 }
116 117
117 /** 118 /**
118 * @param primitive 119 * @param primitive
119 * <code>true</code> if the type is a long 120 * <code>true</code> if the type is a long
129 * @return Long convert with the provided numberFormat 130 * @return Long convert with the provided numberFormat
130 */ 131 */
131 public static NumberToStringConverter fromLong(NumberFormat numberFormat, 132 public static NumberToStringConverter fromLong(NumberFormat numberFormat,
132 bool primitive) { 133 bool primitive) {
133 return new NumberToStringConverter(numberFormat, 134 return new NumberToStringConverter(numberFormat,
134 (primitive) ? Long.TYPE : Long.classinfo); 135 (primitive) ? Long.TYPE : typeid(Long));
135 } 136 }
136 137
137 /** 138 /**
138 * @param primitive 139 * @param primitive
139 * <code>true</code> if the type is a float 140 * <code>true</code> if the type is a float
149 * @return Float converter with the provided numberFormat 150 * @return Float converter with the provided numberFormat
150 */ 151 */
151 public static NumberToStringConverter fromFloat(NumberFormat numberFormat, 152 public static NumberToStringConverter fromFloat(NumberFormat numberFormat,
152 bool primitive) { 153 bool primitive) {
153 return new NumberToStringConverter(numberFormat, 154 return new NumberToStringConverter(numberFormat,
154 (primitive) ? Float.TYPE : Float.classinfo); 155 (primitive) ? Float.TYPE : typeid(Float));
155 } 156 }
156 157
157 /** 158 /**
158 * @param primitive 159 * @param primitive
159 * <code>true</code> if the type is a int 160 * <code>true</code> if the type is a int
169 * @return Integer converter with the provided numberFormat 170 * @return Integer converter with the provided numberFormat
170 */ 171 */
171 public static NumberToStringConverter fromInteger( 172 public static NumberToStringConverter fromInteger(
172 NumberFormat numberFormat, bool primitive) { 173 NumberFormat numberFormat, bool primitive) {
173 return new NumberToStringConverter(numberFormat, 174 return new NumberToStringConverter(numberFormat,
174 (primitive) ? Integer.TYPE : Integer.classinfo); 175 (primitive) ? Integer.TYPE : typeid(Integer));
175 } 176 }
176 177
177 /** 178 /**
178 * @return BigInteger convert for the default locale 179 * @return BigInteger convert for the default locale
179 */ 180 */
185 * @param numberFormat 186 * @param numberFormat
186 * @return BigInteger converter with the provided numberFormat 187 * @return BigInteger converter with the provided numberFormat
187 */ 188 */
188 public static NumberToStringConverter fromBigInteger( 189 public static NumberToStringConverter fromBigInteger(
189 NumberFormat numberFormat) { 190 NumberFormat numberFormat) {
190 return new NumberToStringConverter(numberFormat, BigInteger.classinfo); 191 return new NumberToStringConverter(numberFormat, typeid(BigInteger));
191 } 192 }
192 } 193 }