< prev index next >

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

Print this page




  26 /*
  27  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
  28  * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
  29  *
  30  * The original version of this source code and documentation
  31  * is copyrighted and owned by Taligent, Inc., a wholly-owned
  32  * subsidiary of IBM. These materials are provided under terms
  33  * of a License Agreement between Taligent and Sun. This technology
  34  * is protected by multiple US and International patents.
  35  *
  36  * This notice and attribution to Taligent may not be removed.
  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.HashSet;
  48 import java.util.LinkedHashSet;
  49 import java.util.Locale;
  50 import java.util.Map;
  51 import java.util.Objects;
  52 import java.util.ResourceBundle;
  53 import java.util.Set;
  54 import java.util.TimeZone;
  55 import java.util.concurrent.ConcurrentHashMap;
  56 import java.util.concurrent.ConcurrentMap;
  57 import sun.security.action.GetPropertyAction;
  58 import sun.util.calendar.ZoneInfo;
  59 import sun.util.resources.LocaleData;
  60 import sun.util.resources.OpenListResourceBundle;
  61 import sun.util.resources.ParallelListResourceBundle;
  62 import sun.util.resources.TimeZoneNamesBundle;
  63 
  64 /**
  65  * Central accessor to locale-dependent resources for JRE/CLDR provider adapters.


  71 
  72     private final Locale locale;
  73     private final LocaleData localeData;
  74     private final LocaleProviderAdapter.Type type;
  75 
  76     // Resource cache
  77     private final ConcurrentMap<String, ResourceReference> cache = new ConcurrentHashMap<>();
  78     private final ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
  79 
  80     // cache key prefixes
  81     private static final String BREAK_ITERATOR_INFO = "BII.";
  82     private static final String CALENDAR_DATA = "CALD.";
  83     private static final String COLLATION_DATA_CACHEKEY = "COLD";
  84     private static final String DECIMAL_FORMAT_SYMBOLS_DATA_CACHEKEY = "DFSD";
  85     private static final String CURRENCY_NAMES = "CN.";
  86     private static final String LOCALE_NAMES = "LN.";
  87     private static final String TIME_ZONE_NAMES = "TZN.";
  88     private static final String ZONE_IDS_CACHEKEY = "ZID";
  89     private static final String CALENDAR_NAMES = "CALN.";
  90     private static final String NUMBER_PATTERNS_CACHEKEY = "NP";

  91     private static final String DATE_TIME_PATTERN = "DTP.";
  92 
  93     // TimeZoneNamesBundle exemplar city prefix
  94     private static final String TZNB_EXCITY_PREFIX = "timezone.excity.";
  95 
  96     // null singleton cache value
  97     private static final Object NULLOBJECT = new Object();
  98 
  99     LocaleResources(ResourceBundleBasedAdapter adapter, Locale locale) {
 100         this.locale = locale;
 101         this.localeData = adapter.getLocaleData();
 102         type = ((LocaleProviderAdapter)adapter).getAdapterType();
 103     }
 104 
 105     private void removeEmptyReferences() {
 106         Object ref;
 107         while ((ref = referenceQueue.poll()) != null) {
 108             cache.remove(((ResourceReference)ref).getCacheKey());
 109         }
 110     }


 460             throw new IllegalArgumentException("No date or time style specified");
 461         }
 462         return pattern;
 463     }
 464 
 465     public String[] getNumberPatterns() {
 466         String[] numberPatterns = null;
 467 
 468         removeEmptyReferences();
 469         ResourceReference data = cache.get(NUMBER_PATTERNS_CACHEKEY);
 470 
 471         if (data == null || ((numberPatterns = (String[]) data.get()) == null)) {
 472             ResourceBundle resource = localeData.getNumberFormatData(locale);
 473             numberPatterns = resource.getStringArray("NumberPatterns");
 474             cache.put(NUMBER_PATTERNS_CACHEKEY,
 475                       new ResourceReference(NUMBER_PATTERNS_CACHEKEY, (Object) numberPatterns, referenceQueue));
 476         }
 477 
 478         return numberPatterns;
 479     }


























 480 
 481     /**
 482      * Returns the FormatData resource bundle of this LocaleResources.
 483      * The FormatData should be used only for accessing extra
 484      * resources required by JSR 310.
 485      */
 486     public ResourceBundle getJavaTimeFormatData() {
 487         ResourceBundle rb = localeData.getDateFormatData(locale);
 488         if (rb instanceof ParallelListResourceBundle) {
 489             localeData.setSupplementary((ParallelListResourceBundle) rb);
 490         }
 491         return rb;
 492     }
 493 
 494     private String getDateTimePattern(String prefix, String key, int styleIndex, String calendarType) {
 495         StringBuilder sb = new StringBuilder();
 496         if (prefix != null) {
 497             sb.append(prefix);
 498         }
 499         if (!"gregory".equals(calendarType)) {




  26 /*
  27  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
  28  * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
  29  *
  30  * The original version of this source code and documentation
  31  * is copyrighted and owned by Taligent, Inc., a wholly-owned
  32  * subsidiary of IBM. These materials are provided under terms
  33  * of a License Agreement between Taligent and Sun. This technology
  34  * is protected by multiple US and International patents.
  35  *
  36  * This notice and attribution to Taligent may not be removed.
  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.text.NumberFormat;
  47 import java.util.Calendar;
  48 import java.util.HashSet;
  49 import java.util.LinkedHashSet;
  50 import java.util.Locale;
  51 import java.util.Map;
  52 import java.util.Objects;
  53 import java.util.ResourceBundle;
  54 import java.util.Set;
  55 import java.util.TimeZone;
  56 import java.util.concurrent.ConcurrentHashMap;
  57 import java.util.concurrent.ConcurrentMap;
  58 import sun.security.action.GetPropertyAction;
  59 import sun.util.calendar.ZoneInfo;
  60 import sun.util.resources.LocaleData;
  61 import sun.util.resources.OpenListResourceBundle;
  62 import sun.util.resources.ParallelListResourceBundle;
  63 import sun.util.resources.TimeZoneNamesBundle;
  64 
  65 /**
  66  * Central accessor to locale-dependent resources for JRE/CLDR provider adapters.


  72 
  73     private final Locale locale;
  74     private final LocaleData localeData;
  75     private final LocaleProviderAdapter.Type type;
  76 
  77     // Resource cache
  78     private final ConcurrentMap<String, ResourceReference> cache = new ConcurrentHashMap<>();
  79     private final ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
  80 
  81     // cache key prefixes
  82     private static final String BREAK_ITERATOR_INFO = "BII.";
  83     private static final String CALENDAR_DATA = "CALD.";
  84     private static final String COLLATION_DATA_CACHEKEY = "COLD";
  85     private static final String DECIMAL_FORMAT_SYMBOLS_DATA_CACHEKEY = "DFSD";
  86     private static final String CURRENCY_NAMES = "CN.";
  87     private static final String LOCALE_NAMES = "LN.";
  88     private static final String TIME_ZONE_NAMES = "TZN.";
  89     private static final String ZONE_IDS_CACHEKEY = "ZID";
  90     private static final String CALENDAR_NAMES = "CALN.";
  91     private static final String NUMBER_PATTERNS_CACHEKEY = "NP";
  92     private static final String COMPACT_NUMBER_PATTERNS_CACHEKEY = "CNP";
  93     private static final String DATE_TIME_PATTERN = "DTP.";
  94 
  95     // TimeZoneNamesBundle exemplar city prefix
  96     private static final String TZNB_EXCITY_PREFIX = "timezone.excity.";
  97 
  98     // null singleton cache value
  99     private static final Object NULLOBJECT = new Object();
 100 
 101     LocaleResources(ResourceBundleBasedAdapter adapter, Locale locale) {
 102         this.locale = locale;
 103         this.localeData = adapter.getLocaleData();
 104         type = ((LocaleProviderAdapter)adapter).getAdapterType();
 105     }
 106 
 107     private void removeEmptyReferences() {
 108         Object ref;
 109         while ((ref = referenceQueue.poll()) != null) {
 110             cache.remove(((ResourceReference)ref).getCacheKey());
 111         }
 112     }


 462             throw new IllegalArgumentException("No date or time style specified");
 463         }
 464         return pattern;
 465     }
 466 
 467     public String[] getNumberPatterns() {
 468         String[] numberPatterns = null;
 469 
 470         removeEmptyReferences();
 471         ResourceReference data = cache.get(NUMBER_PATTERNS_CACHEKEY);
 472 
 473         if (data == null || ((numberPatterns = (String[]) data.get()) == null)) {
 474             ResourceBundle resource = localeData.getNumberFormatData(locale);
 475             numberPatterns = resource.getStringArray("NumberPatterns");
 476             cache.put(NUMBER_PATTERNS_CACHEKEY,
 477                       new ResourceReference(NUMBER_PATTERNS_CACHEKEY, (Object) numberPatterns, referenceQueue));
 478         }
 479 
 480         return numberPatterns;
 481     }
 482     
 483     /**
 484      * Returns the compact number format patterns.
 485      * @param formatStyle the style for formatting a number
 486      * @return an array of compact number patterns
 487      */
 488     @SuppressWarnings("unchecked")
 489     public String[] getCNPatterns(NumberFormat.Style formatStyle) {
 490 
 491         Objects.requireNonNull(formatStyle);
 492         String[] compactNumberPatterns = null;
 493         removeEmptyReferences();
 494         String width = (formatStyle == NumberFormat.Style.LONG) ? "long" : "short";
 495         String cacheKey = width + "." + COMPACT_NUMBER_PATTERNS_CACHEKEY;
 496         ResourceReference data = cache.get(cacheKey);
 497         if (data == null || ((compactNumberPatterns
 498                 = (String[]) data.get()) == null)) {
 499             ResourceBundle resource = localeData.getNumberFormatData(locale);
 500             compactNumberPatterns = (String[]) resource
 501                     .getObject(width + ".CompactNumberPatterns");
 502             cache.put(cacheKey, new ResourceReference(cacheKey,
 503                     (Object) compactNumberPatterns, referenceQueue));
 504         }
 505         return compactNumberPatterns;
 506     }
 507 
 508 
 509     /**
 510      * Returns the FormatData resource bundle of this LocaleResources.
 511      * The FormatData should be used only for accessing extra
 512      * resources required by JSR 310.
 513      */
 514     public ResourceBundle getJavaTimeFormatData() {
 515         ResourceBundle rb = localeData.getDateFormatData(locale);
 516         if (rb instanceof ParallelListResourceBundle) {
 517             localeData.setSupplementary((ParallelListResourceBundle) rb);
 518         }
 519         return rb;
 520     }
 521 
 522     private String getDateTimePattern(String prefix, String key, int styleIndex, String calendarType) {
 523         StringBuilder sb = new StringBuilder();
 524         if (prefix != null) {
 525             sb.append(prefix);
 526         }
 527         if (!"gregory".equals(calendarType)) {


< prev index next >