Add an alternate number form generator for Gambia

This commit is contained in:
Jon Chambers authored and Jon Chambers committed 2026-09-02 16:55:49 -04:00
1 parent 9b5ad238cf
commit 2f712b3f93
4 files changed
+142 -2

No files matched your search

@@ -0,0 +1,74 @@
/*
* Copyright 2026 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.textsecuregcm.util;
import com.google.common.annotations.VisibleForTesting;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
/// Generates alternate forms of Gambian phone numbers as [specified by
/// PURA](https://pura.gm/public-notice-migration-of-the-national-mobile-numbering-plan-from-7-digit-to-9-digit-format/).
class GambiaAlternateFormGenerator implements AlternatePhoneNumberFormGenerator {
// Please see https://pura.gm/wp-content/uploads/2026/07/PURA_Website_NNP_Migration_Public_Notice.pdf for number
// format details.
enum Carrier {
QCELL("83", Pattern.compile("^(3\\d{6})|(5[012345789]\\d{5})$")),
COMIUM("86", Pattern.compile("^(6\\d{6})|(8[4567]\\d{5})$")),
AFRICELL("87", Pattern.compile("^([27]\\d{6})|(4[015]\\d{5})$"));
private final String newPrefix;
private final Pattern legacyNumberPattern;
Carrier(final String newPrefix, final Pattern legacyNumberPattern) {
this.newPrefix = newPrefix;
this.legacyNumberPattern = legacyNumberPattern;
}
}
@Override
public List<String> getAlternateForms(final String number) throws NumberParseException {
final Phonenumber.PhoneNumber phoneNumber = PhoneNumberUtil.getInstance().parse(number, null);
final String nationalSignificantNumber = PhoneNumberUtil.getInstance().getNationalSignificantNumber(phoneNumber);
final Optional<String> maybeAlternateForm = getCarrier(nationalSignificantNumber)
.map(carrier -> {
if (nationalSignificantNumber.length() == 7) {
// This is a legacy number, and we need to add the carrier's prefix to get the updated form
return "+220" + carrier.newPrefix + nationalSignificantNumber;
} else {
// This is a new-style number, and we need to strip the carrier's prefix to get the legacy form
return "+220" + nationalSignificantNumber.substring(carrier.newPrefix.length());
}
});
return maybeAlternateForm
.map(alternateForm -> List.of(number, alternateForm))
.orElseGet(() -> List.of(number));
}
@VisibleForTesting
static Optional<Carrier> getCarrier(final String nationalSignificantNumber) {
if (nationalSignificantNumber.length() == 9) {
// This might be a new-style number; see if we can find a carrier by prefix
return Arrays.stream(Carrier.values())
.filter(carrier -> nationalSignificantNumber.startsWith(carrier.newPrefix))
.findFirst();
} else if (nationalSignificantNumber.length() == 7) {
// This might be an old-style number belonging to a carrier with a new prefix; match by pattern if we can
return Arrays.stream(Carrier.values())
.filter(carrier -> carrier.legacyNumberPattern.matcher(nationalSignificantNumber).matches())
.findFirst();
}
return Optional.empty();
}
}
@@ -41,7 +41,8 @@ public class Util {
// region code for some phone numbers
private static final Map<String, AlternatePhoneNumberFormGenerator> ALTERNATE_FORM_GENERATORS_BY_REGION =
new HashMap<>(Map.of(
"BJ", new BeninAlternateFormGenerator()
"BJ", new BeninAlternateFormGenerator(),
"GM", new GambiaAlternateFormGenerator()
));
// Use `CompletableFuture#thenApply(ASYNC_EMPTY_RESPONSE) to convert futures to
@@ -0,0 +1,63 @@
/*
* Copyright 2026 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.textsecuregcm.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.google.i18n.phonenumbers.NumberParseException;
import java.util.List;
import java.util.Optional;
import javax.annotation.Nullable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
class GambiaAlternateFormGeneratorTest {
@ParameterizedTest
@MethodSource
void getAlternateForms(final String e164, final List<String> alternateForms) throws NumberParseException {
assertEquals(alternateForms, new GambiaAlternateFormGenerator().getAlternateForms(e164));
}
private static List<Arguments> getAlternateForms() {
return List.of(
Arguments.argumentSet("Legacy Qcell number", "+2203123456", List.of("+2203123456", "+220833123456")),
Arguments.argumentSet("Legacy Comium number", "+2206123456", List.of("+2206123456", "+220866123456")),
Arguments.argumentSet("Legacy Africell number", "+2202123456", List.of("+2202123456", "+220872123456")),
Arguments.argumentSet("Legacy Gamcel number", "+2209123456", List.of("+2209123456")),
Arguments.argumentSet("New-style Qcell number", "+220833123456", List.of("+220833123456", "+2203123456")),
Arguments.argumentSet("New-style Comium number", "+220866123456", List.of("+220866123456", "+2206123456")),
Arguments.argumentSet("New-style Africell number", "+220872123456", List.of("+220872123456", "+2202123456"))
);
}
@ParameterizedTest
@CsvSource({
"3123456,QCELL",
"5012345,QCELL",
"5612345,",
"835700011,QCELL",
"2123456,AFRICELL",
"7123456,AFRICELL",
"4012345,AFRICELL",
"4212345,",
"872123456,AFRICELL",
"6123456,COMIUM",
"8712345,COMIUM",
"8812345,",
"868712345,COMIUM",
"881234567,"
})
void getCarrier(final String nationalSignificantNumber,
@Nullable final GambiaAlternateFormGenerator.Carrier expectedCarrier) {
assertEquals(Optional.ofNullable(expectedCarrier),
GambiaAlternateFormGenerator.getCarrier(nationalSignificantNumber));
}
}
@@ -44,7 +44,9 @@ class UtilTest {
Arguments.of(usE164, List.of(usE164)),
Arguments.of(NEW_FORMAT_BENIN_E164_STRING, List.of(NEW_FORMAT_BENIN_E164_STRING, OLD_FORMAT_BENIN_E164_STRING)),
Arguments.of(OLD_FORMAT_BENIN_E164_STRING, List.of(OLD_FORMAT_BENIN_E164_STRING, NEW_FORMAT_BENIN_E164_STRING)),
Arguments.of(TEN_DIGIT_NATIONAL_SIGNIFICANT_BENIN_E164, List.of(TEN_DIGIT_NATIONAL_SIGNIFICANT_BENIN_E164))
Arguments.of(TEN_DIGIT_NATIONAL_SIGNIFICANT_BENIN_E164, List.of(TEN_DIGIT_NATIONAL_SIGNIFICANT_BENIN_E164)),
// Please see `GambiaAlternateFormGeneratorTest` for more comprehensive coverage
Arguments.argumentSet("Gambia alternate forms", "+220872123456", List.of("+220872123456", "+2202123456"))
);
}