25
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.
66 *
67 * @author Masayoshi Okutsu
68 * @author Naoto Sato
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)) {
|
25
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.CompactNumberFormat;
46 import java.text.MessageFormat;
47 import java.util.Calendar;
48 import java.util.HashSet;
49 import java.util.LinkedHashSet;
50 import java.util.List;
51 import java.util.Locale;
52 import java.util.Map;
53 import java.util.Objects;
54 import java.util.ResourceBundle;
55 import java.util.Set;
56 import java.util.TimeZone;
57 import java.util.concurrent.ConcurrentHashMap;
58 import java.util.concurrent.ConcurrentMap;
59 import sun.security.action.GetPropertyAction;
60 import sun.util.calendar.ZoneInfo;
61 import sun.util.resources.LocaleData;
62 import sun.util.resources.OpenListResourceBundle;
63 import sun.util.resources.ParallelListResourceBundle;
64 import sun.util.resources.TimeZoneNamesBundle;
65
66 /**
67 * Central accessor to locale-dependent resources for JRE/CLDR provider adapters.
68 *
69 * @author Masayoshi Okutsu
70 * @author Naoto Sato
73
74 private final Locale locale;
75 private final LocaleData localeData;
76 private final LocaleProviderAdapter.Type type;
77
78 // Resource cache
79 private final ConcurrentMap<String, ResourceReference> cache = new ConcurrentHashMap<>();
80 private final ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
81
82 // cache key prefixes
83 private static final String BREAK_ITERATOR_INFO = "BII.";
84 private static final String CALENDAR_DATA = "CALD.";
85 private static final String COLLATION_DATA_CACHEKEY = "COLD";
86 private static final String DECIMAL_FORMAT_SYMBOLS_DATA_CACHEKEY = "DFSD";
87 private static final String CURRENCY_NAMES = "CN.";
88 private static final String LOCALE_NAMES = "LN.";
89 private static final String TIME_ZONE_NAMES = "TZN.";
90 private static final String ZONE_IDS_CACHEKEY = "ZID";
91 private static final String CALENDAR_NAMES = "CALN.";
92 private static final String NUMBER_PATTERNS_CACHEKEY = "NP";
93 private static final String COMPACT_NUMBER_PATTERNS_CACHEKEY = "CNP";
94 private static final String DATE_TIME_PATTERN = "DTP.";
95
96 // TimeZoneNamesBundle exemplar city prefix
97 private static final String TZNB_EXCITY_PREFIX = "timezone.excity.";
98
99 // null singleton cache value
100 private static final Object NULLOBJECT = new Object();
101
102 LocaleResources(ResourceBundleBasedAdapter adapter, Locale locale) {
103 this.locale = locale;
104 this.localeData = adapter.getLocaleData();
105 type = ((LocaleProviderAdapter)adapter).getAdapterType();
106 }
107
108 private void removeEmptyReferences() {
109 Object ref;
110 while ((ref = referenceQueue.poll()) != null) {
111 cache.remove(((ResourceReference)ref).getCacheKey());
112 }
113 }
463 throw new IllegalArgumentException("No date or time style specified");
464 }
465 return pattern;
466 }
467
468 public String[] getNumberPatterns() {
469 String[] numberPatterns = null;
470
471 removeEmptyReferences();
472 ResourceReference data = cache.get(NUMBER_PATTERNS_CACHEKEY);
473
474 if (data == null || ((numberPatterns = (String[]) data.get()) == null)) {
475 ResourceBundle resource = localeData.getNumberFormatData(locale);
476 numberPatterns = resource.getStringArray("NumberPatterns");
477 cache.put(NUMBER_PATTERNS_CACHEKEY,
478 new ResourceReference(NUMBER_PATTERNS_CACHEKEY, (Object) numberPatterns, referenceQueue));
479 }
480
481 return numberPatterns;
482 }
483
484 /**
485 * Returns the compact number format patterns.
486 * @param formatStyle the style for formatting a number
487 * @return a list of compact number patterns
488 */
489 @SuppressWarnings("unchecked")
490 public List<String> getCNPatterns(CompactNumberFormat.Style formatStyle) {
491
492 Objects.requireNonNull(formatStyle);
493 List<String> compactNumberPatterns = null;
494 removeEmptyReferences();
495 String width = (formatStyle == CompactNumberFormat.Style.LONG) ? "long" : "short";
496 String cacheKey = width + "." + COMPACT_NUMBER_PATTERNS_CACHEKEY;
497 ResourceReference data = cache.get(cacheKey);
498 if (data == null || ((compactNumberPatterns
499 = (List<String>) data.get()) == null)) {
500 ResourceBundle resource = localeData.getNumberFormatData(locale);
501 compactNumberPatterns = (List<String>) resource
502 .getObject(width + ".CompactNumberPatterns");
503 cache.put(cacheKey, new ResourceReference(cacheKey,
504 (Object) compactNumberPatterns, referenceQueue));
505 }
506 return compactNumberPatterns;
507 }
508
509
510 /**
511 * Returns the FormatData resource bundle of this LocaleResources.
512 * The FormatData should be used only for accessing extra
513 * resources required by JSR 310.
514 */
515 public ResourceBundle getJavaTimeFormatData() {
516 ResourceBundle rb = localeData.getDateFormatData(locale);
517 if (rb instanceof ParallelListResourceBundle) {
518 localeData.setSupplementary((ParallelListResourceBundle) rb);
519 }
520 return rb;
521 }
522
523 private String getDateTimePattern(String prefix, String key, int styleIndex, String calendarType) {
524 StringBuilder sb = new StringBuilder();
525 if (prefix != null) {
526 sb.append(prefix);
527 }
528 if (!"gregory".equals(calendarType)) {
|