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