/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.lang3; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.function.Predicate; import java.util.stream.Collectors; /** * Operations to assist when working with a {@link Locale}. * *
This class tries to handle {@code null} input gracefully. * An exception will not be thrown for a {@code null} input. * Each method documents its behavior in more detail.
* * @since 2.2 */ public class LocaleUtils { private static final char UNDERSCORE = '_'; private static final char DASH = '-'; // class to avoid synchronization (Init on demand) static class SyncAvoid { /** Unmodifiable list of available locales. */ private static final ListThis 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.
* * @return the unmodifiable list of available locales */ public static ListThis 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.
* * @return the unmodifiable set of available locales */ public static SetThis method takes a language code and searches to find the * countries available for that language. Variant locales are removed.
* * @param languageCode the 2 letter language code, null returns empty * @return an unmodifiable List of Locale objects, not null */ public static ListThis method takes a country code and searches to find the * languages available for that country. Variant locales are removed.
* * @param countryCode the 2-letter country code, null returns empty * @return an unmodifiable List of Locale objects, not null */ public static List
* localeLookupList(Locale("fr", "CA", "xxx"))
* = [Locale("fr", "CA", "xxx"), Locale("fr", "CA"), Locale("fr")]
*
*
* @param locale the locale to start from
* @return the unmodifiable list of Locale objects, 0 being locale, not null
*/
public static List
* localeLookupList(Locale("fr", "CA", "xxx"), Locale("en"))
* = [Locale("fr", "CA", "xxx"), Locale("fr", "CA"), Locale("fr"), Locale("en"]
*
*
* The result list begins with the most specific locale, then the * next more general and so on, finishing with the default locale. * The list will never contain the same locale twice.
* * @param locale the locale to start from, null returns empty list * @param defaultLocale the default locale to use if no other is found * @return the unmodifiable list of Locale objects, 0 being locale, not null */ public static ListThis method takes the string format of a locale and creates the * locale object from it.
* *
* LocaleUtils.toLocale("") = new Locale("", "")
* LocaleUtils.toLocale("en") = new Locale("en", "")
* LocaleUtils.toLocale("en_GB") = new Locale("en", "GB")
* 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") (#)
*
*
* (#) 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.
* *This method validates the input strictly. * The language code must be lowercase. * The country code must be uppercase. * The separator must be an underscore or a dash. * The length must be correct. *
* * @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 Locale toLocale(final String str) { if (str == null) { // TODO Should this return the default locale? 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 == UNDERSCORE || ch0 == DASH) { 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) != ch0) { throw new IllegalArgumentException("Invalid locale format: " + str); } return new Locale(StringUtils.EMPTY, str.substring(1, 3), str.substring(4)); } return parseLocale(str); } /** * {@link LocaleUtils} instances should NOT be constructed in standard programming. * Instead, the class should be used as {@code LocaleUtils.toLocale("en_GB");}. * *This constructor is public to permit tools that require a JavaBean instance * to operate.
*/ public LocaleUtils() { } }