Accept phone numbers starting with +.

This commit is contained in:
akwizgran
2012-12-15 22:24:51 +00:00
parent 6dd17a22eb
commit b090a12b7f
2 changed files with 50 additions and 21 deletions

View File

@@ -19,7 +19,7 @@ class CountryCodes {
new Country("AR", "Argentina", "54", "00", "0"),
new Country("AS", "American Samoa", "1", "011", "1"),
new Country("AT", "Austria", "43", "00", "0"),
new Country("AU", "Australia", "61", "00", ""),
new Country("AU", "Australia", "61", "0011", "0"),
new Country("AW", "Aruba", "297", "00", ""),
new Country("AZ", "Azerbaijan", "994", "00", "8"),
new Country("BA", "Bosnia and Herzegovina", "387", "00", "0"),
@@ -43,9 +43,9 @@ class CountryCodes {
new Country("BZ", "Belize", "501", "00", "0"),
new Country("CA", "Canada", "1", "011", "1"),
new Country("CC", "Cocos (Keeling) Islands", "61", "0011", "0"),
new Country("CD", "Congo", "243", "00", ""),
new Country("CD", "Congo (Republic)", "243", "00", ""),
new Country("CF", "Central African Republic", "236", "00", ""),
new Country("CG", "Congo", "242", "00", ""),
new Country("CG", "Congo (Democratic Republic)", "242", "00", "0"),
new Country("CH", "Switzerland", "41", "00", "0"),
new Country("CI", "Cote D'Ivoire", "225", "00", "0"),
new Country("CK", "Cook Islands", "682", "00", "00"),
@@ -257,12 +257,14 @@ class CountryCodes {
static String translate(String number, String fromIso, String toIso) {
Country from = COUNTRY_MAP.get(fromIso), to = COUNTRY_MAP.get(toIso);
if(from == null || to == null) return null;
// Strip the IDD prefix and country code from the number if present
// Strip any prefixes and country codes from the number
String plusCountryCode = "+" + to.countryCode;
String iddCountryCode = to.idd + to.countryCode;
if(number.startsWith(iddCountryCode))
if(number.startsWith(plusCountryCode))
number = number.substring(plusCountryCode.length());
else if(number.startsWith(iddCountryCode))
number = number.substring(iddCountryCode.length());
// Strip the NDD prefix from the number if present
if(number.startsWith(to.ndd))
else if(number.startsWith(to.ndd))
number = number.substring(to.ndd.length());
if(from == to) return from.ndd + number; // National
return from.idd + to.countryCode + number; // International

View File

@@ -8,27 +8,54 @@ public class CountryCodesTest extends BriarTestCase {
@Test
public void testTranslation() {
// Unknown country for caller
// Unrecognised country for caller
assertNull(CountryCodes.translate("02012345678", "ZZ", "GB"));
// Unknown country for callee
// Unrecognised country for callee
assertNull(CountryCodes.translate("02012345678", "GB", "ZZ"));
// GB to GB, callee has included NDD prefix
assertEquals("02012345678",
CountryCodes.translate("02012345678", "GB", "GB"));
// GB to GB, callee has included IDD prefix and country code
assertEquals("02012345678",
CountryCodes.translate("00442012345678", "GB", "GB"));
// GB to GB, callee has not included a prefix
assertEquals("02012345678",
CountryCodes.translate("2012345678", "GB", "GB"));
// Russia to GB, callee has included NDD prefix
assertEquals("8**10442012345678",
CountryCodes.translate("02012345678", "RU", "GB"));
// Russia to GB, callee has included IDD prefix and country code
assertEquals("8**10442012345678",
CountryCodes.translate("00442012345678", "RU", "GB"));
// GB to GB, callee has included NDD prefix
assertEquals("02012345678",
CountryCodes.translate("02012345678", "GB", "GB"));
// GB to GB, callee has included plus sign and country code
assertEquals("02012345678",
CountryCodes.translate("+442012345678", "GB", "GB"));
// GB to GB, callee has included IDD prefix and country code
assertEquals("02012345678",
CountryCodes.translate("00442012345678", "GB", "GB"));
// Russia to GB, callee has not included a prefix
assertEquals("8**10442012345678",
CountryCodes.translate("2012345678", "RU", "GB"));
// Russia to GB, callee has included NDD prefix
assertEquals("8**10442012345678",
CountryCodes.translate("02012345678", "RU", "GB"));
// Russia to GB, callee has included plus sign and country code
assertEquals("8**10442012345678",
CountryCodes.translate("+442012345678", "RU", "GB"));
// Russia to GB, callee has included IDD prefix and country code
assertEquals("8**10442012345678",
CountryCodes.translate("00442012345678", "RU", "GB"));
// Andorra to Andorra (no NDD), callee has not included a prefix
assertEquals("765432", CountryCodes.translate("765432", "AD", "AD"));
// Andorra to Andorra, callee has included plus sign and country code
assertEquals("765432",
CountryCodes.translate("+376765432", "AD", "AD"));
// Andorra to Andorra, callee has included IDD and country code
assertEquals("765432",
CountryCodes.translate("00376765432", "AD", "AD"));
// GB to Andorra (no NDD), callee has not included a prefix
assertEquals("00376765432",
CountryCodes.translate("765432", "GB", "AD"));
// GB to Andorra, callee has included plus sign and country code
assertEquals("00376765432",
CountryCodes.translate("+376765432", "GB", "AD"));
// GB to Andorra, callee has included IDD and country code
assertEquals("00376765432",
CountryCodes.translate("00376765432", "GB", "AD"));
}
}