Print this page
rev 5615 : 6336885: RFE: Locale Data Deployment Enhancements
4609153: Provide locale data for Indic locales
5104387: Support for gl_ES locale (galician language)
6337471: desktop/system locale preferences support
7056139: (cal) SPI support for locale-dependent Calendar parameters
7058206: Provide CalendarData SPI for week params and display field value names
7073852: Support multiple scripts for digits and decimal symbols per locale
7079560: [Fmt-Da] Context dependent month names support in SimpleDateFormat
7171324: getAvailableLocales() of locale sensitive services should return the actual availability of locales
7151414: (cal) Support calendar type identification
7168528: LocaleServiceProvider needs to be aware of Locale extensions
7171372: (cal) locale's default Calendar should be created if unknown calendar is specified
Summary: JEP 127: Improve Locale Data Packaging and Adopt Unicode CLDR Data (part 1 w/o Jigsaw. by Naoto Sato and Masayoshi Okutsu)

Split Close
Expand all
Collapse all
          --- old/src/share/classes/sun/util/TimeZoneNameUtility.java
          +++ new/src/share/classes/sun/util/locale/provider/TimeZoneNameUtility.java
   1    1  /*
   2      - * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
        2 + * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
   3    3   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4    4   *
   5    5   * This code is free software; you can redistribute it and/or modify it
   6    6   * under the terms of the GNU General Public License version 2 only, as
   7    7   * published by the Free Software Foundation.  Oracle designates this
   8    8   * particular file as subject to the "Classpath" exception as provided
   9    9   * by Oracle in the LICENSE file that accompanied this code.
  10   10   *
  11   11   * This code is distributed in the hope that it will be useful, but WITHOUT
  12   12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
↓ open down ↓ 3 lines elided ↑ open up ↑
  16   16   *
  17   17   * You should have received a copy of the GNU General Public License version
  18   18   * 2 along with this work; if not, write to the Free Software Foundation,
  19   19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20   20   *
  21   21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22   22   * or visit www.oracle.com if you need additional information or have any
  23   23   * questions.
  24   24   */
  25   25  
  26      -package sun.util;
       26 +package sun.util.locale.provider;
  27   27  
  28   28  import java.lang.ref.SoftReference;
  29   29  import java.util.Enumeration;
  30   30  import java.util.LinkedList;
  31   31  import java.util.List;
  32   32  import java.util.Locale;
  33   33  import java.util.Map;
  34      -import java.util.MissingResourceException;
  35   34  import java.util.Set;
  36   35  import java.util.concurrent.ConcurrentHashMap;
  37   36  import java.util.spi.TimeZoneNameProvider;
  38   37  import sun.util.calendar.ZoneInfo;
  39      -import sun.util.resources.LocaleData;
  40   38  import sun.util.resources.OpenListResourceBundle;
  41   39  
  42   40  /**
  43   41   * Utility class that deals with the localized time zone names
       42 + *
       43 + * @author Naoto Sato
  44   44   */
  45   45  public final class TimeZoneNameUtility {
  46   46  
  47   47      /**
  48   48       * cache to hold time zone resource bundles. Keyed by Locale
  49   49       */
  50   50      private static ConcurrentHashMap<Locale, SoftReference<OpenListResourceBundle>> cachedBundles =
  51      -        new ConcurrentHashMap<Locale, SoftReference<OpenListResourceBundle>>();
       51 +        new ConcurrentHashMap<>();
  52   52  
  53   53      /**
  54   54       * cache to hold time zone localized strings. Keyed by Locale
  55   55       */
  56   56      private static ConcurrentHashMap<Locale, SoftReference<String[][]>> cachedZoneData =
  57      -        new ConcurrentHashMap<Locale, SoftReference<String[][]>>();
       57 +        new ConcurrentHashMap<>();
  58   58  
  59   59      /**
  60   60       * get time zone localized strings. Enumerate all keys.
  61   61       */
  62      -    public static final String[][] getZoneStrings(Locale locale) {
       62 +    public static String[][] getZoneStrings(Locale locale) {
  63   63          String[][] zones;
  64   64          SoftReference<String[][]> data = cachedZoneData.get(locale);
  65   65  
  66   66          if (data == null || ((zones = data.get()) == null)) {
  67   67              zones = loadZoneStrings(locale);
  68      -            data = new SoftReference<String[][]>(zones);
       68 +            data = new SoftReference<>(zones);
  69   69              cachedZoneData.put(locale, data);
  70   70          }
  71   71  
  72   72          return zones;
  73   73      }
  74   74  
  75      -    private static final String[][] loadZoneStrings(Locale locale) {
  76      -        List<String[]> zones = new LinkedList<String[]>();
       75 +    private static String[][] loadZoneStrings(Locale locale) {
       76 +        List<String[]> zones = new LinkedList<>();
  77   77          OpenListResourceBundle rb = getBundle(locale);
  78   78          Enumeration<String> keys = rb.getKeys();
  79      -        String[] names = null;
       79 +        String[] names;
  80   80  
  81   81          while(keys.hasMoreElements()) {
  82   82              String key = keys.nextElement();
  83   83  
  84   84              names = retrieveDisplayNames(rb, key, locale);
  85   85              if (names != null) {
  86   86                  zones.add(names);
  87   87              }
  88   88          }
  89   89  
  90   90          String[][] zonesArray = new String[zones.size()][];
  91   91          return zones.toArray(zonesArray);
  92   92      }
  93   93  
  94   94      /**
  95   95       * Retrieve display names for a time zone ID.
  96   96       */
  97      -    public static final String[] retrieveDisplayNames(String id, Locale locale) {
       97 +    public static String[] retrieveDisplayNames(String id, Locale locale) {
  98   98          OpenListResourceBundle rb = getBundle(locale);
  99   99          return retrieveDisplayNames(rb, id, locale);
 100  100      }
 101  101  
 102      -    private static final String[] retrieveDisplayNames(OpenListResourceBundle rb,
      102 +    private static String[] retrieveDisplayNames(OpenListResourceBundle rb,
 103  103                                                  String id, Locale locale) {
 104      -        LocaleServiceProviderPool pool =
 105      -            LocaleServiceProviderPool.getPool(TimeZoneNameProvider.class);
 106      -        String[] names = null;
 107      -
 108      -        // Check whether a provider can provide an implementation that's closer
 109      -        // to the requested locale than what the Java runtime itself can provide.
 110      -        if (pool.hasProviders()) {
 111      -            names = pool.getLocalizedObject(
 112      -                            TimeZoneNameGetter.INSTANCE,
 113      -                            locale, rb, id);
      104 +        if (id == null || locale == null) {
      105 +            throw new NullPointerException();
 114  106          }
 115  107  
 116      -        if (names == null) {
 117      -            try {
 118      -                names = rb.getStringArray(id);
 119      -            } catch (MissingResourceException mre) {
 120      -                // fall through
 121      -            }
 122      -        }
 123      -
 124      -        return names;
      108 +        LocaleServiceProviderPool pool =
      109 +            LocaleServiceProviderPool.getPool(TimeZoneNameProvider.class);
      110 +        return pool.getLocalizedObject(TimeZoneNameGetter.INSTANCE, locale, id);
 125  111      }
 126  112  
 127      -    private static final OpenListResourceBundle getBundle(Locale locale) {
      113 +    private static OpenListResourceBundle getBundle(Locale locale) {
 128  114          OpenListResourceBundle rb;
 129  115          SoftReference<OpenListResourceBundle> data = cachedBundles.get(locale);
 130  116  
 131  117          if (data == null || ((rb = data.get()) == null)) {
 132      -            rb = LocaleData.getTimeZoneNames(locale);
 133      -            data = new SoftReference<OpenListResourceBundle>(rb);
      118 +            rb = LocaleProviderAdapter.forJRE().getLocaleData().getTimeZoneNames(locale);
      119 +            data = new SoftReference<>(rb);
 134  120              cachedBundles.put(locale, data);
 135  121          }
 136  122  
 137  123          return rb;
 138  124      }
 139  125  
 140  126      /**
 141  127       * Obtains a localized time zone strings from a TimeZoneNameProvider
 142  128       * implementation.
 143  129       */
 144  130      private static class TimeZoneNameGetter
 145  131          implements LocaleServiceProviderPool.LocalizedObjectGetter<TimeZoneNameProvider,
 146  132                                                                     String[]>{
 147  133          private static final TimeZoneNameGetter INSTANCE =
 148  134              new TimeZoneNameGetter();
 149  135  
      136 +        @Override
 150  137          public String[] getObject(TimeZoneNameProvider timeZoneNameProvider,
 151  138                                  Locale locale,
 152  139                                  String requestID,
 153  140                                  Object... params) {
 154  141              assert params.length == 0;
 155      -            String[] names = null;
 156  142              String queryID = requestID;
 157  143  
 158      -            if (queryID.equals("GMT")) {
 159      -                names = buildZoneStrings(timeZoneNameProvider, locale, queryID);
 160      -            } else {
      144 +            // First, try to get names with the request ID
      145 +            String[] names = buildZoneStrings(timeZoneNameProvider, locale, requestID);
      146 +
      147 +            if (names == null) {
 161  148                  Map<String, String> aliases = ZoneInfo.getAliasTable();
 162  149  
 163  150                  if (aliases != null) {
 164  151                      // Check whether this id is an alias, if so,
 165  152                      // look for the standard id.
 166  153                      if (aliases.containsKey(queryID)) {
 167  154                          String prevID = queryID;
 168  155                          while ((queryID = aliases.get(queryID)) != null) {
 169  156                              prevID = queryID;
 170  157                          }
↓ open down ↓ 54 lines elided ↑ open up ↑
 225  212              }
 226  213  
 227  214              if (names[1] == null) {
 228  215                  // this id seems not localized by this provider
 229  216                  names = null;
 230  217              }
 231  218  
 232  219              return names;
 233  220          }
 234  221      }
      222 +
      223 +    // No instantiation
      224 +    private TimeZoneNameUtility() {
      225 +    }
 235  226  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX