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