ICU-6445 add abbreviation support

X-SVN-Rev: 24588
This commit is contained in:
Xiaomei Ji 2008-09-17 19:57:42 +00:00
parent f7af84928f
commit 5a154f887e
3 changed files with 229 additions and 107 deletions

View file

@ -9,6 +9,7 @@ package com.ibm.icu.dev.test.format;
import java.text.ParseException;
import com.ibm.icu.dev.test.TestFmwk;
import com.ibm.icu.text.NumberFormat;
import com.ibm.icu.text.TimeUnitFormat;
import com.ibm.icu.util.TimeUnit;
import com.ibm.icu.util.TimeUnitAmount;
@ -24,31 +25,81 @@ public class TimeUnitTest extends TestFmwk {
}
public void TestBasic() {
TimeUnitFormat format = new TimeUnitFormat();
//String[] locales = {"en", "sl", "fr", "zh", "ar"};
String[] locales = {"ar", "ru", "en", "sl", "fr", "zh"};
String[] locales = {"en", "sl", "fr", "zh", "ar", "ru", "zh_Hant"};
for ( int locIndex = 0; locIndex < locales.length; ++locIndex ) {
format.setLocale(new ULocale(locales[locIndex]));
//System.out.println(locales[locIndex]);
final TimeUnit[] values = TimeUnit.values();
for (int j = 0; j < values.length; ++j) {
final TimeUnit timeUnit = values[j];
double[] tests = {0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 5, 10, 100, 101.35};
for (int i = 0; i < tests.length; ++i) {
TimeUnitAmount source = new TimeUnitAmount(tests[i], timeUnit);
String formatted = format.format(source);
//System.out.println(formatted);
logln(tests[i] + " => " + formatted);
try {
TimeUnitAmount result = (TimeUnitAmount) format.parseObject(formatted);
if (result == null || !source.equals(result)) {
errln("No round trip: " + source + " => " + formatted + " => " + result);
//System.out.println("locale: " + locales[locIndex]);
Object[] formats = new Object[] {
new TimeUnitFormat(new ULocale(locales[locIndex]), TimeUnitFormat.FULL_NAME),
new TimeUnitFormat(new ULocale(locales[locIndex]), TimeUnitFormat.ABBREVIATED_NAME)
};
for (int style = TimeUnitFormat.FULL_NAME;
style <= TimeUnitFormat.ABBREVIATED_NAME;
++style) {
final TimeUnit[] values = TimeUnit.values();
for (int j = 0; j < values.length; ++j) {
final TimeUnit timeUnit = values[j];
double[] tests = {0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 5, 10, 100, 101.35};
for (int i = 0; i < tests.length; ++i) {
TimeUnitAmount source = new TimeUnitAmount(tests[i], timeUnit);
String formatted = ((TimeUnitFormat)formats[style]).format(source);
//System.out.println(formatted);
logln(tests[i] + " => " + formatted);
try {
TimeUnitAmount result = (TimeUnitAmount) ((TimeUnitFormat)formats[style]).parseObject(formatted);
if (result == null || !source.equals(result)) {
errln("No round trip: " + source + " => " + formatted + " => " + result);
}
// mix style parsing
result = (TimeUnitAmount) ((TimeUnitFormat)formats[1 - style]).parseObject(formatted);
if (result == null || !source.equals(result)) {
errln("No round trip: " + source + " => " + formatted + " => " + result);
}
} catch (ParseException e) {
errln(e.getMessage());
}
} catch (ParseException e) {
errln(e.getMessage());
}
}
}
}
}
public void TestAPI() {
TimeUnitFormat format = new TimeUnitFormat();
format.setLocale(new ULocale("pt_BR"));
formatParsing(format);
format = new TimeUnitFormat(new ULocale("de"));
formatParsing(format);
format = new TimeUnitFormat(new ULocale("ja"));
format.setNumberFormat(NumberFormat.getNumberInstance(new ULocale("en")));
formatParsing(format);
format = new TimeUnitFormat();
ULocale es = new ULocale("es");
format.setNumberFormat(NumberFormat.getNumberInstance(es));
format.setLocale(es);
formatParsing(format);
}
private void formatParsing(TimeUnitFormat format) {
final TimeUnit[] values = TimeUnit.values();
for (int j = 0; j < values.length; ++j) {
final TimeUnit timeUnit = values[j];
double[] tests = {0, 0.5, 1, 2, 3, 5};
for (int i = 0; i < tests.length; ++i) {
TimeUnitAmount source = new TimeUnitAmount(tests[i], timeUnit);
String formatted = format.format(source);
//System.out.println(formatted);
logln(tests[i] + " => " + formatted);
try {
TimeUnitAmount result = (TimeUnitAmount) format.parseObject(formatted);
if (result == null || !source.equals(result)) {
errln("No round trip: " + source + " => " + formatted + " => " + result);
}
} catch (ParseException e) {
errln(e.getMessage());
}
}
}
}
}

View file

@ -58,13 +58,30 @@ 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".
* @draft ICU 4.1.1
* @provisional This API might change or be removed in a future release.
*/
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".
* @draft ICU 4.1.1
* @provisional This API might change or be removed in a future release.
*/
public static final int ABBREVIATED_NAME = 1;
private static final int TOTAL_STYLES = 2;
private static final long serialVersionUID = -3707773153184971529L;
private static final String DEFAULT_PATTERN_FOR_SECOND = "{0} s";
private static final String DEFAULT_PATTERN_FOR_MINUTE = "{0} min";
private static final String DEFAULT_PATTERN_FOR_HOUR = "{0} h";
private static final String DEFAULT_PATTERN_FOR_WEEK = "{0} w";
private static final String DEFAULT_PATTERN_FOR_DAY = "{0} d";
private static final String DEFAULT_PATTERN_FOR_WEEK = "{0} w";
private static final String DEFAULT_PATTERN_FOR_MONTH = "{0} m";
private static final String DEFAULT_PATTERN_FOR_YEAR = "{0} y";
@ -73,36 +90,72 @@ public class TimeUnitFormat extends MeasureFormat {
private transient Map timeUnitToCountToPatterns;
private transient PluralRules pluralRules;
private transient boolean isReady;
private int style;
/**
* Create empty format. Use setLocale and/or setFormat to modify.
* Create empty format using full name style, for example, "hours".
* Use setLocale and/or setFormat to modify.
* @draft ICU 4.0
* @provisional This API might change or be removed in a future release.
*/
public TimeUnitFormat() {}
public TimeUnitFormat() {
isReady = false;
style = FULL_NAME;
}
/**
* Create TimeUnitFormat given a ULocale.
* Create TimeUnitFormat given a ULocale, and using full name style.
* @param locale locale of this time unit formatter.
* @draft ICU 4.0
* @provisional This API might change or be removed in a future release.
*/
public TimeUnitFormat(ULocale locale) {
this(locale, FULL_NAME);
}
/**
* Create TimeUnitFormat given a Locale, and using full name style.
* @param locale locale of this time unit formatter.
* @draft ICU 4.0
* @provisional This API might change or be removed in a future release.
*/
public TimeUnitFormat(Locale locale) {
this(locale, FULL_NAME);
}
/**
* Create TimeUnitFormat given a ULocale and a formatting style: full or
* abbreviated.
* @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.
* @draft ICU 4.1.1
* @provisional This API might change or be removed in a future release.
*/
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");
}
this.style = style;
this.locale = locale;
isReady = false;
}
/**
* Create TimeUnitFormat given a Locale.
* @draft ICU 4.0
* Create TimeUnitFormat given a Locale and a formatting style: full or
* abbreviated.
* @draft ICU 4.1.1
* @provisional This API might change or be removed in a future release.
*/
public TimeUnitFormat(Locale locale) {
this.locale = ULocale.forLocale(locale);
isReady = false;
public TimeUnitFormat(Locale locale, int style) {
this(ULocale.forLocale(locale), style);
}
/**
* Set the locale used for formatting or parsing.
* @param locale locale of this time unit formatter.
* @return this, for chaining.
* @draft ICU 4.0
* @provisional This API might change or be removed in a future release.
@ -115,6 +168,7 @@ public class TimeUnitFormat extends MeasureFormat {
/**
* Set the locale used for formatting or parsing.
* @param locale locale of this time unit formatter.
* @return this, for chaining.
* @draft ICU 4.0
* @provisional This API might change or be removed in a future release.
@ -127,6 +181,7 @@ public class TimeUnitFormat extends MeasureFormat {
/**
* Set the format used for formatting or parsing. If null or not available, use the getNumberInstance(locale).
* @param format the number formatter.
* @return this, for chaining.
* @draft ICU 4.0
* @provisional This API might change or be removed in a future release.
@ -143,13 +198,19 @@ public class TimeUnitFormat extends MeasureFormat {
this.format = format;
}
// reset the number formatter in the timeUnitToCountToPatterns map
if (isReady == false) {
return this;
}
for (Iterator it = timeUnitToCountToPatterns.keySet().iterator();
it.hasNext();) {
TimeUnit timeUnit = (TimeUnit) it.next();
Map countToPattern = (Map) timeUnitToCountToPatterns.get(timeUnit);
for (Iterator it2 = countToPattern.keySet().iterator(); it2.hasNext();) {
String count = (String) it2.next();
MessageFormat pattern = (MessageFormat) countToPattern.get(count);
Object[] pair = (Object[])countToPattern.get(count);
MessageFormat pattern = (MessageFormat)pair[FULL_NAME];
pattern.setFormatByArgumentIndex(0, format);
pattern = (MessageFormat)pair[ABBREVIATED_NAME];
pattern.setFormatByArgumentIndex(0, format);
}
}
@ -175,7 +236,7 @@ public class TimeUnitFormat extends MeasureFormat {
Map countToPattern = (Map) timeUnitToCountToPatterns.get(amount.getTimeUnit());
double number = amount.getNumber().doubleValue();
String count = pluralRules.select(number);
MessageFormat pattern = (MessageFormat) countToPattern.get(count);
MessageFormat pattern = (MessageFormat)((Object[])countToPattern.get(count))[style];
return pattern.format(new Object[]{amount.getNumber()}, toAppendTo, pos);
}
@ -202,8 +263,9 @@ public class TimeUnitFormat extends MeasureFormat {
TimeUnit timeUnit = (TimeUnit) it.next();
Map countToPattern = (Map) timeUnitToCountToPatterns.get(timeUnit);
for (Iterator it2 = countToPattern.keySet().iterator(); it2.hasNext();) {
String count = (String) it2.next();
MessageFormat pattern = (MessageFormat) countToPattern.get(count);
String count = (String) it2.next();
for (int style = FULL_NAME; style < TOTAL_STYLES; ++style) {
MessageFormat pattern = (MessageFormat)((Object[])countToPattern.get(count))[style];
pos.setErrorIndex(-1);
pos.setIndex(oldPos);
// see if we can parse
@ -232,6 +294,7 @@ public class TimeUnitFormat extends MeasureFormat {
countOfLongestMatch = count;
}
}
}
}
/* After find the longest match, parse the number.
* Result number could be null for the pattern without number pattern.
@ -278,16 +341,46 @@ public class TimeUnitFormat extends MeasureFormat {
pluralRules = PluralRules.forLocale(locale);
timeUnitToCountToPatterns = new HashMap();
setup("units", timeUnitToCountToPatterns, FULL_NAME);
setup("short_units", timeUnitToCountToPatterns, ABBREVIATED_NAME);
isReady = true;
}
private void setup(String resourceKey, Map timeUnitToCountToPatterns,
int style) {
// fill timeUnitToCountToPatterns from resource file
try {
ICUResourceBundle resource = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, locale);
ICUResourceBundle unitsRes = resource.getWithFallback("units");
ICUResourceBundle unitsRes = resource.getWithFallback(resourceKey);
int size = unitsRes.getSize();
for ( int index = 0; index < size; ++index) {
String timeUnitName = unitsRes.get(index).getKey();
TimeUnit timeUnit = null;
if ( timeUnitName.equals("year") ) {
timeUnit = TimeUnit.YEAR;
} else if ( timeUnitName.equals("month") ) {
timeUnit = TimeUnit.MONTH;
} else if ( timeUnitName.equals("day") ) {
timeUnit = TimeUnit.DAY;
} else if ( timeUnitName.equals("hour") ) {
timeUnit = TimeUnit.HOUR;
} else if ( timeUnitName.equals("minute") ) {
timeUnit = TimeUnit.MINUTE;
} else if ( timeUnitName.equals("second") ) {
timeUnit = TimeUnit.SECOND;
} else if ( timeUnitName.equals("week") ) {
timeUnit = TimeUnit.WEEK;
} else {
continue;
}
ICUResourceBundle oneUnitRes = unitsRes.getWithFallback(timeUnitName);
int count = oneUnitRes.getSize();
Map countToPatterns = new TreeMap();
Map countToPatterns = (Map)timeUnitToCountToPatterns.get(timeUnit);
if (countToPatterns == null) {
countToPatterns = new TreeMap();
timeUnitToCountToPatterns.put(timeUnit, countToPatterns);
}
for ( int pluralIndex = 0; pluralIndex < count; ++pluralIndex) {
String pluralCount = oneUnitRes.get(pluralIndex).getKey();
String pattern = oneUnitRes.get(pluralIndex).getString();
@ -295,23 +388,17 @@ public class TimeUnitFormat extends MeasureFormat {
if (format != null) {
messageFormat.setFormatByArgumentIndex(0, format);
}
countToPatterns.put(pluralCount, messageFormat);
// save both full name and abbreviated name in one table
// is good space-wise, but it degrades performance,
// since it needs to check whether the needed space
// is already allocated or not.
Object[] pair = (Object[])countToPatterns.get(pluralCount);
if (pair == null) {
pair = new Object[2];
countToPatterns.put(pluralCount, pair);
}
pair[style] = messageFormat;
}
if ( timeUnitName.equals("year") ) {
timeUnitToCountToPatterns.put(TimeUnit.YEAR, countToPatterns);
} else if ( timeUnitName.equals("month") ) {
timeUnitToCountToPatterns.put(TimeUnit.MONTH, countToPatterns);
} else if ( timeUnitName.equals("day") ) {
timeUnitToCountToPatterns.put(TimeUnit.DAY, countToPatterns);
} else if ( timeUnitName.equals("hour") ) {
timeUnitToCountToPatterns.put(TimeUnit.HOUR, countToPatterns);
} else if ( timeUnitName.equals("minute") ) {
timeUnitToCountToPatterns.put(TimeUnit.MINUTE, countToPatterns);
} else if ( timeUnitName.equals("second") ) {
timeUnitToCountToPatterns.put(TimeUnit.SECOND, countToPatterns);
} else if ( timeUnitName.equals("week") ) {
timeUnitToCountToPatterns.put(TimeUnit.WEEK, countToPatterns);
}
}
} catch ( MissingResourceException e ) {
}
@ -341,23 +428,19 @@ public class TimeUnitFormat extends MeasureFormat {
// get all the patterns for each plural rule in this locale.
final TimeUnit timeUnit = timeUnits[i];
Map countToPatterns = (Map) timeUnitToCountToPatterns.get(timeUnit);
boolean previousEmpty = false;
if ( countToPatterns == null ) {
countToPatterns = new TreeMap();
previousEmpty = true;
timeUnitToCountToPatterns.put(timeUnit, countToPatterns);
}
for (Iterator it = keywords.iterator(); it.hasNext();) {
String pluralCount = (String) it.next();
if ( !countToPatterns.containsKey(pluralCount) ) {
if ( countToPatterns.get(pluralCount) == null ||
((Object[])countToPatterns.get(pluralCount))[style] == null ) {
// look through parents
searchInTree(timeUnit, pluralCount, pluralCount, countToPatterns);
searchInTree(resourceKey, style, timeUnit, pluralCount, pluralCount, countToPatterns);
}
}
if ( previousEmpty ) {
timeUnitToCountToPatterns.put(timeUnit, countToPatterns);
}
}
isReady = true;
}
@ -370,51 +453,32 @@ 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(TimeUnit timeUnit, String srcPluralCount,
private void searchInTree(String resourceKey, int style,
TimeUnit timeUnit, String srcPluralCount,
String searchPluralCount, Map countToPatterns) {
ULocale parentLocale=locale;
String srcTimeUnitName = timeUnit.toString();
boolean found = false;
try {
// look for pattern for srcPluralCount in locale tree
while ( parentLocale != null ) {
while ( parentLocale != null ) {
try {
// look for pattern for srcPluralCount in locale tree
ICUResourceBundle unitsRes = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, parentLocale);
unitsRes = unitsRes.getWithFallback("units");
int size = unitsRes.getSize();
for ( int index = 0; index < size; ++index) {
String timeUnitName = unitsRes.get(index).getKey();
if ( !timeUnitName.equalsIgnoreCase(srcTimeUnitName) ) {
continue;
}
ICUResourceBundle oneUnitRes = unitsRes.getWithFallback(timeUnitName);
int count = oneUnitRes.getSize();
for ( int pluralIndex = 0; pluralIndex < count;
++pluralIndex ) {
String pluralCount = oneUnitRes.get(pluralIndex).getKey();
if ( !pluralCount.equals(searchPluralCount) ) {
continue;
}
String pattern = oneUnitRes.get(pluralIndex).getString();
final MessageFormat messageFormat = new MessageFormat(pattern, locale);
if (format != null) {
messageFormat.setFormatByArgumentIndex(0, format);
}
countToPatterns.put(srcPluralCount, messageFormat);
found = true;
break;
}
// found the right timeUnit, break;
break;
unitsRes = unitsRes.getWithFallback(resourceKey);
ICUResourceBundle oneUnitRes = unitsRes.getWithFallback(srcTimeUnitName);
String pattern = oneUnitRes.getStringWithFallback(searchPluralCount);
final MessageFormat messageFormat = new MessageFormat(pattern, locale);
if (format != null) {
messageFormat.setFormatByArgumentIndex(0, format);
}
if ( found ) {
break;
Object[] pair = (Object[])countToPatterns.get(srcPluralCount);
if (pair == null) {
pair = new Object[2];
countToPatterns.put(srcPluralCount, pair);
}
parentLocale=parentLocale.getFallback();
pair[style] = messageFormat;
return;
} catch ( MissingResourceException e ) {
}
} catch ( MissingResourceException e ) {
}
if ( found ) {
return;
parentLocale=parentLocale.getFallback();
}
// if not found the pattern for this plural count at all,
// fall-back to plural count "other"
@ -436,13 +500,18 @@ public class TimeUnitFormat extends MeasureFormat {
} else if ( timeUnit == TimeUnit.YEAR ) {
messageFormat = new MessageFormat(DEFAULT_PATTERN_FOR_YEAR, locale);
}
if (format != null && messageFormat != null ) {
if (format != null && messageFormat != null) {
messageFormat.setFormatByArgumentIndex(0, format);
}
countToPatterns.put(srcPluralCount, messageFormat);
Object[] pair = (Object[])countToPatterns.get(srcPluralCount);
if (pair == null) {
pair = new Object[2];
countToPatterns.put(srcPluralCount, pair);
}
pair[style] = messageFormat;
} else {
// fall back to rule "other", and search in parents
searchInTree(timeUnit, srcPluralCount, "other", countToPatterns);
searchInTree(resourceKey, style, timeUnit, srcPluralCount, "other", countToPatterns);
}
}
}

View file

@ -29,13 +29,13 @@ public class TimeUnit extends MeasureUnit {
* @provisional This API might change or be removed in a future release.
*/
public static TimeUnit
SECOND = new TimeUnit("SECOND"),
MINUTE = new TimeUnit("MINUTE"),
HOUR = new TimeUnit("HOUR"),
DAY = new TimeUnit("DAY"),
WEEK = new TimeUnit("WEEK"),
MONTH = new TimeUnit("MONTH"),
YEAR = new TimeUnit("YEAR");
SECOND = new TimeUnit("second"),
MINUTE = new TimeUnit("minute"),
HOUR = new TimeUnit("hour"),
DAY = new TimeUnit("day"),
WEEK = new TimeUnit("week"),
MONTH = new TimeUnit("month"),
YEAR = new TimeUnit("year");
private TimeUnit(String name) {
this.name = name;
@ -53,6 +53,8 @@ public class TimeUnit extends MeasureUnit {
/**
* A string representation for debugging.
* It is for debugging purpose. The value might change.
* Please do not count on the value.
* @draft ICU 4.0
* @provisional This API might change or be removed in a future release.
*/