Add LocaleUtils.isLanguageUndetermined(Locale)

This commit is contained in:
Gary Gregory 2023-08-10 11:51:54 -04:00
parent 43d3751787
commit d26170a976
3 changed files with 36 additions and 0 deletions

View File

@ -59,6 +59,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="aherbert" due-to="Dan Watson">Add syntax for optional tokens to DurationFormatUtils #1062.</action> <action type="add" dev="aherbert" due-to="Dan Watson">Add syntax for optional tokens to DurationFormatUtils #1062.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add ArrayFill.</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add ArrayFill.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add FastDateParser.TimeZoneStrategy.TzInfo.toString().</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add FastDateParser.TimeZoneStrategy.TzInfo.toString().</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add LocaleUtils.isLanguageUndetermined(Locale).</action>
<!-- UPDATE --> <!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-parent from 58 to 59.</action> <action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-parent from 58 to 59.</action>
</release> </release>

View File

@ -38,7 +38,9 @@ import java.util.stream.Collectors;
* @since 2.2 * @since 2.2
*/ */
public class LocaleUtils { public class LocaleUtils {
private static final char UNDERSCORE = '_'; private static final char UNDERSCORE = '_';
private static final String UNDETERMINED = "und";
private static final char DASH = '-'; private static final char DASH = '-';
// class to avoid synchronization (Init on demand) // class to avoid synchronization (Init on demand)
@ -140,6 +142,22 @@ public class LocaleUtils {
return StringUtils.isAllLowerCase(str) && (str.length() == 2 || str.length() == 3); return StringUtils.isAllLowerCase(str) && (str.length() == 2 || str.length() == 3);
} }
/**
* Tests whether a Locale's language is undetermined.
* <p>
* A Locale's language tag is undetermined if it's value is {@code "und"}. If a language is empty, or not well-formed (for example, "a" or"e2"), it will be
* equal to {@code "und"}.
* </p>
*
* @param locale the locale to test.
* @return whether a Locale's language is undetermined.
* @see Locale#toLanguageTag()
* @since 3.14.0
*/
public static boolean isLanguageUndetermined(final Locale locale) {
return locale == null || UNDETERMINED.equals(locale.toLanguageTag());
}
/** /**
* Checks whether the given String is a UN M.49 numeric area code. * Checks whether the given String is a UN M.49 numeric area code.
* *

View File

@ -19,6 +19,7 @@ package org.apache.commons.lang3;
import static org.apache.commons.lang3.JavaVersion.JAVA_1_4; import static org.apache.commons.lang3.JavaVersion.JAVA_1_4;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertSame;
@ -359,6 +360,22 @@ public class LocaleUtilsTest extends AbstractLangTest {
assertEquals(set.contains(LOCALE_QQ_ZZ), LocaleUtils.isAvailableLocale(LOCALE_QQ_ZZ)); assertEquals(set.contains(LOCALE_QQ_ZZ), LocaleUtils.isAvailableLocale(LOCALE_QQ_ZZ));
} }
@Test
public void testIsLanguageUndetermined() {
final Set<Locale> set = LocaleUtils.availableLocaleSet();
// Determined
assertNotEquals(set.contains(LOCALE_EN), LocaleUtils.isLanguageUndetermined(LOCALE_EN));
assertNotEquals(set.contains(LOCALE_EN_US), LocaleUtils.isLanguageUndetermined(LOCALE_EN_US));
assertNotEquals(set.contains(LOCALE_FR), LocaleUtils.isLanguageUndetermined(LOCALE_FR));
assertNotEquals(set.contains(LOCALE_FR_CA), LocaleUtils.isLanguageUndetermined(LOCALE_FR_CA));
// Undetermined
assertEquals(set.contains(LOCALE_EN_US_ZZZZ), LocaleUtils.isLanguageUndetermined(LOCALE_EN_US_ZZZZ));
assertEquals(set.contains(LOCALE_QQ), LocaleUtils.isLanguageUndetermined(LOCALE_QQ));
assertEquals(set.contains(LOCALE_QQ_ZZ), LocaleUtils.isLanguageUndetermined(LOCALE_QQ_ZZ));
//
assertTrue(LocaleUtils.isLanguageUndetermined(null));
}
/** /**
* Test for 3-chars locale, further details at LANG-915 * Test for 3-chars locale, further details at LANG-915
* *