KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 StringToShortConverter extends NumberFormatConverter {
23     private final NumberFormat numberFormat;
24     private final boolean primitive;
25     
26     private String JavaDoc outOfRangeMessage;
27
28     /**
29      * Constructs a new instance.
30      */

31     private StringToShortConverter(NumberFormat numberFormat, Class JavaDoc toType) {
32         super(String JavaDoc.class, toType, numberFormat);
33         this.numberFormat = numberFormat;
34         primitive = toType.isPrimitive();
35     }
36
37     /*
38      * (non-Javadoc)
39      *
40      * @see org.eclipse.core.databinding.conversion.IConverter#convert(java.lang.Object)
41      */

42     public Object JavaDoc convert(Object JavaDoc fromObject) {
43         ParseResult result = StringToNumberParser.parse(fromObject,
44                 numberFormat, primitive);
45
46         if (result.getPosition() != null) {
47             // this shouldn't happen in the pipeline as validation should catch
48
// it but anyone can call convert so we should return a properly
49
// formatted message in an exception
50
throw new IllegalArgumentException JavaDoc(StringToNumberParser
51                     .createParseErrorMessage((String JavaDoc) fromObject, result
52                             .getPosition()));
53         } else if (result.getNumber() == null) {
54             // if an error didn't occur and the number is null then it's a boxed
55
// type and null should be returned
56
return null;
57         }
58
59         if (StringToNumberParser.inShortRange(result.getNumber())) {
60             return new Short JavaDoc(result.getNumber().shortValue());
61         }
62         
63         synchronized (this) {
64             if (outOfRangeMessage == null) {
65                 outOfRangeMessage = StringToNumberParser
66                 .createOutOfRangeMessage(new Short JavaDoc(Short.MIN_VALUE), new Short JavaDoc(Short.MAX_VALUE), numberFormat);
67             }
68                         
69             throw new IllegalArgumentException JavaDoc(outOfRangeMessage);
70         }
71     }
72
73     /**
74      * @param primitive
75      * <code>true</code> if the convert to type is a short
76      * @return to Short converter for the default locale
77      */

78     public static StringToShortConverter toShort(boolean primitive) {
79         return toShort(NumberFormat.getIntegerInstance(), primitive);
80     }
81
82     /**
83      * @param numberFormat
84      * @param primitive
85      * @return to Short converter with the provided numberFormat
86      */

87     public static StringToShortConverter toShort(NumberFormat numberFormat,
88             boolean primitive) {
89         return new StringToShortConverter(numberFormat,
90                 (primitive) ? Short.TYPE : Short JavaDoc.class);
91     }
92 }
93
Popular Tags