KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > databinding > conversion > IntegerToStringConverter


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.internal.databinding.conversion;
13
14 import org.eclipse.core.databinding.conversion.Converter;
15
16 import com.ibm.icu.text.NumberFormat;
17
18 /**
19  * Converts a value that is an integer, non decimal, to a String using a
20  * NumberFormat.
21  * <p>
22  * This class is a temporary as this ability exists in NumberToStringConverter
23  * except that short and byte are missing.
24  * </p>
25  *
26  * @since 1.0
27  */

28 public class IntegerToStringConverter extends Converter {
29     private final boolean primitive;
30     private final NumberFormat numberFormat;
31     private final Class JavaDoc boxedType;
32
33     /**
34      * @param numberFormat
35      * @param fromType
36      * @param boxedType
37      */

38     private IntegerToStringConverter(NumberFormat numberFormat, Class JavaDoc fromType,
39             Class JavaDoc boxedType) {
40         super(fromType, String JavaDoc.class);
41         this.primitive = fromType.isPrimitive();
42         this.numberFormat = numberFormat;
43         this.boxedType = boxedType;
44     }
45
46     /*
47      * (non-Javadoc)
48      *
49      * @see org.eclipse.core.databinding.conversion.IConverter#convert(java.lang.Object)
50      */

51     public Object JavaDoc convert(Object JavaDoc fromObject) {
52         // Null is allowed when the type is not primitve.
53
if (fromObject == null && !primitive) {
54             return ""; //$NON-NLS-1$
55
}
56
57         if (!boxedType.isInstance(fromObject)) {
58             throw new IllegalArgumentException JavaDoc(
59                     "'fromObject' is not of type [" + boxedType + "]."); //$NON-NLS-1$//$NON-NLS-2$
60
}
61
62         return numberFormat.format(((Number JavaDoc) fromObject).longValue());
63     }
64
65     /**
66      * @param primitive
67      * @return converter
68      */

69     public static IntegerToStringConverter fromShort(boolean primitive) {
70         return fromShort(NumberFormat.getIntegerInstance(), primitive);
71     }
72
73     /**
74      * @param numberFormat
75      * @param primitive
76      * @return converter
77      */

78     public static IntegerToStringConverter fromShort(NumberFormat numberFormat,
79             boolean primitive) {
80         return new IntegerToStringConverter(numberFormat,
81                 primitive ? Short.TYPE : Short JavaDoc.class, Short JavaDoc.class);
82     }
83
84     /**
85      * @param primitive
86      * @return converter
87      */

88     public static IntegerToStringConverter fromByte(boolean primitive) {
89         return fromByte(NumberFormat.getIntegerInstance(), primitive);
90     }
91
92     /**
93      * @param numberFormat
94      * @param primitive
95      * @return converter
96      */

97     public static IntegerToStringConverter fromByte(NumberFormat numberFormat,
98             boolean primitive) {
99         return new IntegerToStringConverter(numberFormat, primitive ? Byte.TYPE
100                 : Byte JavaDoc.class, Byte JavaDoc.class);
101     }
102 }
103
Popular Tags