String formattedNumber;
// Clear the extension, as that part cannot normally be dialed together with the main number.
- number.clearExtension();
- PhoneNumberType numberType = getNumberType(number);
- if ((regionCode == "CO") && (regionCallingFrom == "CO") &&
+ PhoneNumber numberNoExt = new PhoneNumber().mergeFrom(number).clearExtension();
+ PhoneNumberType numberType = getNumberType(numberNoExt);
+ if ((regionCode.equals("CO")) && (regionCallingFrom.equals("CO")) &&
(numberType == PhoneNumberType.FIXED_LINE)) {
formattedNumber =
- formatNationalNumberWithCarrierCode(number, COLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX);
- } else if ((regionCode == "BR") && (regionCallingFrom == "BR") &&
+ formatNationalNumberWithCarrierCode(numberNoExt, COLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX);
+ } else if ((regionCode.equals("BR")) && (regionCallingFrom.equals("BR")) &&
((numberType == PhoneNumberType.FIXED_LINE) || (numberType == PhoneNumberType.MOBILE) ||
(numberType == PhoneNumberType.FIXED_LINE_OR_MOBILE))) {
- formattedNumber = number.hasPreferredDomesticCarrierCode()
- ? formatNationalNumberWithPreferredCarrierCode(number, "")
+ formattedNumber = numberNoExt.hasPreferredDomesticCarrierCode()
+ ? formatNationalNumberWithPreferredCarrierCode(numberNoExt, "")
// Brazilian fixed line and mobile numbers need to be dialed with a carrier code when
// called within Brazil. Without that, most of the carriers won't connect the call.
// Because of that, we return an empty string here.
: "";
- } else if (canBeInternationallyDialled(number)) {
- return withFormatting ? format(number, PhoneNumberFormat.INTERNATIONAL)
- : format(number, PhoneNumberFormat.E164);
+ } else if (canBeInternationallyDialled(numberNoExt)) {
+ return withFormatting ? format(numberNoExt, PhoneNumberFormat.INTERNATIONAL)
+ : format(numberNoExt, PhoneNumberFormat.E164);
} else {
- formattedNumber = (regionCallingFrom == regionCode)
- ? format(number, PhoneNumberFormat.NATIONAL) : "";
+ formattedNumber = (regionCallingFrom.equals(regionCode))
+ ? format(numberNoExt, PhoneNumberFormat.NATIONAL) : "";
}
return withFormatting ? formattedNumber : normalizeDigitsOnly(formattedNumber);
}
*/
public class ShortNumberUtil {
- private static PhoneNumberUtil phoneUtil;
+ private final PhoneNumberUtil phoneUtil;
public ShortNumberUtil() {
phoneUtil = PhoneNumberUtil.getInstance();
PhoneNumberDesc emergencyNumberDesc = phoneUtil.getMetadataForRegion(regionCode).getEmergency();
Pattern emergencyNumberPattern =
Pattern.compile(emergencyNumberDesc.getNationalNumberPattern());
- if (regionCode == "BR") {
+ if (regionCode.equals("BR")) {
// This is to prevent Brazilian local numbers which start with 911 being incorrectly
// classified as emergency numbers. In Brazil, it is impossible to append additional digits to
// an emergency number to dial the number.
};
public void testMatchesWithStrictGroupingLeniency() throws Exception {
- int noMatchFoundCount = 0;
- int wrongMatchFoundCount = 0;
List<NumberTest> testCases = new ArrayList<NumberTest>();
testCases.addAll(Arrays.asList(STRICT_GROUPING_CASES));
testCases.addAll(Arrays.asList(EXACT_GROUPING_CASES));
return phoneUtil.findNumbers(text, defaultCountry, leniency, Long.MAX_VALUE).iterator();
}
- /**
- * Returns true if there were no matches found.
- */
- private boolean hasNoMatches(Iterator<PhoneNumberMatch> iterator) {
- return !iterator.hasNext();
- }
-
private boolean hasNoMatches(Iterable<PhoneNumberMatch> iterable) {
return !iterable.iterator().hasNext();
}
for (Map.Entry<Integer, String> mapping : mappings.entrySet()) {
String prefix = String.valueOf(mapping.getKey());
File targetFile = null;
- int correspondingAreaCode = -1;
for (File outputBinaryFile : outputBinaryFiles) {
String outputBinaryFilePrefix =
getPhonePrefixLanguagePairFromFilename(outputBinaryFile.getName()).prefix;
if (prefix.startsWith(outputBinaryFilePrefix)) {
targetFile = outputBinaryFile;
- correspondingAreaCode = Integer.parseInt(outputBinaryFilePrefix);
break;
}
}
* Gets the English data text file path corresponding to the provided one.
*/
// @VisibleForTesting
- static String getEnglishDataPath(File inputTextFile) {
- return LANGUAGE_IN_FILE_PATH_PATTERN.matcher(inputTextFile.getAbsolutePath()).replaceFirst(
- "$1en$2");
+ static String getEnglishDataPath(String inputTextFileName) {
+ return LANGUAGE_IN_FILE_PATH_PATTERN.matcher(inputTextFileName).replaceFirst("$1en$2");
}
/**
*/
private void makeDataFallbackToEnglish(File inputTextFile, SortedMap<Integer, String> mappings)
throws IOException {
- File englishTextFile = new File(getEnglishDataPath(inputTextFile));
+ File englishTextFile = new File(getEnglishDataPath(inputTextFile.getAbsolutePath()));
if (inputTextFile.getAbsolutePath().equals(englishTextFile.getAbsolutePath()) ||
!englishTextFile.exists()) {
return;
}
public void testGetEnglishDataPath() {
- assertEquals("/path/en/33.txt",
- GenerateAreaCodeData.getEnglishDataPath(new File("/path/fr/33.txt")));
+ assertEquals("/path/en/33.txt", GenerateAreaCodeData.getEnglishDataPath("/path/fr/33.txt"));
}
public void testHasOverlap() {