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.StringToShortConverter;
|
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.internal.databinding.validation.NumberFormatConverter;
|
|
19
|
|
20 import com.ibm.icu.text.NumberFormat;
|
|
21
|
|
22 /**
|
|
23 * @since 1.0
|
|
24 */
|
|
25 public class StringToShortConverter : NumberFormatConverter {
|
|
26 private final NumberFormat numberFormat;
|
|
27 private final bool primitive;
|
|
28
|
|
29 private String outOfRangeMessage;
|
|
30
|
|
31 /**
|
|
32 * Constructs a new instance.
|
|
33 */
|
85
|
34 private this(NumberFormat numberFormat, TypeInfo toType) {
|
|
35 super(typeid(String), toType, numberFormat);
|
78
|
36 this.numberFormat = numberFormat;
|
85
|
37 primitive = isJavaPrimitive(toType);
|
78
|
38 }
|
|
39
|
|
40 /*
|
|
41 * (non-Javadoc)
|
|
42 *
|
|
43 * @see org.eclipse.core.databinding.conversion.IConverter#convert(java.lang.Object)
|
|
44 */
|
|
45 public Object convert(Object fromObject) {
|
85
|
46 StringToNumberParser.ParseResult result = StringToNumberParser.parse(fromObject,
|
78
|
47 numberFormat, primitive);
|
|
48
|
|
49 if (result.getPosition() !is null) {
|
|
50 // this shouldn't happen in the pipeline as validation should catch
|
|
51 // it but anyone can call convert so we should return a properly
|
|
52 // formatted message in an exception
|
|
53 throw new IllegalArgumentException(StringToNumberParser
|
85
|
54 .createParseErrorMessage(stringcast( fromObject), result
|
78
|
55 .getPosition()));
|
|
56 } else if (result.getNumber() is null) {
|
|
57 // if an error didn't occur and the number is null then it's a boxed
|
|
58 // type and null should be returned
|
|
59 return null;
|
|
60 }
|
|
61
|
|
62 if (StringToNumberParser.inShortRange(result.getNumber())) {
|
|
63 return new Short(result.getNumber().shortValue());
|
|
64 }
|
|
65
|
|
66 synchronized (this) {
|
|
67 if (outOfRangeMessage is null) {
|
|
68 outOfRangeMessage = StringToNumberParser
|
85
|
69 .createOutOfRangeMessage(new Short(Short.MIN_VALUE), new Short(Short.MAX_VALUE), numberFormat);
|
78
|
70 }
|
|
71
|
|
72 throw new IllegalArgumentException(outOfRangeMessage);
|
|
73 }
|
|
74 }
|
|
75
|
|
76 /**
|
|
77 * @param primitive
|
|
78 * <code>true</code> if the convert to type is a short
|
|
79 * @return to Short converter for the default locale
|
|
80 */
|
|
81 public static StringToShortConverter toShort(bool primitive) {
|
|
82 return toShort(NumberFormat.getIntegerInstance(), primitive);
|
|
83 }
|
|
84
|
|
85 /**
|
|
86 * @param numberFormat
|
|
87 * @param primitive
|
|
88 * @return to Short converter with the provided numberFormat
|
|
89 */
|
|
90 public static StringToShortConverter toShort(NumberFormat numberFormat,
|
|
91 bool primitive) {
|
|
92 return new StringToShortConverter(numberFormat,
|
85
|
93 (primitive) ? Short.TYPE : typeid(Short));
|
78
|
94 }
|
|
95 }
|