]> granicus.if.org Git - icu/commitdiff
ICU-9042 in time unit format, ignore those plural counts that are not defined in...
authorXiaomei Ji <xji@google.com>
Fri, 8 Jun 2012 23:53:58 +0000 (23:53 +0000)
committerXiaomei Ji <xji@google.com>
Fri, 8 Jun 2012 23:53:58 +0000 (23:53 +0000)
X-SVN-Rev: 31930

icu4j/main/classes/core/src/com/ibm/icu/text/TimeUnitFormat.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/TimeUnitTest.java

index 2e6f06138cf04b1a2204c22ee546ef8a293aea80..44193975b8e6c91b1ad4e3bb0cb68dc1797e8a92 100644 (file)
@@ -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<TimeUnit, Map<String, Object[]>>();
 
-        setup("units", timeUnitToCountToPatterns, FULL_NAME);
-        setup("unitsShort", timeUnitToCountToPatterns, ABBREVIATED_NAME);
+        Set<String> pluralKeywords = pluralRules.getKeywords();
+        setup("units", timeUnitToCountToPatterns, FULL_NAME, pluralKeywords);
+        setup("unitsShort", timeUnitToCountToPatterns, ABBREVIATED_NAME, pluralKeywords);
         isReady = true;
     }
 
 
     private void setup(String resourceKey, Map<TimeUnit, Map<String, Object[]>> timeUnitToCountToPatterns,
-                       int style) {
+                       int style, Set<String> 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) {
index a5f64f314d1a853727aa9a59c572e8b1b4769448..61201767101edf44a03df8de1757f17b36aa016b 100644 (file)
@@ -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) {