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;
|
|
29 private final ClassInfo fromType;
|
|
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 */
|
|
43 private this(NumberFormat numberFormat, ClassInfo fromType) {
|
|
44 super(fromType, String.classinfo);
|
|
45
|
|
46 this.numberFormat = numberFormat;
|
|
47 this.fromType = fromType;
|
|
48
|
|
49 if (Integer.classinfo.equals(fromType) || Integer.TYPE.equals(fromType)
|
|
50 || Long.classinfo.equals(fromType) || Long.TYPE.equals(fromType)) {
|
|
51 fromTypeIsLong = true;
|
|
52 } else if (Float.classinfo.equals(fromType) || Float.TYPE.equals(fromType)
|
|
53 || Double.classinfo.equals(fromType)
|
|
54 || Double.TYPE.equals(fromType)) {
|
|
55 fromTypeIsDecimalType = true;
|
|
56 } else if (BigInteger.classinfo.equals(fromType)) {
|
|
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.
|
|
74 if (fromObject is null && !fromType.isPrimitive()) {
|
|
75 return ""; //$NON-NLS-1$
|
|
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
|
|
94 return result;
|
|
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,
|
|
114 (primitive) ? Double.TYPE : Double.classinfo);
|
|
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,
|
|
134 (primitive) ? Long.TYPE : Long.classinfo);
|
|
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,
|
|
154 (primitive) ? Float.TYPE : Float.classinfo);
|
|
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,
|
|
174 (primitive) ? Integer.TYPE : Integer.classinfo);
|
|
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) {
|
|
190 return new NumberToStringConverter(numberFormat, BigInteger.classinfo);
|
|
191 }
|
|
192 }
|