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