KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > databinding > conversion > StringToByteConverter


1 /*
2  * Copyright (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 package org.eclipse.core.internal.databinding.conversion;
13
14 import org.eclipse.core.internal.databinding.conversion.StringToNumberParser.ParseResult;
15 import org.eclipse.core.internal.databinding.validation.NumberFormatConverter;
16
17 import com.ibm.icu.text.NumberFormat;
18
19 /**
20  * @since 1.0
21  */

22 public class StringToByteConverter extends NumberFormatConverter {
23     private String JavaDoc outOfRangeMessage;
24     private NumberFormat numberFormat;
25     private boolean primitive;
26     
27     /**
28      * @param numberFormat
29      * @param toType
30      */

31     private StringToByteConverter(NumberFormat numberFormat, Class JavaDoc toType) {
32         super(String JavaDoc.class, toType, numberFormat);
33         primitive = toType.isPrimitive();
34         this.numberFormat = numberFormat;
35     }
36
37     /**
38      * @param numberFormat
39      * @param primitive
40      * @return converter
41      */

42     public static StringToByteConverter toByte(NumberFormat numberFormat,
43             boolean primitive) {
44         return new StringToByteConverter(numberFormat, (primitive) ? Byte.TYPE : Byte JavaDoc.class);
45     }
46
47     /**
48      * @param primitive
49      * @return converter
50      */

51     public static StringToByteConverter toByte(boolean primitive) {
52         return toByte(NumberFormat.getIntegerInstance(), primitive);
53     }
54
55     /* (non-Javadoc)
56      * @see org.eclipse.core.databinding.conversion.IConverter#convert(java.lang.Object)
57      */

58     public Object JavaDoc convert(Object JavaDoc fromObject) {
59         ParseResult result = StringToNumberParser.parse(fromObject,
60                 numberFormat, primitive);
61
62         if (result.getPosition() != null) {
63             // this shouldn't happen in the pipeline as validation should catch
64
// it but anyone can call convert so we should return a properly
65
// formatted message in an exception
66
throw new IllegalArgumentException JavaDoc(StringToNumberParser
67                     .createParseErrorMessage((String JavaDoc) fromObject, result
68                             .getPosition()));
69         } else if (result.getNumber() == null) {
70             // if an error didn't occur and the number is null then it's a boxed
71
// type and null should be returned
72
return null;
73         }
74
75         if (StringToNumberParser.inByteRange(result.getNumber())) {
76             return new Byte JavaDoc(result.getNumber().byteValue());
77         }
78         
79         synchronized (this) {
80             if (outOfRangeMessage == null) {
81                 outOfRangeMessage = StringToNumberParser
82                 .createOutOfRangeMessage(new Byte JavaDoc(Byte.MIN_VALUE), new Byte JavaDoc(Byte.MAX_VALUE), numberFormat);
83             }
84                         
85             throw new IllegalArgumentException JavaDoc(outOfRangeMessage);
86         }
87     }
88 }
89
Popular Tags