CurrencyFormat.java revision f86f25d102340da66b9c7cb6b2d5ecdc0de43ecf
1/* GENERATED SOURCE. DO NOT MODIFY. */
2// © 2016 and later: Unicode, Inc. and others.
3// License & terms of use: http://www.unicode.org/copyright.html#License
4/*
5**********************************************************************
6* Copyright (c) 2004-2014, International Business Machines
7* Corporation and others.  All Rights Reserved.
8**********************************************************************
9* Author: Alan Liu
10* Created: April 20, 2004
11* Since: ICU 3.0
12**********************************************************************
13*/
14package android.icu.text;
15
16import java.io.ObjectStreamException;
17import java.text.FieldPosition;
18import java.text.ParsePosition;
19
20import android.icu.util.CurrencyAmount;
21import android.icu.util.Measure;
22import android.icu.util.ULocale;
23
24/**
25 * Temporary internal concrete subclass of MeasureFormat implementing
26 * parsing and formatting of CurrencyAmount objects.  This class is
27 * likely to be redesigned and rewritten in the near future.
28 *
29 * <p>This class currently delegates to DecimalFormat for parsing and
30 * formatting.
31 *
32 * @see android.icu.text.UFormat
33 * @see android.icu.text.DecimalFormat
34 * @author Alan Liu
35 */
36class CurrencyFormat extends MeasureFormat {
37    // Generated by serialver from JDK 1.4.1_01
38    static final long serialVersionUID = -931679363692504634L;
39
40    private NumberFormat fmt;
41    private transient final MeasureFormat mf;
42
43    public CurrencyFormat(ULocale locale) {
44        // Needed for getLocale(ULocale.VALID_LOCALE).
45        setLocale(locale, locale);
46        mf = MeasureFormat.getInstance(locale, FormatWidth.WIDE);
47        fmt = NumberFormat.getCurrencyInstance(locale.toLocale());
48    }
49
50    /**
51     * {@inheritDoc}
52     */
53    @Override
54    public Object clone() {
55        CurrencyFormat result = (CurrencyFormat) super.clone();
56        result.fmt = (NumberFormat) fmt.clone();
57        return result;
58    }
59
60    /**
61     * Override Format.format().
62     * @see java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)
63     */
64    public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
65        if (!(obj instanceof CurrencyAmount)) {
66            throw new IllegalArgumentException("Invalid type: " + obj.getClass().getName());
67        }
68        CurrencyAmount currency = (CurrencyAmount) obj;
69
70        fmt.setCurrency(currency.getCurrency());
71        return fmt.format(currency.getNumber(), toAppendTo, pos);
72    }
73
74    /**
75     * Override Format.parseObject().
76     * @see java.text.Format#parseObject(java.lang.String, java.text.ParsePosition)
77     */
78    @Override
79    public CurrencyAmount parseObject(String source, ParsePosition pos) {
80        return fmt.parseCurrency(source, pos);
81    }
82
83    // boilerplate code to make CurrencyFormat otherwise follow the contract of
84    // MeasureFormat
85
86    /**
87     * {@inheritDoc}
88     */
89    @Override
90    public StringBuilder formatMeasures(
91            StringBuilder appendTo, FieldPosition fieldPosition, Measure... measures) {
92        return mf.formatMeasures(appendTo, fieldPosition, measures);
93    }
94
95    /**
96     * {@inheritDoc}
97     */
98    @Override
99    public MeasureFormat.FormatWidth getWidth() {
100        return mf.getWidth();
101    }
102
103    /**
104     * {@inheritDoc}
105     */
106    @Override
107    public NumberFormat getNumberFormat() {
108        return mf.getNumberFormat();
109    }
110
111    // End boilerplate.
112
113    // Serialization
114
115    private Object writeReplace() throws ObjectStreamException {
116        return mf.toCurrencyProxy();
117    }
118
119    // Preserve backward serialize backward compatibility.
120    private Object readResolve() throws ObjectStreamException {
121        return new CurrencyFormat(fmt.getLocale(ULocale.ACTUAL_LOCALE));
122    }
123}
124