Skip to content

Commit

Permalink
Refactoring MetadataManager (google#2732)
Browse files Browse the repository at this point in the history
* Added new classes with behavior from MetadataManager & MetadataSource

* Replaced MetadataManager usages with new classes

* New PrefixDataIOHandler which packs data into a jar

* Added mockito-core jar to lib folder

* Added mockito-all jar to lib folder and updated tests to not use junit4 annotations

* Fixed prefix generation entry points

* Specific exception for missing metadata cases

* Small change in checking missing metadata cases
  • Loading branch information
tvislavski authored Apr 7, 2022
1 parent 7b69a59 commit 5a17acd
Show file tree
Hide file tree
Showing 52 changed files with 1,941 additions and 734 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

package com.google.i18n.phonenumbers;

import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberType;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
import com.google.i18n.phonenumbers.metadata.DefaultMetadataDependenciesProvider;
import com.google.i18n.phonenumbers.prefixmapper.PrefixFileReader;

import java.util.Locale;

/**
Expand All @@ -30,9 +29,7 @@
*/
public class PhoneNumberToCarrierMapper {
private static PhoneNumberToCarrierMapper instance = null;
private static final String MAPPING_DATA_DIRECTORY =
"/com/google/i18n/phonenumbers/carrier/data/";
private PrefixFileReader prefixFileReader = null;
private final PrefixFileReader prefixFileReader;

private final PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();

Expand All @@ -51,7 +48,8 @@ public class PhoneNumberToCarrierMapper {
*/
public static synchronized PhoneNumberToCarrierMapper getInstance() {
if (instance == null) {
instance = new PhoneNumberToCarrierMapper(MAPPING_DATA_DIRECTORY);
instance = new PhoneNumberToCarrierMapper(DefaultMetadataDependenciesProvider.getInstance()
.getCarrierDataDirectory());
}
return instance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberType;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
import com.google.i18n.phonenumbers.metadata.DefaultMetadataDependenciesProvider;
import com.google.i18n.phonenumbers.prefixmapper.PrefixFileReader;

import java.util.List;
Expand All @@ -32,9 +33,7 @@
*/
public class PhoneNumberOfflineGeocoder {
private static PhoneNumberOfflineGeocoder instance = null;
private static final String MAPPING_DATA_DIRECTORY =
"/com/google/i18n/phonenumbers/geocoding/data/";
private PrefixFileReader prefixFileReader = null;
private final PrefixFileReader prefixFileReader;

private final PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();

Expand All @@ -54,7 +53,8 @@ public class PhoneNumberOfflineGeocoder {
*/
public static synchronized PhoneNumberOfflineGeocoder getInstance() {
if (instance == null) {
instance = new PhoneNumberOfflineGeocoder(MAPPING_DATA_DIRECTORY);
instance = new PhoneNumberOfflineGeocoder(DefaultMetadataDependenciesProvider.getInstance()
.getGeocodingDataDirectory());
}
return instance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

package com.google.i18n.phonenumbers.prefixmapper;

import com.google.i18n.phonenumbers.MetadataLoader;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;

import com.google.i18n.phonenumbers.metadata.DefaultMetadataDependenciesProvider;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
Expand All @@ -40,17 +42,17 @@ public class PrefixFileReader {
private MappingFileProvider mappingFileProvider = new MappingFileProvider();
// A mapping from countryCallingCode_lang to the corresponding phone prefix map that has been
// loaded.
private Map<String, PhonePrefixMap> availablePhonePrefixMaps =
new HashMap<String, PhonePrefixMap>();
private Map<String, PhonePrefixMap> availablePhonePrefixMaps = new HashMap<>();
private final MetadataLoader metadataLoader;

public PrefixFileReader(String phonePrefixDataDirectory) {
this.phonePrefixDataDirectory = phonePrefixDataDirectory;
this.metadataLoader = DefaultMetadataDependenciesProvider.getInstance().getMetadataLoader();
loadMappingFileProvider();
}

private void loadMappingFileProvider() {
InputStream source =
PrefixFileReader.class.getResourceAsStream(phonePrefixDataDirectory + "config");
InputStream source = metadataLoader.loadMetadata(phonePrefixDataDirectory + "config");
ObjectInputStream in = null;
try {
in = new ObjectInputStream(source);
Expand All @@ -75,8 +77,7 @@ private PhonePrefixMap getPhonePrefixDescriptions(
}

private void loadPhonePrefixMapFromFile(String fileName) {
InputStream source =
PrefixFileReader.class.getResourceAsStream(phonePrefixDataDirectory + fileName);
InputStream source = metadataLoader.loadMetadata(phonePrefixDataDirectory + fileName);
ObjectInputStream in = null;
try {
in = new ObjectInputStream(source);
Expand Down
Binary file added java/lib/mockito-all-1.10.19.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class CountryCodeToRegionCodeMap {
// country/region represented by that country code. In the case of multiple
// countries sharing a calling code, such as the NANPA countries, the one
// indicated with "isMainCountryForCode" in the metadata should be first.
static Map<Integer, List<String>> getCountryCodeToRegionCodeMap() {
public static Map<Integer, List<String>> getCountryCodeToRegionCodeMap() {
// The capacity is set to 286 as there are 215 different entries,
// and this offers a load factor of roughly 0.75.
Map<Integer, List<String>> countryCodeToRegionCodeMap =
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.google.i18n.phonenumbers;

/** Exception class for cases when expected metadata cannot be found. */
public final class MissingMetadataException extends IllegalStateException {

public MissingMetadataException(String message) {
super(message);
}
}
Loading

0 comments on commit 5a17acd

Please sign in to comment.