@Test public void testTranslation() { assertNull(CountryCodes.translate("02012345678", "ZZ", "GB")); assertNull(CountryCodes.translate("02012345678", "GB", "ZZ")); assertEquals("02012345678", CountryCodes.translate("2012345678", "GB", "GB")); assertEquals("02012345678", CountryCodes.translate("02012345678", "GB", "GB")); assertEquals("02012345678", CountryCodes.translate("+442012345678", "GB", "GB")); assertEquals("02012345678", CountryCodes.translate("00442012345678", "GB", "GB")); assertEquals("8**10442012345678", CountryCodes.translate("2012345678", "RU", "GB")); assertEquals("8**10442012345678", CountryCodes.translate("02012345678", "RU", "GB")); assertEquals("8**10442012345678", CountryCodes.translate("+442012345678", "RU", "GB")); assertEquals("8**10442012345678", CountryCodes.translate("00442012345678", "RU", "GB")); assertEquals("765432", CountryCodes.translate("765432", "AD", "AD")); assertEquals("765432", CountryCodes.translate("+376765432", "AD", "AD")); assertEquals("765432", CountryCodes.translate("00376765432", "AD", "AD")); assertEquals("00376765432", CountryCodes.translate("765432", "GB", "AD")); assertEquals("00376765432", CountryCodes.translate("+376765432", "GB", "AD")); assertEquals("00376765432", CountryCodes.translate("00376765432", "GB", "AD")); }
static String translate(String number, String callerIso, String calleeIso) { Country from = COUNTRY_MAP.get(callerIso); Country to = COUNTRY_MAP.get(calleeIso); if (from == null || to == null) return null; String plusCountryCode = "+" + to.countryCode; String iddCountryCode = to.idd + to.countryCode; if (number.startsWith(plusCountryCode)) number = number.substring(plusCountryCode.length()); else if (number.startsWith(iddCountryCode)) number = number.substring(iddCountryCode.length()); else if (number.startsWith(to.ndd)) number = number.substring(to.ndd.length()); if (from == to) return from.ndd + number; return from.idd + to.countryCode + number; }
CountryCodes { static String translate(String number, String callerIso, String calleeIso) { Country from = COUNTRY_MAP.get(callerIso); Country to = COUNTRY_MAP.get(calleeIso); if (from == null || to == null) return null; String plusCountryCode = "+" + to.countryCode; String iddCountryCode = to.idd + to.countryCode; if (number.startsWith(plusCountryCode)) number = number.substring(plusCountryCode.length()); else if (number.startsWith(iddCountryCode)) number = number.substring(iddCountryCode.length()); else if (number.startsWith(to.ndd)) number = number.substring(to.ndd.length()); if (from == to) return from.ndd + number; return from.idd + to.countryCode + number; } }
CountryCodes { static String translate(String number, String callerIso, String calleeIso) { Country from = COUNTRY_MAP.get(callerIso); Country to = COUNTRY_MAP.get(calleeIso); if (from == null || to == null) return null; String plusCountryCode = "+" + to.countryCode; String iddCountryCode = to.idd + to.countryCode; if (number.startsWith(plusCountryCode)) number = number.substring(plusCountryCode.length()); else if (number.startsWith(iddCountryCode)) number = number.substring(iddCountryCode.length()); else if (number.startsWith(to.ndd)) number = number.substring(to.ndd.length()); if (from == to) return from.ndd + number; return from.idd + to.countryCode + number; }  }
CountryCodes { static String translate(String number, String callerIso, String calleeIso) { Country from = COUNTRY_MAP.get(callerIso); Country to = COUNTRY_MAP.get(calleeIso); if (from == null || to == null) return null; String plusCountryCode = "+" + to.countryCode; String iddCountryCode = to.idd + to.countryCode; if (number.startsWith(plusCountryCode)) number = number.substring(plusCountryCode.length()); else if (number.startsWith(iddCountryCode)) number = number.substring(iddCountryCode.length()); else if (number.startsWith(to.ndd)) number = number.substring(to.ndd.length()); if (from == to) return from.ndd + number; return from.idd + to.countryCode + number; }   }
CountryCodes { static String translate(String number, String callerIso, String calleeIso) { Country from = COUNTRY_MAP.get(callerIso); Country to = COUNTRY_MAP.get(calleeIso); if (from == null || to == null) return null; String plusCountryCode = "+" + to.countryCode; String iddCountryCode = to.idd + to.countryCode; if (number.startsWith(plusCountryCode)) number = number.substring(plusCountryCode.length()); else if (number.startsWith(iddCountryCode)) number = number.substring(iddCountryCode.length()); else if (number.startsWith(to.ndd)) number = number.substring(to.ndd.length()); if (from == to) return from.ndd + number; return from.idd + to.countryCode + number; }    }