Merge branch 'master' of

https://gitbox.apache.org/repos/asf/commons-lang.git
This commit is contained in:
Gary Gregory 2022-03-07 11:18:47 -05:00
commit 494dfc9e6b
3 changed files with 13 additions and 7 deletions

View File

@ -36,6 +36,8 @@ import java.util.concurrent.ConcurrentMap;
* @since 2.2 * @since 2.2
*/ */
public class LocaleUtils { public class LocaleUtils {
private static final char UNDERSCORE = '_';
private static final char DASH = '-';
// class to avoid synchronization (Init on demand) // class to avoid synchronization (Init on demand)
static class SyncAvoid { static class SyncAvoid {
@ -248,7 +250,9 @@ public class LocaleUtils {
return new Locale(str); return new Locale(str);
} }
final String[] segments = str.split("_", -1); final String[] segments = str.indexOf(UNDERSCORE) != -1
? str.split(String.valueOf(UNDERSCORE), -1)
: str.split(String.valueOf(DASH), -1);
final String language = segments[0]; final String language = segments[0];
if (segments.length == 2) { if (segments.length == 2) {
final String country = segments[1]; final String country = segments[1];
@ -289,6 +293,7 @@ public class LocaleUtils {
* LocaleUtils.toLocale("") = new Locale("", "") * LocaleUtils.toLocale("") = new Locale("", "")
* LocaleUtils.toLocale("en") = new Locale("en", "") * 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-GB") = new Locale("en", "GB")
* LocaleUtils.toLocale("en_001") = new Locale("en", "001") * LocaleUtils.toLocale("en_001") = new Locale("en", "001")
* LocaleUtils.toLocale("en_GB_xxx") = new Locale("en", "GB", "xxx") (#) * LocaleUtils.toLocale("en_GB_xxx") = new Locale("en", "GB", "xxx") (#)
* </pre> * </pre>
@ -300,7 +305,7 @@ public class LocaleUtils {
* <p>This method validates the input strictly. * <p>This method validates the input strictly.
* The language code must be lowercase. * The language code must be lowercase.
* The country code must be uppercase. * The country code must be uppercase.
* The separator must be an underscore. * The separator must be an underscore or a dash.
* The length must be correct. * The length must be correct.
* </p> * </p>
* *
@ -325,7 +330,7 @@ public class LocaleUtils {
throw new IllegalArgumentException("Invalid locale format: " + str); throw new IllegalArgumentException("Invalid locale format: " + str);
} }
final char ch0 = str.charAt(0); final char ch0 = str.charAt(0);
if (ch0 == '_') { if (ch0 == UNDERSCORE || ch0 == DASH) {
if (len < 3) { if (len < 3) {
throw new IllegalArgumentException("Invalid locale format: " + str); throw new IllegalArgumentException("Invalid locale format: " + str);
} }
@ -340,7 +345,7 @@ public class LocaleUtils {
if (len < 5) { if (len < 5) {
throw new IllegalArgumentException("Invalid locale format: " + str); throw new IllegalArgumentException("Invalid locale format: " + str);
} }
if (str.charAt(3) != '_') { if (str.charAt(3) != ch0) {
throw new IllegalArgumentException("Invalid locale format: " + str); throw new IllegalArgumentException("Invalid locale format: " + str);
} }
return new Locale(StringUtils.EMPTY, str.substring(1, 3), str.substring(4)); return new Locale(StringUtils.EMPTY, str.substring(1, 3), str.substring(4));

View File

@ -725,6 +725,7 @@ public class MethodUtils {
* @param cls The class that will be subjected to the method search * @param cls The class that will be subjected to the method search
* @param methodName The method that we wish to call * @param methodName The method that we wish to call
* @param parameterTypes Argument class types * @param parameterTypes Argument class types
* @throws IllegalStateException if there is no unique result
* @return The method * @return The method
* *
* @since 3.5 * @since 3.5

View File

@ -170,11 +170,10 @@ public class LocaleUtilsTest {
@Test @Test
public void testToLocale_2Part() { public void testToLocale_2Part() {
assertValidToLocale("us_EN", "us", "EN"); assertValidToLocale("us_EN", "us", "EN");
assertValidToLocale("us-EN", "us", "EN");
//valid though doesn't exist //valid though doesn't exist
assertValidToLocale("us_ZH", "us", "ZH"); assertValidToLocale("us_ZH", "us", "ZH");
assertThrows(
IllegalArgumentException.class, () -> LocaleUtils.toLocale("us-EN"), "Should fail as not underscore");
assertThrows( assertThrows(
IllegalArgumentException.class, IllegalArgumentException.class,
() -> LocaleUtils.toLocale("us_En"), () -> LocaleUtils.toLocale("us_En"),
@ -203,6 +202,7 @@ public class LocaleUtilsTest {
@Test @Test
public void testToLocale_3Part() { public void testToLocale_3Part() {
assertValidToLocale("us_EN_A", "us", "EN", "A"); assertValidToLocale("us_EN_A", "us", "EN", "A");
assertValidToLocale("us-EN-A", "us", "EN", "A");
// this isn't pretty, but was caused by a jdk bug it seems // this isn't pretty, but was caused by a jdk bug it seems
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4210525 // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4210525
if (SystemUtils.isJavaVersionAtLeast(JAVA_1_4)) { if (SystemUtils.isJavaVersionAtLeast(JAVA_1_4)) {
@ -214,7 +214,7 @@ public class LocaleUtilsTest {
} }
assertThrows( assertThrows(
IllegalArgumentException.class, () -> LocaleUtils.toLocale("us_EN-a"), "Should fail as not underscore"); IllegalArgumentException.class, () -> LocaleUtils.toLocale("us_EN-a"), "Should fail as no consistent delimiter");
assertThrows( assertThrows(
IllegalArgumentException.class, () -> LocaleUtils.toLocale("uu_UU_"), "Must be 3, 5 or 7+ in length"); IllegalArgumentException.class, () -> LocaleUtils.toLocale("uu_UU_"), "Must be 3, 5 or 7+ in length");
} }