jdk/src/share/classes/java/util/ResourceBundle.java
changeset 9224 75c0420badef
parent 6660 7bc303389b4f
child 10419 12c063b39232
equal deleted inserted replaced
9223:d331b7996fc3 9224:75c0420badef
     1 /*
     1 /*
     2  * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    55 import java.util.concurrent.ConcurrentHashMap;
    55 import java.util.concurrent.ConcurrentHashMap;
    56 import java.util.concurrent.ConcurrentMap;
    56 import java.util.concurrent.ConcurrentMap;
    57 import java.util.jar.JarEntry;
    57 import java.util.jar.JarEntry;
    58 
    58 
    59 import sun.util.locale.BaseLocale;
    59 import sun.util.locale.BaseLocale;
    60 import sun.util.locale.LocaleExtensions;
       
    61 import sun.util.locale.LocaleObjectCache;
    60 import sun.util.locale.LocaleObjectCache;
    62 
    61 
    63 
    62 
    64 /**
    63 /**
    65  *
    64  *
   288      *
   287      *
   289      * This variable would be better named "cache", but we keep the old
   288      * This variable would be better named "cache", but we keep the old
   290      * name for compatibility with some workarounds for bug 4212439.
   289      * name for compatibility with some workarounds for bug 4212439.
   291      */
   290      */
   292     private static final ConcurrentMap<CacheKey, BundleReference> cacheList
   291     private static final ConcurrentMap<CacheKey, BundleReference> cacheList
   293         = new ConcurrentHashMap<CacheKey, BundleReference>(INITIAL_CACHE_SIZE);
   292         = new ConcurrentHashMap<>(INITIAL_CACHE_SIZE);
   294 
   293 
   295     /**
   294     /**
   296      * Queue for reference objects referring to class loaders or bundles.
   295      * Queue for reference objects referring to class loaders or bundles.
   297      */
   296      */
   298     private static final ReferenceQueue referenceQueue = new ReferenceQueue();
   297     private static final ReferenceQueue referenceQueue = new ReferenceQueue();
  1753      * @return a <code>Set</code> of all keys contained in this
  1752      * @return a <code>Set</code> of all keys contained in this
  1754      *         <code>ResourceBundle</code> and its parent bundles.
  1753      *         <code>ResourceBundle</code> and its parent bundles.
  1755      * @since 1.6
  1754      * @since 1.6
  1756      */
  1755      */
  1757     public Set<String> keySet() {
  1756     public Set<String> keySet() {
  1758         Set<String> keys = new HashSet<String>();
  1757         Set<String> keys = new HashSet<>();
  1759         for (ResourceBundle rb = this; rb != null; rb = rb.parent) {
  1758         for (ResourceBundle rb = this; rb != null; rb = rb.parent) {
  1760             keys.addAll(rb.handleKeySet());
  1759             keys.addAll(rb.handleKeySet());
  1761         }
  1760         }
  1762         return keys;
  1761         return keys;
  1763     }
  1762     }
  1781      */
  1780      */
  1782     protected Set<String> handleKeySet() {
  1781     protected Set<String> handleKeySet() {
  1783         if (keySet == null) {
  1782         if (keySet == null) {
  1784             synchronized (this) {
  1783             synchronized (this) {
  1785                 if (keySet == null) {
  1784                 if (keySet == null) {
  1786                     Set<String> keys = new HashSet<String>();
  1785                     Set<String> keys = new HashSet<>();
  1787                     Enumeration<String> enumKeys = getKeys();
  1786                     Enumeration<String> enumKeys = getKeys();
  1788                     while (enumKeys.hasMoreElements()) {
  1787                     while (enumKeys.hasMoreElements()) {
  1789                         String key = enumKeys.nextElement();
  1788                         String key = enumKeys.nextElement();
  1790                         if (handleGetObject(key) != null) {
  1789                         if (handleGetObject(key) != null) {
  1791                             keys.add(key);
  1790                             keys.add(key);
  2299          */
  2298          */
  2300         public List<Locale> getCandidateLocales(String baseName, Locale locale) {
  2299         public List<Locale> getCandidateLocales(String baseName, Locale locale) {
  2301             if (baseName == null) {
  2300             if (baseName == null) {
  2302                 throw new NullPointerException();
  2301                 throw new NullPointerException();
  2303             }
  2302             }
  2304             return new ArrayList<Locale>(CANDIDATES_CACHE.get(locale.getBaseLocale()));
  2303             return new ArrayList<>(CANDIDATES_CACHE.get(locale.getBaseLocale()));
  2305         }
  2304         }
  2306 
  2305 
  2307         private static final CandidateListCache CANDIDATES_CACHE = new CandidateListCache();
  2306         private static final CandidateListCache CANDIDATES_CACHE = new CandidateListCache();
  2308 
  2307 
  2309         private static class CandidateListCache extends LocaleObjectCache<BaseLocale, List<Locale>> {
  2308         private static class CandidateListCache extends LocaleObjectCache<BaseLocale, List<Locale>> {
  2325                     }
  2324                     }
  2326                 }
  2325                 }
  2327                 if (language.equals("nb") || isNorwegianBokmal) {
  2326                 if (language.equals("nb") || isNorwegianBokmal) {
  2328                     List<Locale> tmpList = getDefaultList("nb", script, region, variant);
  2327                     List<Locale> tmpList = getDefaultList("nb", script, region, variant);
  2329                     // Insert a locale replacing "nb" with "no" for every list entry
  2328                     // Insert a locale replacing "nb" with "no" for every list entry
  2330                     List<Locale> bokmalList = new LinkedList<Locale>();
  2329                     List<Locale> bokmalList = new LinkedList<>();
  2331                     for (Locale l : tmpList) {
  2330                     for (Locale l : tmpList) {
  2332                         bokmalList.add(l);
  2331                         bokmalList.add(l);
  2333                         if (l.getLanguage().length() == 0) {
  2332                         if (l.getLanguage().length() == 0) {
  2334                             break;
  2333                             break;
  2335                         }
  2334                         }
  2336                         bokmalList.add(Locale.getInstance("no", l.getScript(), l.getCountry(),
  2335                         bokmalList.add(Locale.getInstance("no", l.getScript(), l.getCountry(),
  2337                                 l.getVariant(), LocaleExtensions.EMPTY_EXTENSIONS));
  2336                                 l.getVariant(), null));
  2338                     }
  2337                     }
  2339                     return bokmalList;
  2338                     return bokmalList;
  2340                 } else if (language.equals("nn") || isNorwegianNynorsk) {
  2339                 } else if (language.equals("nn") || isNorwegianNynorsk) {
  2341                     // Insert no_NO_NY, no_NO, no after nn
  2340                     // Insert no_NO_NY, no_NO, no after nn
  2342                     List<Locale> nynorskList = getDefaultList("nn", script, region, variant);
  2341                     List<Locale> nynorskList = getDefaultList("nn", script, region, variant);
  2372 
  2371 
  2373             private static List<Locale> getDefaultList(String language, String script, String region, String variant) {
  2372             private static List<Locale> getDefaultList(String language, String script, String region, String variant) {
  2374                 List<String> variants = null;
  2373                 List<String> variants = null;
  2375 
  2374 
  2376                 if (variant.length() > 0) {
  2375                 if (variant.length() > 0) {
  2377                     variants = new LinkedList<String>();
  2376                     variants = new LinkedList<>();
  2378                     int idx = variant.length();
  2377                     int idx = variant.length();
  2379                     while (idx != -1) {
  2378                     while (idx != -1) {
  2380                         variants.add(variant.substring(0, idx));
  2379                         variants.add(variant.substring(0, idx));
  2381                         idx = variant.lastIndexOf('_', --idx);
  2380                         idx = variant.lastIndexOf('_', --idx);
  2382                     }
  2381                     }
  2383                 }
  2382                 }
  2384 
  2383 
  2385                 LinkedList<Locale> list = new LinkedList<Locale>();
  2384                 List<Locale> list = new LinkedList<>();
  2386 
  2385 
  2387                 if (variants != null) {
  2386                 if (variants != null) {
  2388                     for (String v : variants) {
  2387                     for (String v : variants) {
  2389                         list.add(Locale.getInstance(language, script, region, v, LocaleExtensions.EMPTY_EXTENSIONS));
  2388                         list.add(Locale.getInstance(language, script, region, v, null));
  2390                     }
  2389                     }
  2391                 }
  2390                 }
  2392                 if (region.length() > 0) {
  2391                 if (region.length() > 0) {
  2393                     list.add(Locale.getInstance(language, script, region, "", LocaleExtensions.EMPTY_EXTENSIONS));
  2392                     list.add(Locale.getInstance(language, script, region, "", null));
  2394                 }
  2393                 }
  2395                 if (script.length() > 0) {
  2394                 if (script.length() > 0) {
  2396                     list.add(Locale.getInstance(language, script, "", "", LocaleExtensions.EMPTY_EXTENSIONS));
  2395                     list.add(Locale.getInstance(language, script, "", "", null));
  2397 
  2396 
  2398                     // With script, after truncating variant, region and script,
  2397                     // With script, after truncating variant, region and script,
  2399                     // start over without script.
  2398                     // start over without script.
  2400                     if (variants != null) {
  2399                     if (variants != null) {
  2401                         for (String v : variants) {
  2400                         for (String v : variants) {
  2402                             list.add(Locale.getInstance(language, "", region, v, LocaleExtensions.EMPTY_EXTENSIONS));
  2401                             list.add(Locale.getInstance(language, "", region, v, null));
  2403                         }
  2402                         }
  2404                     }
  2403                     }
  2405                     if (region.length() > 0) {
  2404                     if (region.length() > 0) {
  2406                         list.add(Locale.getInstance(language, "", region, "", LocaleExtensions.EMPTY_EXTENSIONS));
  2405                         list.add(Locale.getInstance(language, "", region, "", null));
  2407                     }
  2406                     }
  2408                 }
  2407                 }
  2409                 if (language.length() > 0) {
  2408                 if (language.length() > 0) {
  2410                     list.add(Locale.getInstance(language, "", "", "", LocaleExtensions.EMPTY_EXTENSIONS));
  2409                     list.add(Locale.getInstance(language, "", "", "", null));
  2411                 }
  2410                 }
  2412                 // Add root locale at the end
  2411                 // Add root locale at the end
  2413                 list.add(Locale.ROOT);
  2412                 list.add(Locale.ROOT);
  2414 
  2413 
  2415                 return list;
  2414                 return list;