< 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> getISOCountriesImpl() {
 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         
 629         PART1_ALPHA3 {
 630             @Override
 631             Set<String> getISOCountriesImpl() {
 632                 return LocaleISOData.computeISO3166_1Alpha3Countries();
 633             }
 634         },
 635         
 636         /**
 637          * PART3 is used to represent the ISO3166-3 four letter country codes.
 638          */
 639         
 640         PART3 {
 641             @Override
 642             Set<String> getISOCountriesImpl() {
 643                 return Set.of(LocaleISOData.ISO3166_3);
 644             }
 645         };
 646 
 647         /**
 648          * Concrete implementation of this mapping function attempts to compute value 
 649          * for iso3166CodesMap for each IsoCountryCode type key.
 650          */
 651         abstract Set<String> getISOCountriesImpl();
 652 
 653         /**
 654          * Map to hold country codes for each ISO3166 part.
 655          */
 656         private static Map<IsoCountryCode, Set<String>> iso3166CodesMap = new ConcurrentHashMap<>();
 657 
 658         /**
 659          * This method is called from Locale class to retrieve country code set
 660          * for getISOCountries(type)
 661          */
 662         static Set<String> retrieveISOCountryCodes(IsoCountryCode type) {
 663             return iso3166CodesMap.computeIfAbsent(type, IsoCountryCode::getISOCountriesImpl);
 664         }
 665     }
 666 
 667     /**
 668      * Display types for retrieving localized names from the name providers.
 669      */
 670     private static final int DISPLAY_LANGUAGE = 0;
 671     private static final int DISPLAY_COUNTRY  = 1;
 672     private static final int DISPLAY_VARIANT  = 2;
 673     private static final int DISPLAY_SCRIPT   = 3;
 674 
 675     /**
 676      * Private constructor used by getInstance method
 677      */
 678     private Locale(BaseLocale baseLocale, LocaleExtensions extensions) {
 679         this.baseLocale = baseLocale;
 680         this.localeExtensions = extensions;
 681     }
 682 
 683     /**
 684      * Construct a locale from language, country and variant.
 685      * This constructor normalizes the language value to lowercase and
 686      * the country value to uppercase.
 687      * <p>


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


< prev index next >