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