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