78
|
1 /*
|
|
2 * Copyright cast(C) 2005 db4objects Inc. http://www.db4o.com
|
|
3 *
|
|
4 * All rights reserved. This program and the accompanying materials
|
|
5 * are made available under the terms of the Eclipse Public License v1.0
|
|
6 * which accompanies this distribution, and is available at
|
|
7 * http://www.eclipse.org/legal/epl-v10.html
|
|
8 *
|
|
9 * Contributors:
|
|
10 * db4objects - Initial API and implementation
|
|
11 */
|
|
12 module org.eclipse.core.internal.databinding.conversion.StringToByteConverter;
|
81
|
13 import org.eclipse.core.internal.databinding.conversion.StringToNumberParser;
|
78
|
14
|
|
15 import java.lang.all;
|
|
16
|
|
17 import org.eclipse.core.internal.databinding.conversion.StringToNumberParser.ParseResult;
|
|
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 StringToByteConverter : NumberFormatConverter {
|
|
26 private String outOfRangeMessage;
|
|
27 private NumberFormat numberFormat;
|
|
28 private bool primitive;
|
|
29
|
|
30 /**
|
|
31 * @param numberFormat
|
|
32 * @param toType
|
|
33 */
|
|
34 private this(NumberFormat numberFormat, ClassInfo toType) {
|
|
35 super(String.classinfo, toType, numberFormat);
|
|
36 primitive = toType.isPrimitive();
|
|
37 this.numberFormat = numberFormat;
|
|
38 }
|
|
39
|
|
40 /**
|
|
41 * @param numberFormat
|
|
42 * @param primitive
|
|
43 * @return converter
|
|
44 */
|
|
45 public static StringToByteConverter toByte(NumberFormat numberFormat,
|
|
46 bool primitive) {
|
|
47 return new StringToByteConverter(numberFormat, (primitive) ? Byte.TYPE : Byte.classinfo);
|
|
48 }
|
|
49
|
|
50 /**
|
|
51 * @param primitive
|
|
52 * @return converter
|
|
53 */
|
|
54 public static StringToByteConverter toByte(bool primitive) {
|
|
55 return toByte(NumberFormat.getIntegerInstance(), primitive);
|
|
56 }
|
|
57
|
|
58 /* (non-Javadoc)
|
|
59 * @see org.eclipse.core.databinding.conversion.IConverter#convert(java.lang.Object)
|
|
60 */
|
|
61 public Object convert(Object fromObject) {
|
|
62 ParseResult result = StringToNumberParser.parse(fromObject,
|
|
63 numberFormat, primitive);
|
|
64
|
|
65 if (result.getPosition() !is null) {
|
|
66 // this shouldn't happen in the pipeline as validation should catch
|
|
67 // it but anyone can call convert so we should return a properly
|
|
68 // formatted message in an exception
|
|
69 throw new IllegalArgumentException(StringToNumberParser
|
|
70 .createParseErrorMessage(cast(String) fromObject, result
|
|
71 .getPosition()));
|
|
72 } else if (result.getNumber() is null) {
|
|
73 // if an error didn't occur and the number is null then it's a boxed
|
|
74 // type and null should be returned
|
|
75 return null;
|
|
76 }
|
|
77
|
|
78 if (StringToNumberParser.inByteRange(result.getNumber())) {
|
|
79 return new Byte(result.getNumber().byteValue());
|
|
80 }
|
|
81
|
|
82 synchronized (this) {
|
|
83 if (outOfRangeMessage is null) {
|
|
84 outOfRangeMessage = StringToNumberParser
|
|
85 .createOutOfRangeMessage(new Bytecast(Byte.MIN_VALUE), new Bytecast(Byte.MAX_VALUE), numberFormat);
|
|
86 }
|
|
87
|
|
88 throw new IllegalArgumentException(outOfRangeMessage);
|
|
89 }
|
|
90 }
|
|
91 }
|