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