src/share/classes/sun/util/locale/provider/LocaleResources.java

Print this page




  37  * Taligent is a registered trademark of Taligent, Inc.
  38  *
  39  */
  40 
  41 package sun.util.locale.provider;
  42 
  43 import java.lang.ref.ReferenceQueue;
  44 import java.lang.ref.SoftReference;
  45 import java.text.MessageFormat;
  46 import java.util.Calendar;
  47 import java.util.LinkedHashSet;
  48 import java.util.Locale;
  49 import java.util.Map;
  50 import java.util.ResourceBundle;
  51 import java.util.Set;
  52 import java.util.concurrent.ConcurrentHashMap;
  53 import java.util.concurrent.ConcurrentMap;
  54 import sun.util.calendar.ZoneInfo;
  55 import sun.util.resources.LocaleData;
  56 import sun.util.resources.OpenListResourceBundle;

  57 import sun.util.resources.TimeZoneNamesBundle;
  58 
  59 /**
  60  * Central accessor to locale-dependent resources for JRE/CLDR provider adapters.
  61  *
  62  * @author Masayoshi Okutsu
  63  * @author Naoto Sato
  64  */
  65 public class LocaleResources {
  66 
  67     private final Locale locale;
  68     private final LocaleData localeData;
  69     private final LocaleProviderAdapter.Type type;
  70 
  71     // Resource cache
  72     private ConcurrentMap<String, ResourceReference> cache = new ConcurrentHashMap<>();
  73     private ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
  74 
  75     // cache key prefixes
  76     private static final String BREAK_ITERATOR_INFO = "BII.";


 314 
 315     String[] getCalendarNames(String key) {
 316         String[] names = null;
 317         String cacheKey = CALENDAR_NAMES + key;
 318 
 319         removeEmptyReferences();
 320         ResourceReference data = cache.get(cacheKey);
 321 
 322         if (data == null || ((names = (String[]) data.get()) == null)) {
 323             ResourceBundle rb = localeData.getDateFormatData(locale);
 324             if (rb.containsKey(key)) {
 325                 names = rb.getStringArray(key);
 326                 cache.put(cacheKey,
 327                           new ResourceReference(cacheKey, (Object) names, referenceQueue));
 328             }
 329         }
 330 
 331         return names;
 332     }
 333 



















 334     public String getDateTimePattern(int timeStyle, int dateStyle, Calendar cal) {
 335         if (cal == null) {
 336             cal = Calendar.getInstance(locale);
 337         }
 338         return getDateTimePattern(null, timeStyle, dateStyle, cal.getCalendarType());
 339     }
 340 
 341     /**
 342      * Returns a date-time format pattern
 343      * @param timeStyle style of time; one of FULL, LONG, MEDIUM, SHORT in DateFormat,
 344      *                  or -1 if not required
 345      * @param dateStyle style of time; one of FULL, LONG, MEDIUM, SHORT in DateFormat,
 346      *                  or -1 if not required
 347      * @param calType   the calendar type for the pattern
 348      * @return the pattern string
 349      */
 350     public String getCldrDateTimePattern(int timeStyle, int dateStyle, String calType) {
 351         calType = CalendarDataUtility.normalizeCalendarType(calType);
 352         String pattern;
 353         pattern = getDateTimePattern("cldr.", timeStyle, dateStyle, calType);
 354         if (pattern == null) {
 355             pattern = getDateTimePattern(null, timeStyle, dateStyle, calType);
 356         }
 357         return pattern;
 358     }
 359 
 360     private String getDateTimePattern(String prefix, int timeStyle, int dateStyle, String calType) {
 361         String pattern;
 362         String timePattern = null;
 363         String datePattern = null;
 364 
 365         if (timeStyle >= 0) {
 366             if (prefix != null) {
 367                 timePattern = getDateTimePattern(prefix, "TimePatterns", timeStyle, calType);
 368             }
 369             if (timePattern == null) {
 370                 timePattern = getDateTimePattern(null, "TimePatterns", timeStyle, calType);
 371             }
 372         }
 373         if (dateStyle >= 0) {


 413         String[] numberPatterns = null;
 414 
 415         removeEmptyReferences();
 416         ResourceReference data = cache.get(NUMBER_PATTERNS_CACHEKEY);
 417 
 418         if (data == null || ((numberPatterns = (String[]) data.get()) == null)) {
 419             ResourceBundle resource = localeData.getNumberFormatData(locale);
 420             numberPatterns = resource.getStringArray("NumberPatterns");
 421             cache.put(NUMBER_PATTERNS_CACHEKEY,
 422                       new ResourceReference(NUMBER_PATTERNS_CACHEKEY, (Object) numberPatterns, referenceQueue));
 423         }
 424 
 425         return numberPatterns;
 426     }
 427 
 428     /**
 429      * Returns the FormatData resource bundle of this LocaleResources.
 430      * The FormatData should be used only for accessing extra
 431      * resources required by JSR 310.
 432      */
 433     public ResourceBundle getFormatData() {
 434         return localeData.getDateFormatData(locale);




 435     }
 436 
 437     private String getDateTimePattern(String prefix, String key, int styleIndex, String calendarType) {
 438         StringBuilder sb = new StringBuilder();
 439         if (prefix != null) {
 440             sb.append(prefix);
 441         }
 442         if (!"gregory".equals(calendarType)) {
 443             sb.append(calendarType).append('.');
 444         }
 445         sb.append(key);
 446         String resourceKey = sb.toString();
 447         String cacheKey = sb.insert(0, DATE_TIME_PATTERN).toString();
 448 
 449         removeEmptyReferences();
 450         ResourceReference data = cache.get(cacheKey);
 451         Object value = NULLOBJECT;
 452 
 453         if (data == null || ((value = data.get()) == null)) {
 454             ResourceBundle r = localeData.getDateFormatData(locale);
 455             if (r.containsKey(resourceKey)) {
 456                 value = r.getStringArray(resourceKey);
 457             } else {
 458                 assert !resourceKey.equals(key);
 459                 if (r.containsKey(key)) {
 460                     value = r.getStringArray(key);
 461                 }
 462             }
 463             cache.put(cacheKey,
 464                       new ResourceReference(cacheKey, value, referenceQueue));
 465         }
 466         if (value == NULLOBJECT) {
 467             assert prefix != null;
 468             return null;
 469         }
 470         return ((String[])value)[styleIndex];
 471     }
 472 
 473     private static class ResourceReference extends SoftReference<Object> {
 474         private final String cacheKey;


  37  * Taligent is a registered trademark of Taligent, Inc.
  38  *
  39  */
  40 
  41 package sun.util.locale.provider;
  42 
  43 import java.lang.ref.ReferenceQueue;
  44 import java.lang.ref.SoftReference;
  45 import java.text.MessageFormat;
  46 import java.util.Calendar;
  47 import java.util.LinkedHashSet;
  48 import java.util.Locale;
  49 import java.util.Map;
  50 import java.util.ResourceBundle;
  51 import java.util.Set;
  52 import java.util.concurrent.ConcurrentHashMap;
  53 import java.util.concurrent.ConcurrentMap;
  54 import sun.util.calendar.ZoneInfo;
  55 import sun.util.resources.LocaleData;
  56 import sun.util.resources.OpenListResourceBundle;
  57 import sun.util.resources.ParallelListResourceBundle;
  58 import sun.util.resources.TimeZoneNamesBundle;
  59 
  60 /**
  61  * Central accessor to locale-dependent resources for JRE/CLDR provider adapters.
  62  *
  63  * @author Masayoshi Okutsu
  64  * @author Naoto Sato
  65  */
  66 public class LocaleResources {
  67 
  68     private final Locale locale;
  69     private final LocaleData localeData;
  70     private final LocaleProviderAdapter.Type type;
  71 
  72     // Resource cache
  73     private ConcurrentMap<String, ResourceReference> cache = new ConcurrentHashMap<>();
  74     private ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
  75 
  76     // cache key prefixes
  77     private static final String BREAK_ITERATOR_INFO = "BII.";


 315 
 316     String[] getCalendarNames(String key) {
 317         String[] names = null;
 318         String cacheKey = CALENDAR_NAMES + key;
 319 
 320         removeEmptyReferences();
 321         ResourceReference data = cache.get(cacheKey);
 322 
 323         if (data == null || ((names = (String[]) data.get()) == null)) {
 324             ResourceBundle rb = localeData.getDateFormatData(locale);
 325             if (rb.containsKey(key)) {
 326                 names = rb.getStringArray(key);
 327                 cache.put(cacheKey,
 328                           new ResourceReference(cacheKey, (Object) names, referenceQueue));
 329             }
 330         }
 331 
 332         return names;
 333     }
 334 
 335     String[] getJavaTimeNames(String key) {
 336         String[] names = null;
 337         String cacheKey = CALENDAR_NAMES + key;
 338 
 339         removeEmptyReferences();
 340         ResourceReference data = cache.get(cacheKey);
 341 
 342         if (data == null || ((names = (String[]) data.get()) == null)) {
 343             ResourceBundle rb = getJavaTimeFormatData();
 344             if (rb.containsKey(key)) {
 345                 names = rb.getStringArray(key);
 346                 cache.put(cacheKey,
 347                           new ResourceReference(cacheKey, (Object) names, referenceQueue));
 348             }
 349         }
 350 
 351         return names;
 352     }
 353 
 354     public String getDateTimePattern(int timeStyle, int dateStyle, Calendar cal) {
 355         if (cal == null) {
 356             cal = Calendar.getInstance(locale);
 357         }
 358         return getDateTimePattern(null, timeStyle, dateStyle, cal.getCalendarType());
 359     }
 360 
 361     /**
 362      * Returns a date-time format pattern
 363      * @param timeStyle style of time; one of FULL, LONG, MEDIUM, SHORT in DateFormat,
 364      *                  or -1 if not required
 365      * @param dateStyle style of time; one of FULL, LONG, MEDIUM, SHORT in DateFormat,
 366      *                  or -1 if not required
 367      * @param calType   the calendar type for the pattern
 368      * @return the pattern string
 369      */
 370     public String getCldrDateTimePattern(int timeStyle, int dateStyle, String calType) {
 371         calType = CalendarDataUtility.normalizeCalendarType(calType);
 372         String pattern;
 373         pattern = getDateTimePattern("java.time.", timeStyle, dateStyle, calType);
 374         if (pattern == null) {
 375             pattern = getDateTimePattern(null, timeStyle, dateStyle, calType);
 376         }
 377         return pattern;
 378     }
 379 
 380     private String getDateTimePattern(String prefix, int timeStyle, int dateStyle, String calType) {
 381         String pattern;
 382         String timePattern = null;
 383         String datePattern = null;
 384 
 385         if (timeStyle >= 0) {
 386             if (prefix != null) {
 387                 timePattern = getDateTimePattern(prefix, "TimePatterns", timeStyle, calType);
 388             }
 389             if (timePattern == null) {
 390                 timePattern = getDateTimePattern(null, "TimePatterns", timeStyle, calType);
 391             }
 392         }
 393         if (dateStyle >= 0) {


 433         String[] numberPatterns = null;
 434 
 435         removeEmptyReferences();
 436         ResourceReference data = cache.get(NUMBER_PATTERNS_CACHEKEY);
 437 
 438         if (data == null || ((numberPatterns = (String[]) data.get()) == null)) {
 439             ResourceBundle resource = localeData.getNumberFormatData(locale);
 440             numberPatterns = resource.getStringArray("NumberPatterns");
 441             cache.put(NUMBER_PATTERNS_CACHEKEY,
 442                       new ResourceReference(NUMBER_PATTERNS_CACHEKEY, (Object) numberPatterns, referenceQueue));
 443         }
 444 
 445         return numberPatterns;
 446     }
 447 
 448     /**
 449      * Returns the FormatData resource bundle of this LocaleResources.
 450      * The FormatData should be used only for accessing extra
 451      * resources required by JSR 310.
 452      */
 453     public ResourceBundle getJavaTimeFormatData() {
 454         ResourceBundle rb = localeData.getDateFormatData(locale);
 455         if (rb instanceof ParallelListResourceBundle) {
 456             localeData.setSupplementary((ParallelListResourceBundle) rb);
 457         }
 458         return rb;
 459     }
 460 
 461     private String getDateTimePattern(String prefix, String key, int styleIndex, String calendarType) {
 462         StringBuilder sb = new StringBuilder();
 463         if (prefix != null) {
 464             sb.append(prefix);
 465         }
 466         if (!"gregory".equals(calendarType)) {
 467             sb.append(calendarType).append('.');
 468         }
 469         sb.append(key);
 470         String resourceKey = sb.toString();
 471         String cacheKey = sb.insert(0, DATE_TIME_PATTERN).toString();
 472 
 473         removeEmptyReferences();
 474         ResourceReference data = cache.get(cacheKey);
 475         Object value = NULLOBJECT;
 476 
 477         if (data == null || ((value = data.get()) == null)) {
 478             ResourceBundle r = (prefix != null) ? getJavaTimeFormatData() : localeData.getDateFormatData(locale);
 479             if (r.containsKey(resourceKey)) {
 480                 value = r.getStringArray(resourceKey);
 481             } else {
 482                 assert !resourceKey.equals(key);
 483                 if (r.containsKey(key)) {
 484                     value = r.getStringArray(key);
 485                 }
 486             }
 487             cache.put(cacheKey,
 488                       new ResourceReference(cacheKey, value, referenceQueue));
 489         }
 490         if (value == NULLOBJECT) {
 491             assert prefix != null;
 492             return null;
 493         }
 494         return ((String[])value)[styleIndex];
 495     }
 496 
 497     private static class ResourceReference extends SoftReference<Object> {
 498         private final String cacheKey;