Extract some helper methods to make the code better readable

Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/d3146a54
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/d3146a54
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/d3146a54

Branch: refs/heads/release
Commit: d3146a545612422d3f8078f10afa00342b327199
Parents: d547412
Author: Benedikt Ritter <brit...@apache.org>
Authored: Mon Apr 17 12:29:05 2017 +0200
Committer: Benedikt Ritter <brit...@apache.org>
Committed: Mon Apr 17 12:29:05 2017 +0200

----------------------------------------------------------------------
 .../org/apache/commons/lang3/LocaleUtils.java   | 60 +++++++++++++++-----
 1 file changed, 47 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/d3146a54/src/main/java/org/apache/commons/lang3/LocaleUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/lang3/LocaleUtils.java 
b/src/main/java/org/apache/commons/lang3/LocaleUtils.java
index 09479f6..58eb21b 100644
--- a/src/main/java/org/apache/commons/lang3/LocaleUtils.java
+++ b/src/main/java/org/apache/commons/lang3/LocaleUtils.java
@@ -123,35 +123,39 @@ public class LocaleUtils {
             return new Locale(StringUtils.EMPTY, str.substring(1, 3), 
str.substring(4));
         }
 
-        return parseLocale(str, len);
+        return parseLocale(str);
     }
 
-    private static Locale parseLocale(final String str, final int len) {
+    /**
+     * Tries to parse a locale from the given String.
+     *
+     * @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.
+     */
+    private static Locale parseLocale(final String str) {
         final String[] segments = str.split("_", -1);
         final int segmentCount = segments.length -1;
-        final String country = segments[0];
+        final String language = segments[0];
         switch (segmentCount) {
             case 0:
-                if (StringUtils.isAllLowerCase(str) && (len == 2 || len == 3)) 
{
+                if (isISO639LanguageCode(str)) {
                     return new Locale(str);
                 }
                 throw new IllegalArgumentException("Invalid locale format: " + 
str);
 
             case 1:
-                if (StringUtils.isAllLowerCase(country) &&
-                    (country.length() == 2 || country.length() == 3) &&
-                     (segments[1].length() == 2 && 
StringUtils.isAllUpperCase(segments[1])) ||
-                      (segments[1].length() == 3 && 
StringUtils.isNumeric(segments[1]))) {
-                    return new Locale(country, segments[1]);
+                if (isISO639LanguageCode(language) && 
isISO3166CountryCode(segments[1]) ||
+                      isNumericAreaCode(segments[1])) {
+                    return new Locale(language, segments[1]);
                 }
                 throw new IllegalArgumentException("Invalid locale format: " + 
str);
 
             case 2:
-                if (StringUtils.isAllLowerCase(country) &&
-                    (country.length() == 2 || country.length() == 3) &&
-                    (segments[1].length() == 0 || segments[1].length() == 2 && 
StringUtils.isAllUpperCase(segments[1])) &&
+                if (isISO639LanguageCode(language) &&
+                    (segments[1].length() == 0 || 
isISO3166CountryCode(segments[1])) &&
                      segments[2].length() > 0) {
-                    return new Locale(country, segments[1], segments[2]);
+                    return new Locale(language, segments[1], segments[2]);
                 }
 
             //$FALL-THROUGH$
@@ -160,6 +164,36 @@ 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 ISO 3166 alpha-2 country code.
+     *
+     * @param str the String to check
+     * @return true, is the given String is a ISO 3166 compliant country code.
+     */
+    private static boolean isISO3166CountryCode(final String str) {
+        return StringUtils.isAllUpperCase(str) && str.length() == 2;
+    }
+
+    /**
+     * Checks whether the given String is a UN M.49 numeric area code.
+     *
+     * @param str the String to check
+     * @return true, is the given String is a UN M.49 numeric area code.
+     */
+    private static boolean isNumericAreaCode(final String str) {
+        return StringUtils.isNumeric(str) && str.length() == 3;
+    }
+
     //-----------------------------------------------------------------------
     /**
      * <p>Obtains the list of locales to search through when performing

Reply via email to