diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/TimeUnitFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/TimeUnitFormat.java index 2e6f06138c..44193975b8 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/TimeUnitFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/TimeUnitFormat.java @@ -1,6 +1,6 @@ /* ************************************************************************** - * Copyright (C) 2008-2011, Google, International Business Machines + * Copyright (C) 2008-2012, Google, International Business Machines * Corporation and others. All Rights Reserved. ************************************************************************** */ @@ -333,14 +333,15 @@ public class TimeUnitFormat extends MeasureFormat { pluralRules = PluralRules.forLocale(locale); timeUnitToCountToPatterns = new HashMap>(); - setup("units", timeUnitToCountToPatterns, FULL_NAME); - setup("unitsShort", timeUnitToCountToPatterns, ABBREVIATED_NAME); + Set pluralKeywords = pluralRules.getKeywords(); + setup("units", timeUnitToCountToPatterns, FULL_NAME, pluralKeywords); + setup("unitsShort", timeUnitToCountToPatterns, ABBREVIATED_NAME, pluralKeywords); isReady = true; } private void setup(String resourceKey, Map> timeUnitToCountToPatterns, - int style) { + int style, Set pluralKeywords) { // fill timeUnitToCountToPatterns from resource file try { ICUResourceBundle resource = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, locale); @@ -375,6 +376,8 @@ public class TimeUnitFormat extends MeasureFormat { } for ( int pluralIndex = 0; pluralIndex < count; ++pluralIndex) { String pluralCount = oneUnitRes.get(pluralIndex).getKey(); + if (!pluralKeywords.contains(pluralCount)) + continue; String pattern = oneUnitRes.get(pluralIndex).getString(); final MessageFormat messageFormat = new MessageFormat(pattern, locale); if (format != null) { diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/TimeUnitTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/TimeUnitTest.java index a5f64f314d..6120176710 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/TimeUnitTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/TimeUnitTest.java @@ -1,6 +1,6 @@ /* ******************************************************************************* - * Copyright (C) 2008-2010, International Business Machines Corporation and * + * Copyright (C) 2008-2012, International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* */ @@ -182,6 +182,25 @@ public class TimeUnitTest extends TestFmwk { } } + /** + * @bug9042 + * Performs tests for Greek. + * This tests that if the plural count listed in time unit format does not + * match those in the plural rules for the locale, those plural count in + * time unit format will be ingored and subsequently, fall back will kick in + * which is tested above. + * Without data sanitization, setNumberFormat() would crash. + * As of CLDR shiped in ICU4.8, Greek is one such language. + */ + public void TestGreekWithSanitization() { + ULocale loc = new ULocale("el"); + NumberFormat numfmt = NumberFormat.getInstance(loc); + TimeUnitFormat tuf = new TimeUnitFormat(loc); + tuf.parseObject("", new ParsePosition(0)); + tuf.setNumberFormat(numfmt); + } + + private void formatParsing(TimeUnitFormat format) { final TimeUnit[] values = TimeUnit.values(); for (int j = 0; j < values.length; ++j) {