Mercurial > projects > dwt2
annotate org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/IntegerToStringConverter.d @ 89:0ca1aee7decf
Reverting to Class, final check against eclipse sources.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 19 Apr 2009 11:33:03 +0200 |
parents | 9e0ab372d5d8 |
children |
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 | |
89
0ca1aee7decf
Reverting to Class, final check against eclipse sources.
Frank Benoit <benoit@tionex.de>
parents:
88
diff
changeset
|
59 if (!boxedType.isInstance(fromObject)) { |
78 | 60 throw new IllegalArgumentException( |
85 | 61 Format("'fromObject' is not of type [{}].", boxedType)); //$NON-NLS-1$//$NON-NLS-2$ |
78 | 62 } |
63 | |
85 | 64 return stringcast(numberFormat.format((cast(Number) fromObject).longValue())); |
78 | 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, | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
83 primitive ? Short.TYPE : Class.fromType!(Short), Class.fromType!(Short)); |
78 | 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 | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
102 : Class.fromType!(Byte), Class.fromType!(Byte)); |
78 | 103 } |
104 } |