This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-lang.git
commit b5de0ef28017583a451dbec86928f7682b2f8a94 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Mon Dec 21 12:26:32 2020 -0500 Sort members. --- .../java/org/apache/commons/lang3/LocaleUtils.java | 382 ++++++++++----------- 1 file changed, 191 insertions(+), 191 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/LocaleUtils.java b/src/main/java/org/apache/commons/lang3/LocaleUtils.java index d990747..24b7f3e 100644 --- a/src/main/java/org/apache/commons/lang3/LocaleUtils.java +++ b/src/main/java/org/apache/commons/lang3/LocaleUtils.java @@ -37,6 +37,21 @@ import java.util.concurrent.ConcurrentMap; */ public class LocaleUtils { + //----------------------------------------------------------------------- + // class to avoid synchronization (Init on demand) + static class SyncAvoid { + /** Unmodifiable list of available locales. */ + private static final List<Locale> AVAILABLE_LOCALE_LIST; + /** Unmodifiable set of available locales. */ + private static final Set<Locale> AVAILABLE_LOCALE_SET; + + static { + final List<Locale> list = new ArrayList<>(Arrays.asList(Locale.getAvailableLocales())); // extra safe + AVAILABLE_LOCALE_LIST = Collections.unmodifiableList(list); + AVAILABLE_LOCALE_SET = Collections.unmodifiableSet(new HashSet<>(list)); + } + } + /** Concurrent map of language locales by country. */ private static final ConcurrentMap<String, List<Locale>> cLanguagesByCountry = new ConcurrentHashMap<>(); @@ -45,126 +60,75 @@ public class LocaleUtils { private static final ConcurrentMap<String, List<Locale>> cCountriesByLanguage = new ConcurrentHashMap<>(); + //----------------------------------------------------------------------- /** - * <p>{@code LocaleUtils} instances should NOT be constructed in standard programming. - * Instead, the class should be used as {@code LocaleUtils.toLocale("en_GB");}.</p> + * <p>Obtains an unmodifiable list of installed locales.</p> * - * <p>This constructor is public to permit tools that require a JavaBean instance - * to operate.</p> + * <p>This method is a wrapper around {@link Locale#getAvailableLocales()}. + * It is more efficient, as the JDK method must create a new array each + * time it is called.</p> + * + * @return the unmodifiable list of available locales */ - public LocaleUtils() { + public static List<Locale> availableLocaleList() { + return SyncAvoid.AVAILABLE_LOCALE_LIST; } //----------------------------------------------------------------------- /** - * <p>Converts a String to a Locale.</p> - * - * <p>This method takes the string format of a locale and creates the - * locale object from it.</p> - * - * <pre> - * LocaleUtils.toLocale("") = new Locale("", "") - * LocaleUtils.toLocale("en") = new Locale("en", "") - * LocaleUtils.toLocale("en_GB") = new Locale("en", "GB") - * LocaleUtils.toLocale("en_001") = new Locale("en", "001") - * LocaleUtils.toLocale("en_GB_xxx") = new Locale("en", "GB", "xxx") (#) - * </pre> - * - * <p>(#) The behavior of the JDK variant constructor changed between JDK1.3 and JDK1.4. - * In JDK1.3, the constructor upper cases the variant, in JDK1.4, it doesn't. - * Thus, the result from getVariant() may vary depending on your JDK.</p> + * <p>Obtains an unmodifiable set of installed locales.</p> * - * <p>This method validates the input strictly. - * The language code must be lowercase. - * The country code must be uppercase. - * The separator must be an underscore. - * The length must be correct. - * </p> + * <p>This method is a wrapper around {@link Locale#getAvailableLocales()}. + * It is more efficient, as the JDK method must create a new array each + * time it is called.</p> * - * @param str the locale String to convert, null returns null - * @return a Locale, null if null input - * @throws IllegalArgumentException if the string is an invalid format - * @see Locale#forLanguageTag(String) + * @return the unmodifiable set of available locales */ - public static Locale toLocale(final String str) { - if (str == null) { - return null; - } - if (str.isEmpty()) { // LANG-941 - JDK 8 introduced an empty locale where all fields are blank - return new Locale(StringUtils.EMPTY, StringUtils.EMPTY); - } - if (str.contains("#")) { // LANG-879 - Cannot handle Java 7 script & extensions - throw new IllegalArgumentException("Invalid locale format: " + str); - } - final int len = str.length(); - if (len < 2) { - throw new IllegalArgumentException("Invalid locale format: " + str); - } - final char ch0 = str.charAt(0); - if (ch0 == '_') { - if (len < 3) { - throw new IllegalArgumentException("Invalid locale format: " + str); - } - final char ch1 = str.charAt(1); - final char ch2 = str.charAt(2); - if (!Character.isUpperCase(ch1) || !Character.isUpperCase(ch2)) { - throw new IllegalArgumentException("Invalid locale format: " + str); - } - if (len == 3) { - return new Locale(StringUtils.EMPTY, str.substring(1, 3)); - } - if (len < 5) { - throw new IllegalArgumentException("Invalid locale format: " + str); - } - if (str.charAt(3) != '_') { - throw new IllegalArgumentException("Invalid locale format: " + str); - } - return new Locale(StringUtils.EMPTY, str.substring(1, 3), str.substring(4)); - } - - return parseLocale(str); + public static Set<Locale> availableLocaleSet() { + return SyncAvoid.AVAILABLE_LOCALE_SET; } + //----------------------------------------------------------------------- /** - * Tries to parse a locale from the given String. + * <p>Obtains the list of countries supported for a given language.</p> * - * @param str the String to parse a locale from. - * @return a Locale instance parsed from the given String. - * @throws IllegalArgumentException if the given String can not be parsed. + * <p>This method takes a language code and searches to find the + * countries available for that language. Variant locales are removed.</p> + * + * @param languageCode the 2 letter language code, null returns empty + * @return an unmodifiable List of Locale objects, not null */ - private static Locale parseLocale(final String str) { - if (isISO639LanguageCode(str)) { - return new Locale(str); + public static List<Locale> countriesByLanguage(final String languageCode) { + if (languageCode == null) { + return Collections.emptyList(); } - - final String[] segments = str.split("_", -1); - final String language = segments[0]; - if (segments.length == 2) { - final String country = segments[1]; - if (isISO639LanguageCode(language) && isISO3166CountryCode(country) || - isNumericAreaCode(country)) { - return new Locale(language, country); - } - } else if (segments.length == 3) { - final String country = segments[1]; - final String variant = segments[2]; - if (isISO639LanguageCode(language) && - (country.isEmpty() || isISO3166CountryCode(country) || isNumericAreaCode(country)) && - !variant.isEmpty()) { - return new Locale(language, country, variant); + List<Locale> countries = cCountriesByLanguage.get(languageCode); + if (countries == null) { + countries = new ArrayList<>(); + final List<Locale> locales = availableLocaleList(); + for (final Locale locale : locales) { + if (languageCode.equals(locale.getLanguage()) && + !locale.getCountry().isEmpty() && + locale.getVariant().isEmpty()) { + countries.add(locale); + } } + countries = Collections.unmodifiableList(countries); + cCountriesByLanguage.putIfAbsent(languageCode, countries); + countries = cCountriesByLanguage.get(languageCode); } - throw new IllegalArgumentException("Invalid locale format: " + str); + return countries; } + //----------------------------------------------------------------------- /** - * Checks whether the given String is a ISO 639 compliant language code. + * <p>Checks if the locale specified is in the list of available locales.</p> * - * @param str the String to check. - * @return true, if the given String is a ISO 639 compliant language code. + * @param locale the Locale object to check if it is available + * @return true if the locale is a known locale */ - private static boolean isISO639LanguageCode(final String str) { - return StringUtils.isAllLowerCase(str) && (str.length() == 2 || str.length() == 3); + public static boolean isAvailableLocale(final Locale locale) { + return availableLocaleList().contains(locale); } /** @@ -178,6 +142,16 @@ public class LocaleUtils { } /** + * Checks whether the given String is a ISO 639 compliant language code. + * + * @param str the String to check. + * @return true, if the given String is a ISO 639 compliant language code. + */ + private static boolean isISO639LanguageCode(final String str) { + return StringUtils.isAllLowerCase(str) && (str.length() == 2 || str.length() == 3); + } + + /** * Checks whether the given String is a UN M.49 numeric area code. * * @param str the String to check @@ -189,6 +163,37 @@ public class LocaleUtils { //----------------------------------------------------------------------- /** + * <p>Obtains the list of languages supported for a given country.</p> + * + * <p>This method takes a country code and searches to find the + * languages available for that country. Variant locales are removed.</p> + * + * @param countryCode the 2 letter country code, null returns empty + * @return an unmodifiable List of Locale objects, not null + */ + public static List<Locale> languagesByCountry(final String countryCode) { + if (countryCode == null) { + return Collections.emptyList(); + } + List<Locale> langs = cLanguagesByCountry.get(countryCode); + if (langs == null) { + langs = new ArrayList<>(); + final List<Locale> locales = availableLocaleList(); + for (final Locale locale : locales) { + if (countryCode.equals(locale.getCountry()) && + locale.getVariant().isEmpty()) { + langs.add(locale); + } + } + langs = Collections.unmodifiableList(langs); + cLanguagesByCountry.putIfAbsent(countryCode, langs); + langs = cLanguagesByCountry.get(countryCode); + } + return langs; + } + + //----------------------------------------------------------------------- + /** * <p>Obtains the list of locales to search through when performing * a locale search.</p> * @@ -239,121 +244,116 @@ public class LocaleUtils { return Collections.unmodifiableList(list); } - //----------------------------------------------------------------------- /** - * <p>Obtains an unmodifiable list of installed locales.</p> - * - * <p>This method is a wrapper around {@link Locale#getAvailableLocales()}. - * It is more efficient, as the JDK method must create a new array each - * time it is called.</p> + * Tries to parse a locale from the given String. * - * @return the unmodifiable list of available locales + * @param str the String to parse a locale from. + * @return a Locale instance parsed from the given String. + * @throws IllegalArgumentException if the given String can not be parsed. */ - public static List<Locale> availableLocaleList() { - return SyncAvoid.AVAILABLE_LOCALE_LIST; + private static Locale parseLocale(final String str) { + if (isISO639LanguageCode(str)) { + return new Locale(str); + } + + final String[] segments = str.split("_", -1); + final String language = segments[0]; + if (segments.length == 2) { + final String country = segments[1]; + if (isISO639LanguageCode(language) && isISO3166CountryCode(country) || + isNumericAreaCode(country)) { + return new Locale(language, country); + } + } else if (segments.length == 3) { + final String country = segments[1]; + final String variant = segments[2]; + if (isISO639LanguageCode(language) && + (country.isEmpty() || isISO3166CountryCode(country) || isNumericAreaCode(country)) && + !variant.isEmpty()) { + return new Locale(language, country, variant); + } + } + throw new IllegalArgumentException("Invalid locale format: " + str); } //----------------------------------------------------------------------- /** - * <p>Obtains an unmodifiable set of installed locales.</p> + * <p>Converts a String to a Locale.</p> * - * <p>This method is a wrapper around {@link Locale#getAvailableLocales()}. - * It is more efficient, as the JDK method must create a new array each - * time it is called.</p> + * <p>This method takes the string format of a locale and creates the + * locale object from it.</p> * - * @return the unmodifiable set of available locales - */ - public static Set<Locale> availableLocaleSet() { - return SyncAvoid.AVAILABLE_LOCALE_SET; - } - - //----------------------------------------------------------------------- - /** - * <p>Checks if the locale specified is in the list of available locales.</p> + * <pre> + * LocaleUtils.toLocale("") = new Locale("", "") + * LocaleUtils.toLocale("en") = new Locale("en", "") + * LocaleUtils.toLocale("en_GB") = new Locale("en", "GB") + * LocaleUtils.toLocale("en_001") = new Locale("en", "001") + * LocaleUtils.toLocale("en_GB_xxx") = new Locale("en", "GB", "xxx") (#) + * </pre> * - * @param locale the Locale object to check if it is available - * @return true if the locale is a known locale - */ - public static boolean isAvailableLocale(final Locale locale) { - return availableLocaleList().contains(locale); - } - - //----------------------------------------------------------------------- - /** - * <p>Obtains the list of languages supported for a given country.</p> + * <p>(#) The behavior of the JDK variant constructor changed between JDK1.3 and JDK1.4. + * In JDK1.3, the constructor upper cases the variant, in JDK1.4, it doesn't. + * Thus, the result from getVariant() may vary depending on your JDK.</p> * - * <p>This method takes a country code and searches to find the - * languages available for that country. Variant locales are removed.</p> + * <p>This method validates the input strictly. + * The language code must be lowercase. + * The country code must be uppercase. + * The separator must be an underscore. + * The length must be correct. + * </p> * - * @param countryCode the 2 letter country code, null returns empty - * @return an unmodifiable List of Locale objects, not null + * @param str the locale String to convert, null returns null + * @return a Locale, null if null input + * @throws IllegalArgumentException if the string is an invalid format + * @see Locale#forLanguageTag(String) */ - public static List<Locale> languagesByCountry(final String countryCode) { - if (countryCode == null) { - return Collections.emptyList(); + public static Locale toLocale(final String str) { + if (str == null) { + return null; } - List<Locale> langs = cLanguagesByCountry.get(countryCode); - if (langs == null) { - langs = new ArrayList<>(); - final List<Locale> locales = availableLocaleList(); - for (final Locale locale : locales) { - if (countryCode.equals(locale.getCountry()) && - locale.getVariant().isEmpty()) { - langs.add(locale); - } + if (str.isEmpty()) { // LANG-941 - JDK 8 introduced an empty locale where all fields are blank + return new Locale(StringUtils.EMPTY, StringUtils.EMPTY); + } + if (str.contains("#")) { // LANG-879 - Cannot handle Java 7 script & extensions + throw new IllegalArgumentException("Invalid locale format: " + str); + } + final int len = str.length(); + if (len < 2) { + throw new IllegalArgumentException("Invalid locale format: " + str); + } + final char ch0 = str.charAt(0); + if (ch0 == '_') { + if (len < 3) { + throw new IllegalArgumentException("Invalid locale format: " + str); } - langs = Collections.unmodifiableList(langs); - cLanguagesByCountry.putIfAbsent(countryCode, langs); - langs = cLanguagesByCountry.get(countryCode); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + if (!Character.isUpperCase(ch1) || !Character.isUpperCase(ch2)) { + throw new IllegalArgumentException("Invalid locale format: " + str); + } + if (len == 3) { + return new Locale(StringUtils.EMPTY, str.substring(1, 3)); + } + if (len < 5) { + throw new IllegalArgumentException("Invalid locale format: " + str); + } + if (str.charAt(3) != '_') { + throw new IllegalArgumentException("Invalid locale format: " + str); + } + return new Locale(StringUtils.EMPTY, str.substring(1, 3), str.substring(4)); } - return langs; + + return parseLocale(str); } - //----------------------------------------------------------------------- /** - * <p>Obtains the list of countries supported for a given language.</p> - * - * <p>This method takes a language code and searches to find the - * countries available for that language. Variant locales are removed.</p> + * <p>{@code LocaleUtils} instances should NOT be constructed in standard programming. + * Instead, the class should be used as {@code LocaleUtils.toLocale("en_GB");}.</p> * - * @param languageCode the 2 letter language code, null returns empty - * @return an unmodifiable List of Locale objects, not null + * <p>This constructor is public to permit tools that require a JavaBean instance + * to operate.</p> */ - public static List<Locale> countriesByLanguage(final String languageCode) { - if (languageCode == null) { - return Collections.emptyList(); - } - List<Locale> countries = cCountriesByLanguage.get(languageCode); - if (countries == null) { - countries = new ArrayList<>(); - final List<Locale> locales = availableLocaleList(); - for (final Locale locale : locales) { - if (languageCode.equals(locale.getLanguage()) && - !locale.getCountry().isEmpty() && - locale.getVariant().isEmpty()) { - countries.add(locale); - } - } - countries = Collections.unmodifiableList(countries); - cCountriesByLanguage.putIfAbsent(languageCode, countries); - countries = cCountriesByLanguage.get(languageCode); - } - return countries; - } - - //----------------------------------------------------------------------- - // class to avoid synchronization (Init on demand) - static class SyncAvoid { - /** Unmodifiable list of available locales. */ - private static final List<Locale> AVAILABLE_LOCALE_LIST; - /** Unmodifiable set of available locales. */ - private static final Set<Locale> AVAILABLE_LOCALE_SET; - - static { - final List<Locale> list = new ArrayList<>(Arrays.asList(Locale.getAvailableLocales())); // extra safe - AVAILABLE_LOCALE_LIST = Collections.unmodifiableList(list); - AVAILABLE_LOCALE_SET = Collections.unmodifiableSet(new HashSet<>(list)); - } + public LocaleUtils() { } }