Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id ABD93200C59 for ; Mon, 17 Apr 2017 12:52:04 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id AA82D160BB2; Mon, 17 Apr 2017 10:52:04 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id F1FFE160B9C for ; Mon, 17 Apr 2017 12:52:03 +0200 (CEST) Received: (qmail 70595 invoked by uid 500); 17 Apr 2017 10:52:01 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 70580 invoked by uid 99); 17 Apr 2017 10:52:01 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 17 Apr 2017 10:52:01 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 315CDDF984; Mon, 17 Apr 2017 10:52:01 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: britter@apache.org To: commits@commons.apache.org Date: Mon, 17 Apr 2017 10:52:03 -0000 Message-Id: <248b2eb516064bb3998409960d12e53a@git.apache.org> In-Reply-To: <983ce83d08174ee6b691e78721fa0ab1@git.apache.org> References: <983ce83d08174ee6b691e78721fa0ab1@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [3/6] [lang] Extract some helper methods to make the code better readable archived-at: Mon, 17 Apr 2017 10:52:04 -0000 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/master Commit: d3146a545612422d3f8078f10afa00342b327199 Parents: d547412 Author: Benedikt Ritter Authored: Mon Apr 17 12:29:05 2017 +0200 Committer: Benedikt Ritter 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; + } + //----------------------------------------------------------------------- /** *

Obtains the list of locales to search through when performing