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