Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / libaddressinput / src / java / src / com / android / i18n / addressinput / Util.java
index 0547543..f883c3b 100644 (file)
@@ -26,201 +26,201 @@ import java.util.regex.Pattern;
  * Utility functions used by the address widget.
  */
 class Util {
-    /**
-     * This variable is in upper-case, since we convert the language code to upper case before doing
-     * string comparison.
-     */
-    private static final String LATIN_SCRIPT = "LATN";
+  /**
+   * This variable is in upper-case, since we convert the language code to upper case before doing
+   * string comparison.
+   */
+  private static final String LATIN_SCRIPT = "LATN";
 
-    /**
-     * Map of countries that have non-latin local names, with the language that their local names
-     * are in. We only list a country here if we have the appropriate data. Only language sub-tags
-     * are listed.
-     */
-     private static final Map<String, String> nonLatinLocalLanguageCountries =
-             new HashMap<String, String>();
-     static {
-       nonLatinLocalLanguageCountries.put("AM", "hy");
-       nonLatinLocalLanguageCountries.put("CN", "zh");
-       nonLatinLocalLanguageCountries.put("HK", "zh");
-       nonLatinLocalLanguageCountries.put("JP", "ja");
-       nonLatinLocalLanguageCountries.put("KP", "ko");
-       nonLatinLocalLanguageCountries.put("KR", "ko");
-       nonLatinLocalLanguageCountries.put("MO", "zh");
-       nonLatinLocalLanguageCountries.put("TH", "th");
-       nonLatinLocalLanguageCountries.put("TW", "zh");
-       nonLatinLocalLanguageCountries.put("VN", "vi");
-     }
+  /**
+   * Map of countries that have non-latin local names, with the language that their local names
+   * are in. We only list a country here if we have the appropriate data. Only language sub-tags
+   * are listed.
+   */
+  private static final Map<String, String> nonLatinLocalLanguageCountries =
+      new HashMap<String, String>();
+  static {
+    nonLatinLocalLanguageCountries.put("AM", "hy");
+    nonLatinLocalLanguageCountries.put("CN", "zh");
+    nonLatinLocalLanguageCountries.put("HK", "zh");
+    nonLatinLocalLanguageCountries.put("JP", "ja");
+    nonLatinLocalLanguageCountries.put("KP", "ko");
+    nonLatinLocalLanguageCountries.put("KR", "ko");
+    nonLatinLocalLanguageCountries.put("MO", "zh");
+    nonLatinLocalLanguageCountries.put("TH", "th");
+    nonLatinLocalLanguageCountries.put("TW", "zh");
+    nonLatinLocalLanguageCountries.put("VN", "vi");
+  }
 
-    /**
-     * Cannot instantiate this class - private constructor.
-     */
-    private Util() {
-    }
+  /**
+   * Cannot instantiate this class - private constructor.
+   */
+  private Util() {
+  }
 
-    /**
-     * Returns true if the language code is explicitly marked to be in the latin script. For
-     * example, "zh-Latn" would return true, but "zh-TW", "en" and "zh" would all return false.
-     */
-    static boolean isExplicitLatinScript(String languageCode) {
-        // Convert to upper-case for easier comparison.
-        languageCode = languageCode.toUpperCase();
-        // Check to see if the language code contains a script modifier.
-        final Pattern languageCodePattern = Pattern.compile("\\w{2,3}[-_](\\w{4})");
-        Matcher m = languageCodePattern.matcher(languageCode);
-        if (m.lookingAt()) {
-            String script = m.group(1);
-            if (script.equals(LATIN_SCRIPT)) {
-                return true;
-            }
-        }
-        return false;
+  /**
+   * Returns true if the language code is explicitly marked to be in the latin script. For
+   * example, "zh-Latn" would return true, but "zh-TW", "en" and "zh" would all return false.
+   */
+  static boolean isExplicitLatinScript(String languageCode) {
+    // Convert to upper-case for easier comparison.
+    languageCode = languageCode.toUpperCase();
+    // Check to see if the language code contains a script modifier.
+    final Pattern languageCodePattern = Pattern.compile("\\w{2,3}[-_](\\w{4})");
+    Matcher m = languageCodePattern.matcher(languageCode);
+    if (m.lookingAt()) {
+      String script = m.group(1);
+      if (script.equals(LATIN_SCRIPT)) {
+        return true;
+      }
     }
+    return false;
+  }
 
-    /**
-     * Returns the language subtag of a language code. For example, returns "zh" if given "zh-Hans",
-     * "zh-CN" or other "zh" variants. If no language subtag can be found or the language tag is
-     * malformed, returns "und".
-     */
-    static String getLanguageSubtag(String languageCode) {
-        final Pattern languageCodePattern = Pattern
-                .compile("(\\w{2,3})(?:[-_]\\w{4})?(?:[-_]\\w{2})?");
-        Matcher m = languageCodePattern.matcher(languageCode);
-        if (m.matches()) {
-            return m.group(1).toLowerCase();
-        }
-        return "und";
+  /**
+   * Returns the language subtag of a language code. For example, returns "zh" if given "zh-Hans",
+   * "zh-CN" or other "zh" variants. If no language subtag can be found or the language tag is
+   * malformed, returns "und".
+   */
+  static String getLanguageSubtag(String languageCode) {
+    final Pattern languageCodePattern = Pattern
+        .compile("(\\w{2,3})(?:[-_]\\w{4})?(?:[-_]\\w{2})?");
+    Matcher m = languageCodePattern.matcher(languageCode);
+    if (m.matches()) {
+      return m.group(1).toLowerCase();
     }
+    return "und";
+  }
 
-    /**
-     * Trims the string. If the field is empty after trimming, returns null instead. Note that this
-     * only trims ASCII white-space.
-     */
-    static String trimToNull(String originalStr) {
-        if (originalStr == null) {
-            return null;
-        }
-        String trimmedString = originalStr.trim();
-        return (trimmedString.length() == 0) ? null : trimmedString;
+  /**
+   * Trims the string. If the field is empty after trimming, returns null instead. Note that this
+   * only trims ASCII white-space.
+   */
+  static String trimToNull(String originalStr) {
+    if (originalStr == null) {
+      return null;
     }
+    String trimmedString = originalStr.trim();
+    return (trimmedString.length() == 0) ? null : trimmedString;
+  }
 
-    /**
-     * Throws an exception if the object is null, with a generic error message.
-     */
-    static void checkNotNull(Object o) throws NullPointerException {
-        checkNotNull(o, "This object should not be null.");
-    }
+  /**
+   * Throws an exception if the object is null, with a generic error message.
+   */
+  static void checkNotNull(Object o) throws NullPointerException {
+    checkNotNull(o, "This object should not be null.");
+  }
 
-    /**
-     * Throws an exception if the object is null, with the error message supplied.
-     */
-    static void checkNotNull(Object o, String message) throws NullPointerException {
-        if (o == null) {
-            throw new NullPointerException(message);
-        }
+  /**
+   * Throws an exception if the object is null, with the error message supplied.
+   */
+  static void checkNotNull(Object o, String message) throws NullPointerException {
+    if (o == null) {
+      throw new NullPointerException(message);
     }
+  }
 
-    /**
-     * Joins input string with the given separator. If an input string is null, it will be skipped.
-     */
-    static String joinAndSkipNulls(String separator, String... strings) {
-        StringBuilder sb = null;
-        for (String s : strings) {
-            if (s != null) {
-                s = s.trim();
-                if (s.length() > 0) {
-                    if (sb == null) {
-                        sb = new StringBuilder(s);
-                    } else {
-                        sb.append(separator).append(s);
-                    }
-                }
-            }
+  /**
+   * Joins input string with the given separator. If an input string is null, it will be skipped.
+   */
+  static String joinAndSkipNulls(String separator, String... strings) {
+    StringBuilder sb = null;
+    for (String s : strings) {
+      if (s != null) {
+        s = s.trim();
+        if (s.length() > 0) {
+          if (sb == null) {
+            sb = new StringBuilder(s);
+          } else {
+            sb.append(separator).append(s);
+          }
         }
-        return sb == null ? null : sb.toString();
+      }
     }
+    return sb == null ? null : sb.toString();
+  }
 
-    /**
-     * Builds a map of the lower-cased values of the keys, names and local names provided. Each name
-     * and local name is mapped to its respective key in the map.
-     *
-     * @throws IllegalStateException if the names or lnames array is greater than the keys array.
-     */
-    static Map<String, String> buildNameToKeyMap(String[] keys, String[] names, String[] lnames) {
-        if (keys == null) {
-            return null;
-        }
+  /**
+   * Builds a map of the lower-cased values of the keys, names and local names provided. Each name
+   * and local name is mapped to its respective key in the map.
+   *
+   * @throws IllegalStateException if the names or lnames array is greater than the keys array.
+   */
+  static Map<String, String> buildNameToKeyMap(String[] keys, String[] names, String[] lnames) {
+    if (keys == null) {
+      return null;
+    }
 
-        Map<String, String> nameToKeyMap = new HashMap<String, String>();
+    Map<String, String> nameToKeyMap = new HashMap<String, String>();
 
-        int keyLength = keys.length;
-        for (String k : keys) {
-            nameToKeyMap.put(k.toLowerCase(), k);
-        }
-        if (names != null) {
-            if (names.length > keyLength) {
-                throw new IllegalStateException(
-                        "names length (" + names.length + ") is greater than keys length (" +
-                        keys.length + ")");
-            }
-            for (int i = 0; i < keyLength; i++) {
-                // If we have less names than keys, we ignore all missing names. This happens
-                // generally because reg-ex splitting methods on different platforms (java, js etc)
-                // behave differently in the default case. Since missing names are fine, we opt to
-                // be more robust here.
-                if (i < names.length && names[i].length() > 0) {
-                    nameToKeyMap.put(names[i].toLowerCase(), keys[i]);
-                }
-            }
+    int keyLength = keys.length;
+    for (String k : keys) {
+      nameToKeyMap.put(k.toLowerCase(), k);
+    }
+    if (names != null) {
+      if (names.length > keyLength) {
+        throw new IllegalStateException(
+            "names length (" + names.length + ") is greater than keys length (" +
+            keys.length + ")");
+      }
+      for (int i = 0; i < keyLength; i++) {
+        // If we have less names than keys, we ignore all missing names. This happens
+        // generally because reg-ex splitting methods on different platforms (java, js etc)
+        // behave differently in the default case. Since missing names are fine, we opt to
+        // be more robust here.
+        if (i < names.length && names[i].length() > 0) {
+          nameToKeyMap.put(names[i].toLowerCase(), keys[i]);
         }
-        if (lnames != null) {
-            if (lnames.length > keyLength) {
-                throw new IllegalStateException(
-                        "lnames length (" + lnames.length + ") is greater than keys length (" +
-                        keys.length + ")");
-            }
-            for (int i = 0; i < keyLength; i++) {
-                if (i < lnames.length && lnames[i].length() > 0) {
-                    nameToKeyMap.put(lnames[i].toLowerCase(), keys[i]);
-                }
-            }
+      }
+    }
+    if (lnames != null) {
+      if (lnames.length > keyLength) {
+        throw new IllegalStateException(
+            "lnames length (" + lnames.length + ") is greater than keys length (" +
+            keys.length + ")");
+      }
+      for (int i = 0; i < keyLength; i++) {
+        if (i < lnames.length && lnames[i].length() > 0) {
+          nameToKeyMap.put(lnames[i].toLowerCase(), keys[i]);
         }
-        return nameToKeyMap;
+      }
     }
+    return nameToKeyMap;
+  }
 
-    /**
-     * Returns a language code that the widget can use when fetching data, based on a {@link
-     * java.util.Locale} language and the current selected country in the address widget. This
-     * method is necessary since we have to determine later whether a language is "local" or "latin"
-     * for certain countries.
-     *
-     * @param language the current user language
-     * @param currentCountry the current selected country
-     * @return a language code string in BCP-47 format (e.g. "en", "zh-Latn", "zh-Hans" or
-     * "en-US").
-     */
-    static String getWidgetCompatibleLanguageCode(Locale language, String currentCountry) {
-        String country = currentCountry.toUpperCase();
-        // Only do something if the country is one of those where we have names in the local
-        // language as well as in latin script.
-        if (nonLatinLocalLanguageCountries.containsKey(country)) {
-            String languageTag = language.getLanguage();
-            // Only do something if the language tag is _not_ the local language.
-            if (!languageTag.equals(nonLatinLocalLanguageCountries.get(country))) {
-                // Build up the language tag with the country and language specified, and add in the
-                // script-tag of "Latn" explicitly, since this is _not_ a local language. This means
-                // that we might create a language tag of "th-Latn", which is not what the actual
-                // language being used is, but it indicates that we prefer "Latn" names to whatever
-                // the local alternative was.
-                StringBuilder languageTagBuilder = new StringBuilder(languageTag);
-                languageTagBuilder.append("_latn");
-                if (language.getCountry().length() > 0) {
-                    languageTagBuilder.append("_");
-                    languageTagBuilder.append(language.getCountry());
-                }
-                return languageTagBuilder.toString();
-            }
+  /**
+   * Returns a language code that the widget can use when fetching data, based on a {@link
+   * java.util.Locale} language and the current selected country in the address widget. This
+   * method is necessary since we have to determine later whether a language is "local" or "latin"
+   * for certain countries.
+   *
+   * @param language the current user language
+   * @param currentCountry the current selected country
+   * @return a language code string in BCP-47 format (e.g. "en", "zh-Latn", "zh-Hans" or
+   * "en-US").
+   */
+  static String getWidgetCompatibleLanguageCode(Locale language, String currentCountry) {
+    String country = currentCountry.toUpperCase();
+    // Only do something if the country is one of those where we have names in the local
+    // language as well as in latin script.
+    if (nonLatinLocalLanguageCountries.containsKey(country)) {
+      String languageTag = language.getLanguage();
+      // Only do something if the language tag is _not_ the local language.
+      if (!languageTag.equals(nonLatinLocalLanguageCountries.get(country))) {
+        // Build up the language tag with the country and language specified, and add in the
+        // script-tag of "Latn" explicitly, since this is _not_ a local language. This means
+        // that we might create a language tag of "th-Latn", which is not what the actual
+        // language being used is, but it indicates that we prefer "Latn" names to whatever
+        // the local alternative was.
+        StringBuilder languageTagBuilder = new StringBuilder(languageTag);
+        languageTagBuilder.append("_latn");
+        if (language.getCountry().length() > 0) {
+          languageTagBuilder.append("_");
+          languageTagBuilder.append(language.getCountry());
         }
-        return language.toString();
+        return languageTagBuilder.toString();
+      }
     }
+    return language.toString();
+  }
 }