ICU-13526 Running auto-formatter on MeasureFormat.java and dependencies in advance of upcoming refactor.

X-SVN-Rev: 40772
This commit is contained in:
Shane Carr 2018-01-11 00:50:24 +00:00
parent 0344ea5118
commit 476d2690e8
3 changed files with 437 additions and 364 deletions

View File

@ -21,12 +21,11 @@ import com.ibm.icu.util.Measure;
import com.ibm.icu.util.ULocale;
/**
* Temporary internal concrete subclass of MeasureFormat implementing
* parsing and formatting of CurrencyAmount objects. This class is
* likely to be redesigned and rewritten in the near future.
* Temporary internal concrete subclass of MeasureFormat implementing parsing and formatting of
* CurrencyAmount objects. This class is likely to be redesigned and rewritten in the near future.
*
* <p>This class currently delegates to DecimalFormat for parsing and
* formatting.
* <p>
* This class currently delegates to DecimalFormat for parsing and formatting.
*
* @see com.ibm.icu.text.UFormat
* @see com.ibm.icu.text.DecimalFormat
@ -35,7 +34,7 @@ import com.ibm.icu.util.ULocale;
class CurrencyFormat extends MeasureFormat {
// Generated by serialver from JDK 1.4.1_01
static final long serialVersionUID = -931679363692504634L;
private NumberFormat fmt;
private transient final MeasureFormat mf;
@ -45,7 +44,7 @@ class CurrencyFormat extends MeasureFormat {
mf = MeasureFormat.getInstance(locale, FormatWidth.WIDE);
fmt = NumberFormat.getCurrencyInstance(locale.toLocale());
}
/**
* {@inheritDoc}
*/
@ -58,39 +57,44 @@ class CurrencyFormat extends MeasureFormat {
/**
* Override Format.format().
*
* @see java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)
*/
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
if (!(obj instanceof CurrencyAmount)) {
throw new IllegalArgumentException("Invalid type: " + obj.getClass().getName());
}
CurrencyAmount currency = (CurrencyAmount) obj;
fmt.setCurrency(currency.getCurrency());
return fmt.format(currency.getNumber(), toAppendTo, pos);
}
/**
* Override Format.parseObject().
*
* @see java.text.Format#parseObject(java.lang.String, java.text.ParsePosition)
*/
@Override
public CurrencyAmount parseObject(String source, ParsePosition pos) {
return fmt.parseCurrency(source, pos);
}
// boilerplate code to make CurrencyFormat otherwise follow the contract of
// MeasureFormat
/**
* {@inheritDoc}
*/
@Override
public StringBuilder formatMeasures(
StringBuilder appendTo, FieldPosition fieldPosition, Measure... measures) {
StringBuilder appendTo,
FieldPosition fieldPosition,
Measure... measures) {
return mf.formatMeasures(appendTo, fieldPosition, measures);
}
/**
* {@inheritDoc}
*/
@ -98,7 +102,7 @@ class CurrencyFormat extends MeasureFormat {
public MeasureFormat.FormatWidth getWidth() {
return mf.getWidth();
}
/**
* {@inheritDoc}
*/
@ -106,15 +110,15 @@ class CurrencyFormat extends MeasureFormat {
public NumberFormat getNumberFormat() {
return mf.getNumberFormat();
}
// End boilerplate.
// Serialization
private Object writeReplace() throws ObjectStreamException {
return mf.toCurrencyProxy();
}
// Preserve backward serialize backward compatibility.
private Object readResolve() throws ObjectStreamException {
return new CurrencyFormat(fmt.getLocale(ULocale.ACTUAL_LOCALE));

View File

@ -30,34 +30,35 @@ import com.ibm.icu.util.ULocale;
import com.ibm.icu.util.ULocale.Category;
import com.ibm.icu.util.UResourceBundle;
/**
* Format or parse a TimeUnitAmount, using plural rules for the units where available.
*
* <P>
* Code Sample:
*
* <pre>
* // create a time unit instance.
* // only SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, and YEAR are supported
* TimeUnit timeUnit = TimeUnit.SECOND;
* // create time unit amount instance - a combination of Number and time unit
* TimeUnitAmount source = new TimeUnitAmount(2, timeUnit);
* // create time unit format instance
* TimeUnitFormat format = new TimeUnitFormat();
* // set the locale of time unit format
* format.setLocale(new ULocale("en"));
* // format a time unit amount
* String formatted = format.format(source);
* System.out.println(formatted);
* try {
* // parse a string into time unit amount
* TimeUnitAmount result = (TimeUnitAmount) format.parseObject(formatted);
* // result should equal to source
* } catch (ParseException e) {
* }
* // create a time unit instance.
* // only SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, and YEAR are supported
* TimeUnit timeUnit = TimeUnit.SECOND;
* // create time unit amount instance - a combination of Number and time unit
* TimeUnitAmount source = new TimeUnitAmount(2, timeUnit);
* // create time unit format instance
* TimeUnitFormat format = new TimeUnitFormat();
* // set the locale of time unit format
* format.setLocale(new ULocale("en"));
* // format a time unit amount
* String formatted = format.format(source);
* System.out.println(formatted);
* try {
* // parse a string into time unit amount
* TimeUnitAmount result = (TimeUnitAmount) format.parseObject(formatted);
* // result should equal to source
* } catch (ParseException e) {
* }
* </pre>
*
* <P>
*
* @see TimeUnitAmount
* @see MeasureFormat
* @author markdavis
@ -67,15 +68,17 @@ import com.ibm.icu.util.UResourceBundle;
public class TimeUnitFormat extends MeasureFormat {
/**
* Constant for full name style format.
* For example, the full name for "hour" in English is "hour" or "hours".
* Constant for full name style format. For example, the full name for "hour" in English is "hour" or
* "hours".
*
* @deprecated ICU 53 see {@link MeasureFormat.FormatWidth}
*/
@Deprecated
public static final int FULL_NAME = 0;
/**
* Constant for abbreviated name style format.
* For example, the abbreviated name for "hour" in English is "hr" or "hrs".
* Constant for abbreviated name style format. For example, the abbreviated name for "hour" in
* English is "hr" or "hrs".
*
* @deprecated ICU 53 see {@link MeasureFormat.FormatWidth}
*/
@Deprecated
@ -111,8 +114,9 @@ public class TimeUnitFormat extends MeasureFormat {
private static final String DEFAULT_PATTERN_FOR_YEAR = "{0} y";
/**
* Create empty format using full name style, for example, "hours".
* Use setLocale and/or setFormat to modify.
* Create empty format using full name style, for example, "hours". Use setLocale and/or setFormat to
* modify.
*
* @deprecated ICU 53 use {@link MeasureFormat} instead.
*/
@Deprecated
@ -124,7 +128,9 @@ public class TimeUnitFormat extends MeasureFormat {
/**
* Create TimeUnitFormat given a ULocale, and using full name style.
* @param locale locale of this time unit formatter.
*
* @param locale
* locale of this time unit formatter.
* @deprecated ICU 53 use {@link MeasureFormat} instead.
*/
@Deprecated
@ -134,7 +140,9 @@ public class TimeUnitFormat extends MeasureFormat {
/**
* Create TimeUnitFormat given a Locale, and using full name style.
* @param locale locale of this time unit formatter.
*
* @param locale
* locale of this time unit formatter.
* @deprecated ICU 53 use {@link MeasureFormat} instead.
*/
@Deprecated
@ -144,19 +152,23 @@ public class TimeUnitFormat extends MeasureFormat {
/**
* Create TimeUnitFormat given a ULocale and a formatting style.
* @param locale locale of this time unit formatter.
* @param style format style, either FULL_NAME or ABBREVIATED_NAME style.
* @throws IllegalArgumentException if the style is not FULL_NAME or
* ABBREVIATED_NAME style.
*
* @param locale
* locale of this time unit formatter.
* @param style
* format style, either FULL_NAME or ABBREVIATED_NAME style.
* @throws IllegalArgumentException
* if the style is not FULL_NAME or ABBREVIATED_NAME style.
* @deprecated ICU 53 use {@link MeasureFormat} instead.
*/
@Deprecated
public TimeUnitFormat(ULocale locale, int style) {
if (style < FULL_NAME || style >= TOTAL_STYLES) {
throw new IllegalArgumentException("style should be either FULL_NAME or ABBREVIATED_NAME style");
throw new IllegalArgumentException(
"style should be either FULL_NAME or ABBREVIATED_NAME style");
}
mf = MeasureFormat.getInstance(
locale, style == FULL_NAME ? FormatWidth.WIDE : FormatWidth.SHORT);
mf = MeasureFormat.getInstance(locale,
style == FULL_NAME ? FormatWidth.WIDE : FormatWidth.SHORT);
this.style = style;
// Needed for getLocale(ULocale.VALID_LOCALE)
@ -174,22 +186,25 @@ public class TimeUnitFormat extends MeasureFormat {
/**
* Create TimeUnitFormat given a Locale and a formatting style.
*
* @deprecated ICU 53 use {@link MeasureFormat} instead.
*/
@Deprecated
public TimeUnitFormat(Locale locale, int style) {
this(ULocale.forLocale(locale), style);
this(ULocale.forLocale(locale), style);
}
/**
* Set the locale used for formatting or parsing.
* @param locale locale of this time unit formatter.
*
* @param locale
* locale of this time unit formatter.
* @return this, for chaining.
* @deprecated ICU 53 see {@link MeasureFormat}.
*/
@Deprecated
public TimeUnitFormat setLocale(ULocale locale) {
if (locale != this.locale){
if (locale != this.locale) {
mf = mf.withLocale(locale);
// Needed for getLocale(ULocale.VALID_LOCALE)
@ -202,7 +217,9 @@ public class TimeUnitFormat extends MeasureFormat {
/**
* Set the locale used for formatting or parsing.
* @param locale locale of this time unit formatter.
*
* @param locale
* locale of this time unit formatter.
* @return this, for chaining.
* @deprecated ICU 53 see {@link MeasureFormat}.
*/
@ -214,7 +231,9 @@ public class TimeUnitFormat extends MeasureFormat {
/**
* Set the format used for formatting or parsing. Passing null is equivalent to passing
* {@link NumberFormat#getNumberInstance(ULocale)}.
* @param format the number formatter.
*
* @param format
* the number formatter.
* @return this, for chaining.
* @deprecated ICU 53 see {@link MeasureFormat}.
*/
@ -238,20 +257,21 @@ public class TimeUnitFormat extends MeasureFormat {
return this;
}
/**
* Format a TimeUnitAmount.
*
* @see java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)
* @deprecated ICU 53 see {@link MeasureFormat}.
*/
@Override
@Deprecated
public StringBuffer format(Object obj, StringBuffer toAppendTo,
FieldPosition pos) {
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
return mf.format(obj, toAppendTo, pos);
}
/**
* Parse a TimeUnitAmount.
*
* @see java.text.Format#parseObject(java.lang.String, java.text.ParsePosition)
* @deprecated ICU 53 see {@link MeasureFormat}.
*/
@ -293,9 +313,11 @@ public class TimeUnitFormat extends MeasureFormat {
if (tempObj instanceof Number) {
temp = (Number) tempObj;
} else {
// Since we now format the number ourselves, parseObject will likely give us back a String
// Since we now format the number ourselves, parseObject will likely give us
// back a String
// for
// the number. When this happens we must parse the formatted number ourselves.
// the number. When this happens we must parse the formatted number
// ourselves.
try {
temp = format.parse(tempObj.toString());
} catch (ParseException e) {
@ -315,8 +337,9 @@ public class TimeUnitFormat extends MeasureFormat {
}
}
/*
* After find the longest match, parse the number. Result number could be null for the pattern without number
* pattern. such as unit pattern in Arabic. When result number is null, use plural rule to set the number.
* After find the longest match, parse the number. Result number could be null for the pattern
* without number pattern. such as unit pattern in Arabic. When result number is null, use plural
* rule to set the number.
*/
if (resultNumber == null && longestParseDistance != 0) {
// set the number using plurrual count
@ -371,8 +394,11 @@ public class TimeUnitFormat extends MeasureFormat {
ULocale locale;
boolean beenHere;
TimeUnitFormatSetupSink(Map<TimeUnit, Map<String, Object[]>> timeUnitToCountToPatterns,
int style, Set<String> pluralKeywords, ULocale locale) {
TimeUnitFormatSetupSink(
Map<TimeUnit, Map<String, Object[]>> timeUnitToCountToPatterns,
int style,
Set<String> pluralKeywords,
ULocale locale) {
this.timeUnitToCountToPatterns = timeUnitToCountToPatterns;
this.style = style;
this.pluralKeywords = pluralKeywords;
@ -388,7 +414,7 @@ public class TimeUnitFormat extends MeasureFormat {
} else {
beenHere = true;
}
UResource.Table units = value.getTable();
for (int i = 0; units.getKeyAndValue(i, key, value); ++i) {
String timeUnitName = key.toString();
@ -442,16 +468,21 @@ public class TimeUnitFormat extends MeasureFormat {
}
}
private void setup(String resourceKey, Map<TimeUnit, Map<String, Object[]>> timeUnitToCountToPatterns, int style,
private void setup(
String resourceKey,
Map<TimeUnit, Map<String, Object[]>> timeUnitToCountToPatterns,
int style,
Set<String> pluralKeywords) {
// fill timeUnitToCountToPatterns from resource file
try {
ICUResourceBundle resource = (ICUResourceBundle) UResourceBundle.getBundleInstance(
ICUData.ICU_UNIT_BASE_NAME, locale);
ICUResourceBundle resource = (ICUResourceBundle) UResourceBundle
.getBundleInstance(ICUData.ICU_UNIT_BASE_NAME, locale);
TimeUnitFormatSetupSink sink = new TimeUnitFormatSetupSink(
timeUnitToCountToPatterns, style, pluralKeywords, locale);
TimeUnitFormatSetupSink sink = new TimeUnitFormatSetupSink(timeUnitToCountToPatterns,
style,
pluralKeywords,
locale);
resource.getAllItemsWithFallback(resourceKey, sink);
} catch (MissingResourceException e) {
}
@ -485,9 +516,15 @@ public class TimeUnitFormat extends MeasureFormat {
timeUnitToCountToPatterns.put(timeUnit, countToPatterns);
}
for (String pluralCount : keywords) {
if (countToPatterns.get(pluralCount) == null || countToPatterns.get(pluralCount)[style] == null) {
if (countToPatterns.get(pluralCount) == null
|| countToPatterns.get(pluralCount)[style] == null) {
// look through parents
searchInTree(resourceKey, style, timeUnit, pluralCount, pluralCount, countToPatterns);
searchInTree(resourceKey,
style,
timeUnit,
pluralCount,
pluralCount,
countToPatterns);
}
}
}
@ -501,15 +538,20 @@ public class TimeUnitFormat extends MeasureFormat {
// if the pattern is not found even in root, fallback to
// using patterns of plural count "other",
// then, "other" is the searchPluralCount.
private void searchInTree(String resourceKey, int styl, TimeUnit timeUnit, String srcPluralCount,
String searchPluralCount, Map<String, Object[]> countToPatterns) {
private void searchInTree(
String resourceKey,
int styl,
TimeUnit timeUnit,
String srcPluralCount,
String searchPluralCount,
Map<String, Object[]> countToPatterns) {
ULocale parentLocale = locale;
String srcTimeUnitName = timeUnit.toString();
while (parentLocale != null) {
try {
// look for pattern for srcPluralCount in locale tree
ICUResourceBundle unitsRes = (ICUResourceBundle) UResourceBundle.getBundleInstance(
ICUData.ICU_UNIT_BASE_NAME, parentLocale);
ICUResourceBundle unitsRes = (ICUResourceBundle) UResourceBundle
.getBundleInstance(ICUData.ICU_UNIT_BASE_NAME, parentLocale);
unitsRes = unitsRes.getWithFallback(resourceKey);
ICUResourceBundle oneUnitRes = unitsRes.getWithFallback(srcTimeUnitName);
String pattern = oneUnitRes.getStringWithFallback(searchPluralCount);
@ -569,7 +611,6 @@ public class TimeUnitFormat extends MeasureFormat {
// boilerplate code to make TimeUnitFormat otherwise follow the contract of
// MeasureFormat
/**
* @internal
* @deprecated This API is ICU internal only.
@ -577,7 +618,9 @@ public class TimeUnitFormat extends MeasureFormat {
@Deprecated
@Override
public StringBuilder formatMeasures(
StringBuilder appendTo, FieldPosition fieldPosition, Measure... measures) {
StringBuilder appendTo,
FieldPosition fieldPosition,
Measure... measures) {
return mf.formatMeasures(appendTo, fieldPosition, measures);
}