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