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.internal.databinding.conversion.IntegerToStringConverter;
|
|
13
|
|
14 import java.lang.all;
|
|
15
|
|
16 import org.eclipse.core.databinding.conversion.Converter;
|
|
17
|
|
18 import com.ibm.icu.text.NumberFormat;
|
|
19
|
|
20 /**
|
|
21 * Converts a value that is an integer, non decimal, to a String using a
|
|
22 * NumberFormat.
|
|
23 * <p>
|
|
24 * This class is a temporary as this ability exists in NumberToStringConverter
|
|
25 * except that short and byte are missing.
|
|
26 * </p>
|
|
27 *
|
|
28 * @since 1.0
|
|
29 */
|
|
30 public class IntegerToStringConverter : Converter {
|
|
31 private final bool primitive;
|
|
32 private final NumberFormat numberFormat;
|
|
33 private final ClassInfo boxedType;
|
|
34
|
|
35 /**
|
|
36 * @param numberFormat
|
|
37 * @param fromType
|
|
38 * @param boxedType
|
|
39 */
|
|
40 private this(NumberFormat numberFormat, ClassInfo fromType,
|
|
41 ClassInfo boxedType) {
|
|
42 super(fromType, String.classinfo);
|
|
43 this.primitive = fromType.isPrimitive();
|
|
44 this.numberFormat = numberFormat;
|
|
45 this.boxedType = boxedType;
|
|
46 }
|
|
47
|
|
48 /*
|
|
49 * (non-Javadoc)
|
|
50 *
|
|
51 * @see org.eclipse.core.databinding.conversion.IConverter#convert(java.lang.Object)
|
|
52 */
|
|
53 public Object convert(Object fromObject) {
|
|
54 // Null is allowed when the type is not primitve.
|
|
55 if (fromObject is null && !primitive) {
|
|
56 return ""; //$NON-NLS-1$
|
|
57 }
|
|
58
|
|
59 if (!boxedType.isInstance(fromObject)) {
|
|
60 throw new IllegalArgumentException(
|
|
61 "'fromObject' is not of type [" + boxedType + "]."); //$NON-NLS-1$//$NON-NLS-2$
|
|
62 }
|
|
63
|
|
64 return numberFormat.format((cast(Number) fromObject).longValue());
|
|
65 }
|
|
66
|
|
67 /**
|
|
68 * @param primitive
|
|
69 * @return converter
|
|
70 */
|
|
71 public static IntegerToStringConverter fromShort(bool primitive) {
|
|
72 return fromShort(NumberFormat.getIntegerInstance(), primitive);
|
|
73 }
|
|
74
|
|
75 /**
|
|
76 * @param numberFormat
|
|
77 * @param primitive
|
|
78 * @return converter
|
|
79 */
|
|
80 public static IntegerToStringConverter fromShort(NumberFormat numberFormat,
|
|
81 bool primitive) {
|
|
82 return new IntegerToStringConverter(numberFormat,
|
|
83 primitive ? Short.TYPE : Short.classinfo, Short.classinfo);
|
|
84 }
|
|
85
|
|
86 /**
|
|
87 * @param primitive
|
|
88 * @return converter
|
|
89 */
|
|
90 public static IntegerToStringConverter fromByte(bool primitive) {
|
|
91 return fromByte(NumberFormat.getIntegerInstance(), primitive);
|
|
92 }
|
|
93
|
|
94 /**
|
|
95 * @param numberFormat
|
|
96 * @param primitive
|
|
97 * @return converter
|
|
98 */
|
|
99 public static IntegerToStringConverter fromByte(NumberFormat numberFormat,
|
|
100 bool primitive) {
|
|
101 return new IntegerToStringConverter(numberFormat, primitive ? Byte.TYPE
|
|
102 : Byte.classinfo, Byte.classinfo);
|
|
103 }
|
|
104 }
|