Changing the offline geocoder to not return any country at all if the number could...
authorLara Scheidegger <lararennie@google.com>
Thu, 11 Sep 2014 13:11:28 +0000 (13:11 +0000)
committerYoungjae Shin <yj99.shin@samsung.com>
Tue, 9 Jun 2015 11:43:19 +0000 (20:43 +0900)
java/geocoder/src/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoder.java
java/geocoder/test/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoderTest.java
java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CC
java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CX
resources/PhoneNumberMetadataForTesting.xml

index c3247c9..32bc681 100644 (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);
+    }
   }
 
   /**
index 5e7ed0c..ed9bd3b 100644 (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")));
index 97fa27e..f715e55 100644 (file)
Binary files a/java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CC and b/java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CC differ
index ad6bff5..b6984d8 100644 (file)
Binary files a/java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CX and b/java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CX differ
index 280373d..ae670ca 100644 (file)
     <!-- 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 -->
     <!-- 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 -->