JAVA: updated libphonenumber API
authorroes@google.com <roes@google.com@ee073f10-1060-11df-b6a4-87a95322a99c>
Thu, 19 Sep 2013 14:59:31 +0000 (14:59 +0000)
committerroes@google.com <roes@google.com@ee073f10-1060-11df-b6a4-87a95322a99c>
Thu, 19 Sep 2013 14:59:31 +0000 (14:59 +0000)
git-svn-id: http://libphonenumber.googlecode.com/svn/trunk@609 ee073f10-1060-11df-b6a4-87a95322a99c

java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java
java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java
java/libphonenumber/test/com/google/i18n/phonenumbers/RegionCode.java
java/release_notes.txt

index 62afa54..8ad8b80 100644 (file)
@@ -81,6 +81,11 @@ public class PhoneNumberUtil {
   // a mobile phone in Colombia.
   private static final String COLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX = "3";
 
+  // Map of country calling codes that use a mobile token before the area code. One example of when
+  // this is relevant is when determining the length of the national destination code, which should
+  // be the length of the area code plus the length of the mobile token.
+  private static final Map<Integer, String> MOBILE_TOKEN_MAPPINGS;
+
   // The PLUS_SIGN signifies the international prefix.
   static final char PLUS_SIGN = '+';
 
@@ -107,6 +112,11 @@ public class PhoneNumberUtil {
   private static final Map<Character, Character> ALL_PLUS_NUMBER_GROUPING_SYMBOLS;
 
   static {
+    HashMap<Integer, String> mobileTokenMap = new HashMap<Integer, String>();
+    mobileTokenMap.put(52, "1");
+    mobileTokenMap.put(54, "9");
+    MOBILE_TOKEN_MAPPINGS = Collections.unmodifiableMap(mobileTokenMap);
+
     // Simple ASCII digits map used to populate ALPHA_PHONE_MAPPINGS and
     // ALL_PLUS_NUMBER_GROUPING_SYMBOLS.
     HashMap<Character, Character> asciiDigitMappings = new HashMap<Character, Character>();
@@ -878,20 +888,36 @@ public class PhoneNumberUtil {
       return 0;
     }
 
-    if (getRegionCodeForCountryCode(number.getCountryCode()).equals("AR") &&
-        getNumberType(number) == PhoneNumberType.MOBILE) {
-      // Argentinian mobile numbers, when formatted in the international format, are in the form of
-      // +54 9 NDC XXXX.... As a result, we take the length of the third group (NDC) and add 1 for
-      // the digit 9, which also forms part of the national significant number.
-      //
-      // TODO: Investigate the possibility of better modeling the metadata to make it
-      // easier to obtain the NDC.
-      return numberGroups[3].length() + 1;
+    if (getNumberType(number) == PhoneNumberType.MOBILE) {
+      // For example Argentinian mobile numbers, when formatted in the international format, are in
+      // the form of +54 9 NDC XXXX.... As a result, we take the length of the third group (NDC) and
+      // add the length of the second group (which is the mobile token), which also forms part of
+      // the national significant number. This assumes that the mobile token is always formatted
+      // separately from the rest of the phone number.
+      String mobileToken = getCountryMobileToken(number.getCountryCode());
+      if (!mobileToken.equals("")) {
+        return numberGroups[2].length() + numberGroups[3].length();
+      }
     }
     return numberGroups[2].length();
   }
 
   /**
+   * Returns the mobile token for the provided country calling code if it has one, otherwise
+   * returns an empty string. A mobile token is a number inserted before the area code when dialing
+   * a mobile number from that country from abroad.
+   *
+   * @param countryCallingCode  the country calling code for which we want the mobile token
+   * @return  the mobile token, as a string, for the given country calling code
+   */
+  public static String getCountryMobileToken(int countryCallingCode) {
+    if (MOBILE_TOKEN_MAPPINGS.containsKey(countryCallingCode)) {
+      return MOBILE_TOKEN_MAPPINGS.get(countryCallingCode);
+    }
+    return "";
+  }
+
+  /**
    * Normalizes a string of characters representing a phone number by replacing all characters found
    * in the accompanying map with the values therein, and stripping all other characters if
    * removeNonMatches is true.
index 701558f..3207709 100644 (file)
@@ -292,6 +292,15 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase {
     assertEquals(4, phoneUtil.getLengthOfNationalDestinationCode(INTERNATIONAL_TOLL_FREE));
   }
 
+  public void testGetCountryMobileToken() {
+    assertEquals("1", PhoneNumberUtil.getCountryMobileToken(phoneUtil.getCountryCodeForRegion(
+        RegionCode.MX)));
+
+    // Country calling code for Sweden, which has no mobile token.
+    assertEquals("", PhoneNumberUtil.getCountryMobileToken(phoneUtil.getCountryCodeForRegion(
+        RegionCode.SE)));
+  }
+
   public void testGetNationalSignificantNumber() {
     assertEquals("6502530000", phoneUtil.getNationalSignificantNumber(US_NUMBER));
 
index 1dfa5c4..66e1a63 100644 (file)
@@ -48,6 +48,7 @@ final class RegionCode {
   static final String PG = "PG";
   static final String PL = "PL";
   static final String RE = "RE";
+  static final String SE = "SE";
   static final String SG = "SG";
   static final String US = "US";
   static final String YT = "YT";
index c203516..6fcdb38 100644 (file)
@@ -1,3 +1,7 @@
+Sep 19, 2013: libphonenumber-5.8.2
+* Code changes:
+  - New method in the PhoneNumberUtil API - getCountryMobileToken.
+
 Sep 19, 2013: libphonenumber-5.8.1
 * Metadata changes:
  - New alternate formatting data for country calling code(s): 675