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