Mercurial > projects > dwt2
annotate org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/IntegerToStringConverter.d @ 88:9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 19 Apr 2009 11:10:09 +0200 |
parents | 6be48cf9f95c |
children | 0ca1aee7decf |
rev | line source |
---|---|
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; | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
33 private final Class boxedType; |
78 | 34 |
35 /** | |
36 * @param numberFormat | |
37 * @param fromType | |
38 * @param boxedType | |
39 */ | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
40 private this(NumberFormat numberFormat, Class fromType, |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
41 Class boxedType) { |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
42 super(fromType, Class.fromType!(StringCls)); |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
43 this.primitive = fromType.isPrimitive(); |
78 | 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) { | |
85 | 56 return stringcast(""); //$NON-NLS-1$ |
78 | 57 } |
58 | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
59 //FIXME check with org |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
60 if (!Class.fromObject(boxedType).isAssignableFrom(Class.fromObject(fromObject))) { |
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, | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
84 primitive ? Short.TYPE : Class.fromType!(Short), Class.fromType!(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 | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
103 : Class.fromType!(Byte), Class.fromType!(Byte)); |
78 | 104 } |
105 } |