Mercurial > projects > dwt2
annotate org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToByteConverter.d @ 88:9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 19 Apr 2009 11:10:09 +0200 |
parents | 6be48cf9f95c |
children |
rev | line source |
---|---|
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.validation.NumberFormatConverter; | |
18 | |
19 import com.ibm.icu.text.NumberFormat; | |
20 | |
21 /** | |
22 * @since 1.0 | |
23 */ | |
24 public class StringToByteConverter : NumberFormatConverter { | |
25 private String outOfRangeMessage; | |
26 private NumberFormat numberFormat; | |
27 private bool primitive; | |
28 | |
29 /** | |
30 * @param numberFormat | |
31 * @param toType | |
32 */ | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
33 private this(NumberFormat numberFormat, Class toType) { |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
34 super(Class.fromType!(StringCls), toType, numberFormat); |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
35 primitive = toType.isPrimitive(); |
78 | 36 this.numberFormat = numberFormat; |
37 } | |
38 | |
39 /** | |
40 * @param numberFormat | |
41 * @param primitive | |
42 * @return converter | |
43 */ | |
44 public static StringToByteConverter toByte(NumberFormat numberFormat, | |
45 bool primitive) { | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
46 return new StringToByteConverter(numberFormat, (primitive) ? Byte.TYPE : Class.fromType!(Byte)); |
78 | 47 } |
48 | |
49 /** | |
50 * @param primitive | |
51 * @return converter | |
52 */ | |
53 public static StringToByteConverter toByte(bool primitive) { | |
54 return toByte(NumberFormat.getIntegerInstance(), primitive); | |
55 } | |
56 | |
57 /* (non-Javadoc) | |
58 * @see org.eclipse.core.databinding.conversion.IConverter#convert(java.lang.Object) | |
59 */ | |
60 public Object convert(Object fromObject) { | |
85 | 61 StringToNumberParser.ParseResult result = StringToNumberParser.parse(fromObject, |
78 | 62 numberFormat, primitive); |
63 | |
64 if (result.getPosition() !is null) { | |
65 // this shouldn't happen in the pipeline as validation should catch | |
66 // it but anyone can call convert so we should return a properly | |
67 // formatted message in an exception | |
68 throw new IllegalArgumentException(StringToNumberParser | |
85 | 69 .createParseErrorMessage(stringcast(fromObject), result |
78 | 70 .getPosition())); |
71 } else if (result.getNumber() is null) { | |
72 // if an error didn't occur and the number is null then it's a boxed | |
73 // type and null should be returned | |
74 return null; | |
75 } | |
76 | |
77 if (StringToNumberParser.inByteRange(result.getNumber())) { | |
78 return new Byte(result.getNumber().byteValue()); | |
79 } | |
80 | |
81 synchronized (this) { | |
82 if (outOfRangeMessage is null) { | |
83 outOfRangeMessage = StringToNumberParser | |
85 | 84 .createOutOfRangeMessage(new Byte(Byte.MIN_VALUE), new Byte(Byte.MAX_VALUE), numberFormat); |
78 | 85 } |
86 | |
87 throw new IllegalArgumentException(outOfRangeMessage); | |
88 } | |
89 } | |
90 } |