Browse Source

JAVA: updated libphonenumber API

pull/567/head
Cecilia Roes 12 years ago
committed by Mihaela Rosca
parent
commit
fcfaefcdd3
4 changed files with 49 additions and 9 deletions
  1. +35
    -9
      java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java
  2. +9
    -0
      java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java
  3. +1
    -0
      java/libphonenumber/test/com/google/i18n/phonenumbers/RegionCode.java
  4. +4
    -0
      java/release_notes.txt

+ 35
- 9
java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java View 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,19 +888,35 @@ 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


+ 9
- 0
java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java View 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));


+ 1
- 0
java/libphonenumber/test/com/google/i18n/phonenumbers/RegionCode.java View 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";


+ 4
- 0
java/release_notes.txt View 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


Loading…
Cancel
Save