Browse Source

Changing the offline geocoder to not return any country at all if the number could belong to multiple countries.

pull/567/head
Lara Scheidegger 11 years ago
committed by Mihaela Rosca
parent
commit
af007418cf
5 changed files with 43 additions and 5 deletions
  1. +20
    -3
      java/geocoder/src/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoder.java
  2. +9
    -0
      java/geocoder/test/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoderTest.java
  3. BIN
      java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CC
  4. BIN
      java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CX
  5. +14
    -2
      resources/PhoneNumberMetadataForTesting.xml

+ 20
- 3
java/geocoder/src/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoder.java View File

@ -22,6 +22,7 @@ import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberType;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
import com.google.i18n.phonenumbers.prefixmapper.PrefixFileReader;
import java.util.List;
import java.util.Locale;
/**
@ -60,11 +61,27 @@ public class PhoneNumberOfflineGeocoder {
/**
* Returns the customary display name in the given language for the given territory the phone
* number is from.
* number is from. If it could be from many territories, nothing is returned.
*/
private String getCountryNameForNumber(PhoneNumber number, Locale language) {
String regionCode = phoneUtil.getRegionCodeForNumber(number);
return getRegionDisplayName(regionCode, language);
List<String> regionCodes =
phoneUtil.getRegionCodesForCountryCode(number.getCountryCode());
if (regionCodes.size() == 1) {
return getRegionDisplayName(regionCodes.get(0), language);
} else {
String regionWhereNumberIsValid = "ZZ";
for (String regionCode : regionCodes) {
if (phoneUtil.isValidNumberForRegion(number, regionCode)) {
if (!regionWhereNumberIsValid.equals("ZZ")) {
// If we can't assign the phone number as definitely belonging to only one territory,
// then we return nothing.
return "";
}
regionWhereNumberIsValid = regionCode;
}
}
return getRegionDisplayName(regionWhereNumberIsValid, language);
}
}
/**


+ 9
- 0
java/geocoder/test/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoderTest.java View File

@ -51,6 +51,8 @@ public class PhoneNumberOfflineGeocoderTest extends TestCase {
new PhoneNumber().setCountryCode(1).setNationalNumber(6174240000L);
private static final PhoneNumber US_INVALID_NUMBER =
new PhoneNumber().setCountryCode(1).setNationalNumber(123456789L);
private static final PhoneNumber NANPA_TOLL_FREE =
new PhoneNumber().setCountryCode(1).setNationalNumber(8002431234L);
private static final PhoneNumber BS_NUMBER1 =
new PhoneNumber().setCountryCode(1).setNationalNumber(2423651234L);
private static final PhoneNumber AU_NUMBER =
@ -84,6 +86,13 @@ public class PhoneNumberOfflineGeocoderTest extends TestCase {
geocoder.getDescriptionForNumber(US_NUMBER4, new Locale("en", "US")));
}
public void testGetDescriptionForNumberBelongingToMultipleCountriesIsEmpty() {
// Test that nothing is returned when the number passed in is valid but not
// covered by the geocoding data file and belongs to multiple countries
assertEquals("",
geocoder.getDescriptionForNumber(NANPA_TOLL_FREE, new Locale("en", "US")));
}
public void testGetDescriptionForNumber_en_US() {
assertEquals("CA",
geocoder.getDescriptionForNumber(US_NUMBER1, new Locale("en", "US")));


BIN
java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CC View File


BIN
java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CX View File


+ 14
- 2
resources/PhoneNumberMetadataForTesting.xml View File

@ -223,8 +223,14 @@
<!-- Cocos Islands -->
<!-- Country calling code shared with Australia. -->
<!-- This country is used to test ShortNumberInfo, so at least the country calling code must be
recognised by the library. -->
recognised by the library. However, we don't want all numbers to be considered valid for
it, so we set at least a general description that allows our test geocoding numbers to be
valid only for Australia. -->
<territory id="CC" countryCode="61">
<generalDesc>
<nationalNumberPattern>5\d{3,14}</nationalNumberPattern>
<possibleNumberPattern>\d{2,14}</possibleNumberPattern>
</generalDesc>
</territory>
<!-- China -->
@ -249,8 +255,14 @@
<!-- Christmas Islands -->
<!-- Country calling code shared with Australia. -->
<!-- This country is used to test ShortNumberInfo, so at least the country calling code must be
recognised by the library. -->
recognised by the library. However, we don't want all numbers to be considered valid for
it, so we set at least a general description that allows our test geocoding numbers to be
valid only for Australia. -->
<territory id="CX" countryCode="61">
<generalDesc>
<nationalNumberPattern>5\d{3,14}</nationalNumberPattern>
<possibleNumberPattern>\d{2,14}</possibleNumberPattern>
</generalDesc>
</territory>
<!-- Germany -->


Loading…
Cancel
Save