]> granicus.if.org Git - icu/commitdiff
ICU-8627 Lazily instantiate TimeZoneGenericNames also in J to improve the initial...
authorYoshito Umaoka <y.umaoka@gmail.com>
Thu, 9 Jun 2011 19:15:09 +0000 (19:15 +0000)
committerYoshito Umaoka <y.umaoka@gmail.com>
Thu, 9 Jun 2011 19:15:09 +0000 (19:15 +0000)
X-SVN-Rev: 30198

icu4j/main/classes/core/src/com/ibm/icu/text/TimeZoneFormat.java

index e5061868ce328bcf4ba033edaa181f80c7358145..45682e1fc8d4616a1c581b0e47815c98d3d37362 100644 (file)
@@ -210,7 +210,7 @@ public class TimeZoneFormat extends UFormat implements Freezable<TimeZoneFormat>
      */
     private ULocale _locale;
     private TimeZoneNames _tznames;
-    private TimeZoneGenericNames _gnames;
+    private volatile TimeZoneGenericNames _gnames;
     private String _gmtPattern;
     private String[] _gmtOffsetPatterns;
     private String[] _gmtOffsetDigits;
@@ -280,7 +280,7 @@ public class TimeZoneFormat extends UFormat implements Freezable<TimeZoneFormat>
     protected TimeZoneFormat(ULocale locale) {
         _locale = locale;
         _tznames = TimeZoneNames.getInstance(locale);
-        _gnames = TimeZoneGenericNames.getInstance(locale);
+        // TimeZoneGenericNames _gnames will be instantiated lazily
 
         String gmtPattern = null;
         String hourFormats = null;
@@ -365,6 +365,26 @@ public class TimeZoneFormat extends UFormat implements Freezable<TimeZoneFormat>
         return _tznames;
     }
 
+    /**
+     * Private method returning the instance of TimeZoneGenericNames
+     * used by this object. The instance of TimeZoneGenericNames might
+     * not be available until the first use (lazy instantiation) because
+     * it is only required for handling generic names (that are not used
+     * by DateFormat's default patterns) and it requires relatively heavy
+     * one time initialization.
+     * @return the instance of TimeZoneGenericNames used by this object.
+     */
+    private TimeZoneGenericNames getTimeZoneGenericNames() {
+        if (_gnames == null) { // _gnames is volatile
+            synchronized(this) {
+                if (_gnames == null) {
+                    _gnames = TimeZoneGenericNames.getInstance(_locale);
+                }
+            }
+        }
+        return _gnames;
+    }
+
     /**
      * Sets the time zone display name data to this instance.
      * 
@@ -747,13 +767,13 @@ public class TimeZoneFormat extends UFormat implements Freezable<TimeZoneFormat>
 
         switch (style) {
         case GENERIC_LOCATION:
-            result = _gnames.getGenericLocationName(ZoneMeta.getCanonicalCLDRID(tz));
+            result = getTimeZoneGenericNames().getGenericLocationName(ZoneMeta.getCanonicalCLDRID(tz));
             break;
         case GENERIC_LONG:
-            result = _gnames.getDisplayName(tz, GenericNameType.LONG, date);
+            result = getTimeZoneGenericNames().getDisplayName(tz, GenericNameType.LONG, date);
             break;
         case GENERIC_SHORT:
-            result = _gnames.getDisplayName(tz, GenericNameType.SHORT, date);
+            result = getTimeZoneGenericNames().getDisplayName(tz, GenericNameType.SHORT, date);
             break;
         case SPECIFIC_LONG:
             result = formatSpecific(tz, NameType.LONG_STANDARD, NameType.LONG_DAYLIGHT, date, timeType);
@@ -1168,7 +1188,7 @@ public class TimeZoneFormat extends UFormat implements Freezable<TimeZoneFormat>
                 genericNameTypes = EnumSet.of(GenericNameType.SHORT, GenericNameType.LOCATION);
                 break;
             }
-            GenericMatchInfo bestGeneric = _gnames.findBestMatch(text, startIdx, genericNameTypes);
+            GenericMatchInfo bestGeneric = getTimeZoneGenericNames().findBestMatch(text, startIdx, genericNameTypes);
             if (bestGeneric != null) {
                 if (timeType != null) {
                     timeType.value = bestGeneric.timeType();
@@ -1219,7 +1239,7 @@ public class TimeZoneFormat extends UFormat implements Freezable<TimeZoneFormat>
             }
 
             // Then generic names
-            GenericMatchInfo bestGeneric = _gnames.findBestMatch(text, startIdx, ALL_GENERIC_NAME_TYPES);
+            GenericMatchInfo bestGeneric = getTimeZoneGenericNames().findBestMatch(text, startIdx, ALL_GENERIC_NAME_TYPES);
 
             if (bestSpecific != null || bestGeneric != null) {
                 if (bestGeneric == null ||