]> granicus.if.org Git - icu/commitdiff
ICU-11986 optimize UnitPatternSink a bit; add MeasureUnitTest.testIndividualPluralFal...
authorMarkus Scherer <markus.icu@gmail.com>
Wed, 2 Dec 2015 22:18:50 +0000 (22:18 +0000)
committerMarkus Scherer <markus.icu@gmail.com>
Wed, 2 Dec 2015 22:18:50 +0000 (22:18 +0000)
X-SVN-Rev: 38101

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

index 28ad541f3b7d6f9ce7328110816183861530c35e..601665f632faad42d09c329304bd493294f46daa 100644 (file)
@@ -746,6 +746,8 @@ public class MeasureFormat extends UFormat {
          * unitsShort/duration/hour contains other{"{0} hrs"}.
          */
         class UnitPatternSink extends UResource.TableSink {
+            QuantityFormatter countToFormat;
+
             @Override
             public void put(UResource.Key key, UResource.Value value) {
                 if (key.contentEquals("dnam")) {
@@ -757,20 +759,20 @@ public class MeasureFormat extends UFormat {
                     // The key must be one of the plural form strings. For example:
                     // one{"{0} hr"}
                     // other{"{0} hrs"}
-                    QuantityFormatter countToFormat;
-                    EnumMap<FormatWidth, QuantityFormatter> styleToCountToFormat =
-                            cacheData.unitToStyleToCountToFormat.get(unit);
-                    if (styleToCountToFormat == null) {
-                        styleToCountToFormat =
-                                new EnumMap<FormatWidth, QuantityFormatter>(FormatWidth.class);
-                        cacheData.unitToStyleToCountToFormat.put(unit, styleToCountToFormat);
-                        countToFormat = null;
-                    } else {
-                        countToFormat = styleToCountToFormat.get(width);
-                    }
                     if (countToFormat == null) {
-                        countToFormat = new QuantityFormatter();
-                        styleToCountToFormat.put(width, countToFormat);
+                        EnumMap<FormatWidth, QuantityFormatter> styleToCountToFormat =
+                                cacheData.unitToStyleToCountToFormat.get(unit);
+                        if (styleToCountToFormat == null) {
+                            styleToCountToFormat =
+                                    new EnumMap<FormatWidth, QuantityFormatter>(FormatWidth.class);
+                            cacheData.unitToStyleToCountToFormat.put(unit, styleToCountToFormat);
+                        } else {
+                            countToFormat = styleToCountToFormat.get(width);
+                        }
+                        if (countToFormat == null) {
+                            countToFormat = new QuantityFormatter();
+                            styleToCountToFormat.put(width, countToFormat);
+                        }
                     }
                     countToFormat.addIfAbsent(key, value);
                 }
@@ -787,6 +789,8 @@ public class MeasureFormat extends UFormat {
             public UResource.TableSink getOrCreateTableSink(UResource.Key key, int initialSize) {
                 // Should we ignore or reject unknown units?
                 unit = MeasureUnit.internalGetInstance(type, key.toString());  // never null
+                // Trigger a fresh lookup of the QuantityFormatter for this unit+width.
+                patternSink.countToFormat = null;
                 return patternSink;
             }
         }
index 390a27b3dd53a205f81d6d346b810bdc47de2a33..19da78c023a7c3225668b2e81064e5430c95f4e2 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *******************************************************************************
- * Copyright (C) 2013-2015, International Business Machines Corporation and         *
- * others. All Rights Reserved.                                                *
+ * Copyright (C) 2013-2015, International Business Machines Corporation and
+ * others. All Rights Reserved.
  *******************************************************************************
  */
 package com.ibm.icu.dev.test.format;
@@ -1364,7 +1364,16 @@ public class MeasureUnitTest extends TestFmwk {
                         new Measure(16, MeasureUnit.SECOND)));
         
     }
-    
+
+    public void testIndividualPluralFallback() {
+        // See ticket #11986 "incomplete fallback in MeasureFormat".
+        // In CLDR 28, fr_CA temperature-generic/short has only the "one" form,
+        // and falls back to fr for the "other" form.
+        MeasureFormat mf = MeasureFormat.getInstance(new ULocale("fr_CA"), FormatWidth.SHORT);
+        Measure twoDeg = new Measure(2, MeasureUnit.GENERIC_TEMPERATURE);
+        assertEquals("2 deg temp in fr_CA", "2°", mf.format(twoDeg));
+    }
+
     // DO NOT DELETE THIS FUNCTION! It may appear as dead code, but we use this to generate code
     // for MeasureFormat during the release process.
     static Map<MeasureUnit, Pair<MeasureUnit, MeasureUnit>> getUnitsToPerParts() {
index 5465b0ac4132c1178198d21a0621fdf78b2ad63c..ebca48a947c0720a4dd9263a08881a87f9bec4f2 100644 (file)
@@ -284,7 +284,7 @@ public class TimeUnitTest extends TestFmwk {
             try {
                 new TimeUnitFormat(locale, TimeUnitFormat.ABBREVIATED_NAME);
             } catch (RuntimeException e) {
-                errln("failed to load TimeUnitFormat data for " + locale);
+                errln("failed to load TimeUnitFormat data for " + locale + ": " + e);
             }
         }
     }