KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > databinding > conversion > NumberToStringConverter


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 package org.eclipse.core.databinding.conversion;
13
14 import java.math.BigInteger JavaDoc;
15
16 import com.ibm.icu.text.NumberFormat;
17
18 /**
19  * Converts a Number to a String using <code>NumberFormat.format(...)</code>.
20  * This class is thread safe.
21  *
22  * @since 1.0
23  */

24 public class NumberToStringConverter extends Converter {
25     private final NumberFormat numberFormat;
26     private final Class JavaDoc fromType;
27     private boolean fromTypeIsLong;
28     private boolean fromTypeIsDecimalType;
29     private boolean fromTypeIsBigInteger;
30
31     /**
32      * Constructs a new instance.
33      * <p>
34      * Private to restrict public instantiation.
35      * </p>
36      *
37      * @param numberFormat
38      * @param fromType
39      */

40     private NumberToStringConverter(NumberFormat numberFormat, Class JavaDoc fromType) {
41         super(fromType, String JavaDoc.class);
42
43         this.numberFormat = numberFormat;
44         this.fromType = fromType;
45
46         if (Integer JavaDoc.class.equals(fromType) || Integer.TYPE.equals(fromType)
47                 || Long JavaDoc.class.equals(fromType) || Long.TYPE.equals(fromType)) {
48             fromTypeIsLong = true;
49         } else if (Float JavaDoc.class.equals(fromType) || Float.TYPE.equals(fromType)
50                 || Double JavaDoc.class.equals(fromType)
51                 || Double.TYPE.equals(fromType)) {
52             fromTypeIsDecimalType = true;
53         } else if (BigInteger JavaDoc.class.equals(fromType)) {
54             fromTypeIsBigInteger = true;
55         }
56     }
57
58     /**
59      * Converts the provided <code>fromObject</code> to a <code>String</code>.
60      * If the converter was constructed for an object type, non primitive, a
61      * <code>fromObject</code> of <code>null</code> will be converted to an
62      * empty string.
63      *
64      * @param fromObject
65      * value to convert. May be <code>null</code> if the converter
66      * was constructed for a non primitive type.
67      * @see org.eclipse.core.databinding.conversion.IConverter#convert(java.lang.Object)
68      */

69     public Object JavaDoc convert(Object JavaDoc fromObject) {
70         // Null is allowed when the type is not primitve.
71
if (fromObject == null && !fromType.isPrimitive()) {
72             return ""; //$NON-NLS-1$
73
}
74
75         Number JavaDoc number = (Number JavaDoc) fromObject;
76         String JavaDoc result = null;
77         if (fromTypeIsLong) {
78             synchronized (numberFormat) {
79                 result = numberFormat.format(number.longValue());
80             }
81         } else if (fromTypeIsDecimalType) {
82             synchronized (numberFormat) {
83                 result = numberFormat.format(number.doubleValue());
84             }
85         } else if (fromTypeIsBigInteger) {
86             synchronized (numberFormat) {
87                 result = numberFormat.format((BigInteger JavaDoc) number);
88             }
89         }
90
91         return result;
92     }
93
94     /**
95      * @param primitive
96      * <code>true</code> if the type is a double
97      * @return Double converter for the default locale
98      */

99     public static NumberToStringConverter fromDouble(boolean primitive) {
100         return fromDouble(NumberFormat.getNumberInstance(), primitive);
101     }
102
103     /**
104      * @param numberFormat
105      * @param primitive
106      * @return Double converter with the provided numberFormat
107      */

108     public static NumberToStringConverter fromDouble(NumberFormat numberFormat,
109             boolean primitive) {
110         return new NumberToStringConverter(numberFormat,
111                 (primitive) ? Double.TYPE : Double JavaDoc.class);
112     }
113
114     /**
115      * @param primitive
116      * <code>true</code> if the type is a long
117      * @return Long converter for the default locale
118      */

119     public static NumberToStringConverter fromLong(boolean primitive) {
120         return fromLong(NumberFormat.getIntegerInstance(), primitive);
121     }
122
123     /**
124      * @param numberFormat
125      * @param primitive
126      * @return Long convert with the provided numberFormat
127      */

128     public static NumberToStringConverter fromLong(NumberFormat numberFormat,
129             boolean primitive) {
130         return new NumberToStringConverter(numberFormat,
131                 (primitive) ? Long.TYPE : Long JavaDoc.class);
132     }
133
134     /**
135      * @param primitive
136      * <code>true</code> if the type is a float
137      * @return Float converter for the default locale
138      */

139     public static NumberToStringConverter fromFloat(boolean primitive) {
140         return fromFloat(NumberFormat.getNumberInstance(), primitive);
141     }
142
143     /**
144      * @param numberFormat
145      * @param primitive
146      * @return Float converter with the provided numberFormat
147      */

148     public static NumberToStringConverter fromFloat(NumberFormat numberFormat,
149             boolean primitive) {
150         return new NumberToStringConverter(numberFormat,
151                 (primitive) ? Float.TYPE : Float JavaDoc.class);
152     }
153
154     /**
155      * @param primitive
156      * <code>true</code> if the type is a int
157      * @return Integer converter for the default locale
158      */

159     public static NumberToStringConverter fromInteger(boolean primitive) {
160         return fromInteger(NumberFormat.getIntegerInstance(), primitive);
161     }
162
163     /**
164      * @param numberFormat
165      * @param primitive
166      * @return Integer converter with the provided numberFormat
167      */

168     public static NumberToStringConverter fromInteger(
169             NumberFormat numberFormat, boolean primitive) {
170         return new NumberToStringConverter(numberFormat,
171                 (primitive) ? Integer.TYPE : Integer JavaDoc.class);
172     }
173
174     /**
175      * @return BigInteger convert for the default locale
176      */

177     public static NumberToStringConverter fromBigInteger() {
178         return fromBigInteger(NumberFormat.getIntegerInstance());
179     }
180
181     /**
182      * @param numberFormat
183      * @return BigInteger converter with the provided numberFormat
184      */

185     public static NumberToStringConverter fromBigInteger(
186             NumberFormat numberFormat) {
187         return new NumberToStringConverter(numberFormat, BigInteger JavaDoc.class);
188     }
189 }
190
Popular Tags