Mercurial > projects > dwt2
annotate org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/NumberToNumberConverter.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 |
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.NumberToNumberConverter; | |
81 | 13 import org.eclipse.core.internal.databinding.conversion.StringToNumberParser; |
78 | 14 |
15 import java.lang.all; | |
16 | |
17 import org.eclipse.core.databinding.conversion.Converter; | |
18 | |
19 import com.ibm.icu.text.NumberFormat; | |
20 | |
21 /** | |
22 * Base class for number to number converters. | |
23 * <p> | |
24 * This class is thread safe. | |
25 * </p> | |
26 * | |
27 * @since 1.0 | |
28 */ | |
29 public abstract class NumberToNumberConverter : Converter { | |
30 private NumberFormat numberFormat; | |
31 | |
32 private bool primitive; | |
33 | |
34 private String outOfRangeMessage; | |
35 | |
36 protected this(NumberFormat numberFormat, | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
37 Class fromType, Class toType) { |
78 | 38 super(fromType, toType); |
39 this.numberFormat = numberFormat; | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
40 this.primitive = toType.isPrimitive(); |
78 | 41 } |
42 | |
43 /* | |
44 * (non-Javadoc) | |
45 * | |
46 * @see org.eclipse.core.databinding.conversion.IConverter#convert(java.lang.Object) | |
47 */ | |
85 | 48 // DWT not final, need to override to reimplement interface IConvert |
49 public /+final+/ Object convert(Object fromObject) { | |
78 | 50 if (fromObject is null) { |
51 if (primitive) { | |
52 throw new IllegalArgumentException( | |
53 "Parameter 'fromObject' cannot be null."); //$NON-NLS-1$ | |
54 } | |
55 | |
56 return null; | |
57 } | |
58 | |
59 if (!( null !is cast(Number)fromObject )) { | |
60 throw new IllegalArgumentException( | |
61 "Parameter 'fromObject' must be of type Number."); //$NON-NLS-1$ | |
62 } | |
63 | |
64 Number number = cast(Number) fromObject; | |
65 Number result = doConvert(number); | |
66 | |
67 if (result !is null) { | |
68 return result; | |
69 } | |
70 | |
71 synchronized (this) { | |
72 if (outOfRangeMessage is null) { | |
73 outOfRangeMessage = StringToNumberParser | |
85 | 74 .createOutOfRangeMessage(new Short(Short.MIN_VALUE), |
75 new Short(Short.MAX_VALUE), numberFormat); | |
78 | 76 } |
77 | |
78 throw new IllegalArgumentException(outOfRangeMessage); | |
79 } | |
80 } | |
81 | |
82 /** | |
83 * Invoked when the number should converted. | |
84 * | |
85 * @param number | |
86 * @return number if conversion was successfule, <code>null</code> if the | |
87 * number was out of range | |
88 */ | |
89 protected abstract Number doConvert(Number number); | |
90 | |
91 /** | |
92 * NumberFormat being used by the converter. Access to the format must be | |
93 * synchronized on the number format instance. | |
94 * | |
95 * @return number format | |
96 */ | |
97 public NumberFormat getNumberFormat() { | |
98 return numberFormat; | |
99 } | |
100 } |