KickJava   Java API By Example, From Geeks To Geeks.

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


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.databinding.conversion.Converter;
15
16 import com.ibm.icu.text.NumberFormat;
17
18 /**
19  * Base class for number to number converters.
20  * <p>
21  * This class is thread safe.
22  * </p>
23  *
24  * @since 1.0
25  */

26 public abstract class NumberToNumberConverter extends Converter {
27     private NumberFormat numberFormat;
28
29     private boolean primitive;
30
31     private String JavaDoc outOfRangeMessage;
32
33     protected NumberToNumberConverter(NumberFormat numberFormat,
34             Class JavaDoc fromType, Class JavaDoc toType) {
35         super(fromType, toType);
36         this.numberFormat = numberFormat;
37         this.primitive = toType.isPrimitive();
38     }
39
40     /*
41      * (non-Javadoc)
42      *
43      * @see org.eclipse.core.databinding.conversion.IConverter#convert(java.lang.Object)
44      */

45     public final Object JavaDoc convert(Object JavaDoc fromObject) {
46         if (fromObject == null) {
47             if (primitive) {
48                 throw new IllegalArgumentException JavaDoc(
49                         "Parameter 'fromObject' cannot be null."); //$NON-NLS-1$
50
}
51
52             return null;
53         }
54
55         if (!(fromObject instanceof Number JavaDoc)) {
56             throw new IllegalArgumentException JavaDoc(
57                     "Parameter 'fromObject' must be of type Number."); //$NON-NLS-1$
58
}
59
60         Number JavaDoc number = (Number JavaDoc) fromObject;
61         Number JavaDoc result = doConvert(number);
62
63         if (result != null) {
64             return result;
65         }
66
67         synchronized (this) {
68             if (outOfRangeMessage == null) {
69                 outOfRangeMessage = StringToNumberParser
70                         .createOutOfRangeMessage(new Short JavaDoc(Short.MIN_VALUE),
71                                 new Short JavaDoc(Short.MAX_VALUE), numberFormat);
72             }
73
74             throw new IllegalArgumentException JavaDoc(outOfRangeMessage);
75         }
76     }
77
78     /**
79      * Invoked when the number should converted.
80      *
81      * @param number
82      * @return number if conversion was successfule, <code>null</code> if the
83      * number was out of range
84      */

85     protected abstract Number JavaDoc doConvert(Number JavaDoc number);
86
87     /**
88      * NumberFormat being used by the converter. Access to the format must be
89      * synchronized on the number format instance.
90      *
91      * @return number format
92      */

93     public NumberFormat getNumberFormat() {
94         return numberFormat;
95     }
96 }
97
Popular Tags