< prev index next >

src/java.base/share/classes/java/util/Locale.java

Print this page




  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 java.util;
  42 
  43 import java.io.IOException;
  44 import java.io.ObjectInputStream;
  45 import java.io.ObjectOutputStream;
  46 import java.io.ObjectStreamField;
  47 import java.io.Serializable;
  48 import java.text.MessageFormat;

  49 import java.util.spi.LocaleNameProvider;
  50 
  51 import sun.security.action.GetPropertyAction;
  52 import sun.util.locale.BaseLocale;
  53 import sun.util.locale.InternalLocaleBuilder;
  54 import sun.util.locale.LanguageTag;
  55 import sun.util.locale.LocaleExtensions;
  56 import sun.util.locale.LocaleMatcher;
  57 import sun.util.locale.LocaleObjectCache;
  58 import sun.util.locale.LocaleSyntaxException;
  59 import sun.util.locale.LocaleUtils;
  60 import sun.util.locale.ParseStatus;
  61 import sun.util.locale.provider.LocaleProviderAdapter;
  62 import sun.util.locale.provider.LocaleResources;
  63 import sun.util.locale.provider.LocaleServiceProviderPool;
  64 
  65 /**
  66  * A <code>Locale</code> object represents a specific geographical, political,
  67  * or cultural region. An operation that requires a <code>Locale</code> to perform
  68  * its task is called <em>locale-sensitive</em> and uses the <code>Locale</code>


 583      * @see #getExtension(char)
 584      * @see Builder#setExtension(char, String)
 585      * @since 1.7
 586      */
 587     public static final char PRIVATE_USE_EXTENSION = 'x';
 588 
 589     /**
 590      * The key for Unicode locale extension ('u').
 591      *
 592      * @see #getExtension(char)
 593      * @see Builder#setExtension(char, String)
 594      * @since 1.7
 595      */
 596     public static final char UNICODE_LOCALE_EXTENSION = 'u';
 597 
 598     /** serialization ID
 599      */
 600     static final long serialVersionUID = 9149081749638150636L;
 601 
 602     /**






























































 603      * Display types for retrieving localized names from the name providers.
 604      */
 605     private static final int DISPLAY_LANGUAGE = 0;
 606     private static final int DISPLAY_COUNTRY  = 1;
 607     private static final int DISPLAY_VARIANT  = 2;
 608     private static final int DISPLAY_SCRIPT   = 3;
 609 
 610     /**
 611      * Private constructor used by getInstance method
 612      */
 613     private Locale(BaseLocale baseLocale, LocaleExtensions extensions) {
 614         this.baseLocale = baseLocale;
 615         this.localeExtensions = extensions;
 616     }
 617 
 618     /**
 619      * Construct a locale from language, country and variant.
 620      * This constructor normalizes the language value to lowercase and
 621      * the country value to uppercase.
 622      * <p>


 979         }
 980     }
 981 
 982     /**
 983      * Returns an array of all installed locales.
 984      * The returned array represents the union of locales supported
 985      * by the Java runtime environment and by installed
 986      * {@link java.util.spi.LocaleServiceProvider LocaleServiceProvider}
 987      * implementations.  It must contain at least a <code>Locale</code>
 988      * instance equal to {@link java.util.Locale#US Locale.US}.
 989      *
 990      * @return An array of installed locales.
 991      */
 992     public static Locale[] getAvailableLocales() {
 993         return LocaleServiceProviderPool.getAllAvailableLocales();
 994     }
 995 
 996     /**
 997      * Returns a list of all 2-letter country codes defined in ISO 3166.
 998      * Can be used to create Locales.


 999      * <p>
1000      * <b>Note:</b> The <code>Locale</code> class also supports other codes for
1001      * country (region), such as 3-letter numeric UN M.49 area codes.
1002      * Therefore, the list returned by this method does not contain ALL valid
1003      * codes that can be used to create Locales.
1004      *




1005      * @return An array of ISO 3166 two-letter country codes.
1006      */
1007     public static String[] getISOCountries() {
1008         if (isoCountries == null) {
1009             isoCountries = getISO2Table(LocaleISOData.isoCountryTable);
1010         }
1011         String[] result = new String[isoCountries.length];
1012         System.arraycopy(isoCountries, 0, result, 0, isoCountries.length);
1013         return result;
1014     }
1015 
1016     /**














1017      * Returns a list of all 2-letter language codes defined in ISO 639.
1018      * Can be used to create Locales.
1019      * <p>
1020      * <b>Note:</b>
1021      * <ul>
1022      * <li>ISO 639 is not a stable standard&mdash; some languages' codes have changed.
1023      * The list this function returns includes both the new and the old codes for the
1024      * languages whose codes have changed.
1025      * <li>The <code>Locale</code> class also supports language codes up to
1026      * 8 characters in length.  Therefore, the list returned by this method does
1027      * not contain ALL valid codes that can be used to create Locales.
1028      * </ul>
1029      *
1030      * @return An array of ISO 639 two-letter language codes.
1031      */
1032     public static String[] getISOLanguages() {
1033         if (isoLanguages == null) {
1034             isoLanguages = getISO2Table(LocaleISOData.isoLanguageTable);
1035         }
1036         String[] result = new String[isoLanguages.length];




  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 java.util;
  42 
  43 import java.io.IOException;
  44 import java.io.ObjectInputStream;
  45 import java.io.ObjectOutputStream;
  46 import java.io.ObjectStreamField;
  47 import java.io.Serializable;
  48 import java.text.MessageFormat;
  49 import java.util.concurrent.ConcurrentHashMap;
  50 import java.util.spi.LocaleNameProvider;
  51 
  52 import sun.security.action.GetPropertyAction;
  53 import sun.util.locale.BaseLocale;
  54 import sun.util.locale.InternalLocaleBuilder;
  55 import sun.util.locale.LanguageTag;
  56 import sun.util.locale.LocaleExtensions;
  57 import sun.util.locale.LocaleMatcher;
  58 import sun.util.locale.LocaleObjectCache;
  59 import sun.util.locale.LocaleSyntaxException;
  60 import sun.util.locale.LocaleUtils;
  61 import sun.util.locale.ParseStatus;
  62 import sun.util.locale.provider.LocaleProviderAdapter;
  63 import sun.util.locale.provider.LocaleResources;
  64 import sun.util.locale.provider.LocaleServiceProviderPool;
  65 
  66 /**
  67  * A <code>Locale</code> object represents a specific geographical, political,
  68  * or cultural region. An operation that requires a <code>Locale</code> to perform
  69  * its task is called <em>locale-sensitive</em> and uses the <code>Locale</code>


 584      * @see #getExtension(char)
 585      * @see Builder#setExtension(char, String)
 586      * @since 1.7
 587      */
 588     public static final char PRIVATE_USE_EXTENSION = 'x';
 589 
 590     /**
 591      * The key for Unicode locale extension ('u').
 592      *
 593      * @see #getExtension(char)
 594      * @see Builder#setExtension(char, String)
 595      * @since 1.7
 596      */
 597     public static final char UNICODE_LOCALE_EXTENSION = 'u';
 598 
 599     /** serialization ID
 600      */
 601     static final long serialVersionUID = 9149081749638150636L;
 602 
 603     /**
 604      * Enum for specifying the type defined in ISO 3166. This enum is used to
 605      * retrieve the two-letter ISO3166-1 alpha-2, three-letter ISO3166-1
 606      * alpha-3, four-letter ISO3166-3 country codes.
 607      *
 608      * @see #getISOCountries(Locale.IsoCountryCode)
 609      * @since 9
 610      */
 611     public static enum IsoCountryCode {
 612         /**
 613          * PART1_ALPHA2 is used to represent the ISO3166-1 alpha-2 two letter
 614          * country codes.
 615          */
 616         PART1_ALPHA2 {
 617             @Override
 618             Set<String> createCountryCodeSet() {
 619                 return Set.of(Locale.getISOCountries());
 620             }
 621         },
 622         
 623         /**
 624          *
 625          * PART1_ALPHA3 is used to represent the ISO3166-1 alpha-3 three letter
 626          * country codes.
 627          */        
 628         PART1_ALPHA3 {
 629             @Override
 630             Set<String> createCountryCodeSet() {
 631                 return LocaleISOData.computeISO3166_1Alpha3Countries();
 632             }
 633         },
 634         
 635         /**
 636          * PART3 is used to represent the ISO3166-3 four letter country codes.
 637          */        
 638         PART3 {
 639             @Override
 640             Set<String> createCountryCodeSet() {
 641                 return Set.of(LocaleISOData.ISO3166_3);
 642             }
 643         };
 644 
 645         /**
 646          * Concrete implementation of this method attempts to compute value 
 647          * for iso3166CodesMap for each IsoCountryCode type key.
 648          */
 649         abstract Set<String> createCountryCodeSet();
 650 
 651         /**
 652          * Map to hold country codes for each ISO3166 part.
 653          */
 654         private static Map<IsoCountryCode, Set<String>> iso3166CodesMap = new ConcurrentHashMap<>();
 655 
 656         /**
 657          * This method is called from Locale class to retrieve country code set
 658          * for getISOCountries(type)
 659          */
 660         static Set<String> retrieveISOCountryCodes(IsoCountryCode type) {
 661             return iso3166CodesMap.computeIfAbsent(type, IsoCountryCode::createCountryCodeSet);
 662         }
 663     }
 664 
 665     /**
 666      * Display types for retrieving localized names from the name providers.
 667      */
 668     private static final int DISPLAY_LANGUAGE = 0;
 669     private static final int DISPLAY_COUNTRY  = 1;
 670     private static final int DISPLAY_VARIANT  = 2;
 671     private static final int DISPLAY_SCRIPT   = 3;
 672 
 673     /**
 674      * Private constructor used by getInstance method
 675      */
 676     private Locale(BaseLocale baseLocale, LocaleExtensions extensions) {
 677         this.baseLocale = baseLocale;
 678         this.localeExtensions = extensions;
 679     }
 680 
 681     /**
 682      * Construct a locale from language, country and variant.
 683      * This constructor normalizes the language value to lowercase and
 684      * the country value to uppercase.
 685      * <p>


1042         }
1043     }
1044 
1045     /**
1046      * Returns an array of all installed locales.
1047      * The returned array represents the union of locales supported
1048      * by the Java runtime environment and by installed
1049      * {@link java.util.spi.LocaleServiceProvider LocaleServiceProvider}
1050      * implementations.  It must contain at least a <code>Locale</code>
1051      * instance equal to {@link java.util.Locale#US Locale.US}.
1052      *
1053      * @return An array of installed locales.
1054      */
1055     public static Locale[] getAvailableLocales() {
1056         return LocaleServiceProviderPool.getAllAvailableLocales();
1057     }
1058 
1059     /**
1060      * Returns a list of all 2-letter country codes defined in ISO 3166.
1061      * Can be used to create Locales.
1062      * This method is equivalent to {@link #getISOCountries(Locale.IsoCountryCode type)}
1063      * with {@code type}  {@link IsoCountryCode#PART1_ALPHA2}.
1064      * <p>
1065      * <b>Note:</b> The <code>Locale</code> class also supports other codes for
1066      * country (region), such as 3-letter numeric UN M.49 area codes.
1067      * Therefore, the list returned by this method does not contain ALL valid
1068      * codes that can be used to create Locales.
1069      * <p>
1070      * Note that this method does not return obsolete 2-letter country codes.
1071      * ISO3166-3 codes which designate country codes for those obsolete codes,
1072      * can be retrieved from {@link #getISOCountries(Locale.IsoCountryCode type)} with
1073      * {@code type}  {@link IsoCountryCode#PART3}.
1074      * @return An array of ISO 3166 two-letter country codes.
1075      */
1076     public static String[] getISOCountries() {
1077         if (isoCountries == null) {
1078             isoCountries = getISO2Table(LocaleISOData.isoCountryTable);
1079         }
1080         String[] result = new String[isoCountries.length];
1081         System.arraycopy(isoCountries, 0, result, 0, isoCountries.length);
1082         return result;
1083     }
1084 
1085     /**
1086      * Returns a {@code Set} of ISO3166 country codes for the specified type.
1087      *
1088      * @param type {@link Locale.IsoCountryCode} specified ISO code type.
1089      * @see java.util.Locale.IsoCountryCode
1090      * @throws NullPointerException if type is null
1091      * @return a {@code Set} of ISO country codes for the specified type.
1092      * @since 9
1093      */
1094     public static Set<String> getISOCountries(IsoCountryCode type) {
1095         Objects.requireNonNull(type);
1096         return IsoCountryCode.retrieveISOCountryCodes(type);
1097     }
1098 
1099     /**
1100      * Returns a list of all 2-letter language codes defined in ISO 639.
1101      * Can be used to create Locales.
1102      * <p>
1103      * <b>Note:</b>
1104      * <ul>
1105      * <li>ISO 639 is not a stable standard&mdash; some languages' codes have changed.
1106      * The list this function returns includes both the new and the old codes for the
1107      * languages whose codes have changed.
1108      * <li>The <code>Locale</code> class also supports language codes up to
1109      * 8 characters in length.  Therefore, the list returned by this method does
1110      * not contain ALL valid codes that can be used to create Locales.
1111      * </ul>
1112      *
1113      * @return An array of ISO 639 two-letter language codes.
1114      */
1115     public static String[] getISOLanguages() {
1116         if (isoLanguages == null) {
1117             isoLanguages = getISO2Table(LocaleISOData.isoLanguageTable);
1118         }
1119         String[] result = new String[isoLanguages.length];


< prev index next >