Path: blob/master/src/java.base/share/classes/sun/util/locale/provider/LocaleNameProviderImpl.java
41161 views
/*1* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.util.locale.provider;2627import java.util.Locale;28import java.util.Set;29import java.util.spi.LocaleNameProvider;3031/**32* Concrete implementation of the33* {@link java.util.spi.LocaleNameProvider LocaleNameProvider} class34* for the JRE LocaleProviderAdapter.35*36* @author Naoto Sato37* @author Masayoshi Okutsu38*/39public class LocaleNameProviderImpl extends LocaleNameProvider implements AvailableLanguageTags {40private final LocaleProviderAdapter.Type type;41private final Set<String> langtags;4243public LocaleNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {44this.type = type;45this.langtags = langtags;46}4748/**49* Returns an array of all locales for which this locale service provider50* can provide localized objects or names.51*52* @return An array of all locales for which this locale service provider53* can provide localized objects or names.54*/55@Override56public Locale[] getAvailableLocales() {57return LocaleProviderAdapter.toLocaleArray(langtags);58}5960@Override61public boolean isSupportedLocale(Locale locale) {62return LocaleProviderAdapter.forType(type).isSupportedProviderLocale(locale, langtags);63}6465/**66* Returns a localized name for the given ISO 639 language code and the67* given locale that is appropriate for display to the user.68* For example, if <code>languageCode</code> is "fr" and <code>locale</code>69* is en_US, getDisplayLanguage() will return "French"; if <code>languageCode</code>70* is "en" and <code>locale</code> is fr_FR, getDisplayLanguage() will return "anglais".71* If the name returned cannot be localized according to <code>locale</code>,72* (say, the provider does not have a Japanese name for Croatian),73* this method returns null.74* @param lang the ISO 639 language code string in the form of two75* lower-case letters between 'a' (U+0061) and 'z' (U+007A)76* @param locale the desired locale77* @return the name of the given language code for the specified locale, or null if it's not78* available.79* @exception NullPointerException if <code>languageCode</code> or <code>locale</code> is null80* @exception IllegalArgumentException if <code>languageCode</code> is not in the form of81* two lower-case letters, or <code>locale</code> isn't82* one of the locales returned from83* {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()84* getAvailableLocales()}.85* @see java.util.Locale#getDisplayLanguage(java.util.Locale)86*/87@Override88public String getDisplayLanguage(String lang, Locale locale) {89return getDisplayString(lang, locale);90}9192/**93* Returns a localized name for the given <a href="http://www.rfc-editor.org/rfc/bcp/bcp47.txt">94* IETF BCP47</a> script code and the given locale that is appropriate for95* display to the user.96* For example, if <code>scriptCode</code> is "Latn" and <code>locale</code>97* is en_US, getDisplayScript() will return "Latin"; if <code>scriptCode</code>98* is "Cyrl" and <code>locale</code> is fr_FR, getDisplayScript() will return "cyrillique".99* If the name returned cannot be localized according to <code>locale</code>,100* (say, the provider does not have a Japanese name for Cyrillic),101* this method returns null. The default implementation returns null.102* @param scriptCode the four letter script code string in the form of title-case103* letters (the first letter is upper-case character between 'A' (U+0041) and104* 'Z' (U+005A) followed by three lower-case character between 'a' (U+0061)105* and 'z' (U+007A)).106* @param locale the desired locale107* @return the name of the given script code for the specified locale, or null if it's not108* available.109* @exception NullPointerException if <code>scriptCode</code> or <code>locale</code> is null110* @exception IllegalArgumentException if <code>scriptCode</code> is not in the form of111* four title case letters, or <code>locale</code> isn't112* one of the locales returned from113* {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()114* getAvailableLocales()}.115* @see java.util.Locale#getDisplayScript(java.util.Locale)116*/117@Override118public String getDisplayScript(String scriptCode, Locale locale) {119return getDisplayString(scriptCode, locale);120}121122/**123* Returns a localized name for the given ISO 3166 country code and the124* given locale that is appropriate for display to the user.125* For example, if <code>countryCode</code> is "FR" and <code>locale</code>126* is en_US, getDisplayCountry() will return "France"; if <code>countryCode</code>127* is "US" and <code>locale</code> is fr_FR, getDisplayCountry() will return "Etats-Unis".128* If the name returned cannot be localized according to <code>locale</code>,129* (say, the provider does not have a Japanese name for Croatia),130* this method returns null.131* @param ctry the ISO 3166 country code string in the form of two132* upper-case letters between 'A' (U+0041) and 'Z' (U+005A)133* @param locale the desired locale134* @return the name of the given country code for the specified locale, or null if it's not135* available.136* @exception NullPointerException if <code>countryCode</code> or <code>locale</code> is null137* @exception IllegalArgumentException if <code>countryCode</code> is not in the form of138* two upper-case letters, or <code>locale</code> isn't139* one of the locales returned from140* {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()141* getAvailableLocales()}.142* @see java.util.Locale#getDisplayCountry(java.util.Locale)143*/144@Override145public String getDisplayCountry(String ctry, Locale locale) {146return getDisplayString(ctry, locale);147}148149/**150* Returns a localized name for the given variant code and the given locale that151* is appropriate for display to the user.152* If the name returned cannot be localized according to <code>locale</code>,153* this method returns null.154* @param vrnt the variant string155* @param locale the desired locale156* @return the name of the given variant string for the specified locale, or null if it's not157* available.158* @exception NullPointerException if <code>variant</code> or <code>locale</code> is null159* @exception IllegalArgumentException if <code>locale</code> isn't160* one of the locales returned from161* {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()162* getAvailableLocales()}.163* @see java.util.Locale#getDisplayVariant(java.util.Locale)164*/165@Override166public String getDisplayVariant(String vrnt, Locale locale) {167return getDisplayString("%%"+vrnt, locale);168}169170/**171* @inheritDoc172*/173@Override174public String getDisplayUnicodeExtensionKey(String key, Locale locale) {175super.getDisplayUnicodeExtensionKey(key, locale); // null check176String rbKey = "key." + key;177String name = getDisplayString(rbKey, locale);178return rbKey.equals(name) ? key : name;179}180181/**182* @inheritDoc183*/184@Override185public String getDisplayUnicodeExtensionType(String extType, String key, Locale locale) {186super.getDisplayUnicodeExtensionType(extType, key, locale); // null check187String rbKey = "type." + key + "." + extType;188String name = getDisplayString(rbKey, locale);189return rbKey.equals(name) ? extType : name;190}191192private String getDisplayString(String key, Locale locale) {193if (key == null || locale == null) {194throw new NullPointerException();195}196197return LocaleProviderAdapter.forType(type).getLocaleResources(locale).getLocaleName(key);198}199200@Override201public Set<String> getAvailableLanguageTags() {202return langtags;203}204}205206207