]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/text/CurrencyFormat.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / text / CurrencyFormat.java
1 /*\r
2 **********************************************************************\r
3 * Copyright (c) 2004-2005, International Business Machines\r
4 * Corporation and others.  All Rights Reserved.\r
5 **********************************************************************\r
6 * Author: Alan Liu\r
7 * Created: April 20, 2004\r
8 * Since: ICU 3.0\r
9 **********************************************************************\r
10 */\r
11 package com.ibm.icu.text;\r
12 \r
13 import java.text.FieldPosition;\r
14 import java.text.ParsePosition;\r
15 \r
16 import com.ibm.icu.util.CurrencyAmount;\r
17 import com.ibm.icu.util.ULocale;\r
18 \r
19 /**\r
20  * Temporary internal concrete subclass of MeasureFormat implementing\r
21  * parsing and formatting of CurrencyAmount objects.  This class is\r
22  * likely to be redesigned and rewritten in the near future.\r
23  *\r
24  * <p>This class currently delegates to DecimalFormat for parsing and\r
25  * formatting.\r
26  *\r
27  * @see com.ibm.icu.text.UFormat\r
28  * @see com.ibm.icu.text.DecimalFormat\r
29  * @author Alan Liu\r
30  * @internal\r
31  */\r
32 class CurrencyFormat extends MeasureFormat {\r
33     // Generated by serialver from JDK 1.4.1_01\r
34     static final long serialVersionUID = -931679363692504634L;\r
35     \r
36     private NumberFormat fmt;\r
37 \r
38     public CurrencyFormat(ULocale locale) {\r
39         fmt = NumberFormat.getCurrencyInstance(locale.toLocale());\r
40     }\r
41 \r
42     /**\r
43      * Override Format.format().\r
44      * @see java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)\r
45      */\r
46     public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {\r
47         try {\r
48             CurrencyAmount currency = (CurrencyAmount) obj;\r
49             fmt.setCurrency(currency.getCurrency());\r
50             return fmt.format(currency.getNumber(), toAppendTo, pos);\r
51         } catch (ClassCastException e) {\r
52             throw new IllegalArgumentException("Invalid type: " + obj.getClass().getName());\r
53         }\r
54     }\r
55 \r
56     /**\r
57      * Override Format.parseObject().\r
58      * @see java.text.Format#parseObject(java.lang.String, java.text.ParsePosition)\r
59      */\r
60     public Object parseObject(String source, ParsePosition pos) {\r
61         return fmt.parseCurrency(source, pos);\r
62     }\r
63 }\r