Mercurial > projects > dwt2
comparison org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToByteConverter.d @ 78:0a55d2d5a946
Added file for databinding
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 14 Apr 2009 11:35:29 +0200 |
parents | |
children | 383ce7bd736b |
comparison
equal
deleted
inserted
replaced
76:f05e6e8b2f2d | 78:0a55d2d5a946 |
---|---|
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; | |
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 StringToByteConverter : NumberFormatConverter { | |
25 private String outOfRangeMessage; | |
26 private NumberFormat numberFormat; | |
27 private bool primitive; | |
28 | |
29 /** | |
30 * @param numberFormat | |
31 * @param toType | |
32 */ | |
33 private this(NumberFormat numberFormat, ClassInfo toType) { | |
34 super(String.classinfo, toType, numberFormat); | |
35 primitive = toType.isPrimitive(); | |
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) { | |
46 return new StringToByteConverter(numberFormat, (primitive) ? Byte.TYPE : Byte.classinfo); | |
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) { | |
61 ParseResult result = StringToNumberParser.parse(fromObject, | |
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 | |
69 .createParseErrorMessage(cast(String) fromObject, result | |
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 | |
84 .createOutOfRangeMessage(new Bytecast(Byte.MIN_VALUE), new Bytecast(Byte.MAX_VALUE), numberFormat); | |
85 } | |
86 | |
87 throw new IllegalArgumentException(outOfRangeMessage); | |
88 } | |
89 } | |
90 } |