Browse Source

Use instance variables rather than passing them through methods in MultiFileMetadataSourceImpl.

pull/844/head
Keghani Kouzoujian 10 years ago
parent
commit
e9b82df4c2
3 changed files with 11 additions and 15 deletions
  1. +6
    -8
      java/libphonenumber/src/com/google/i18n/phonenumbers/MultiFileMetadataSourceImpl.java
  2. +5
    -7
      java/libphonenumber/test/com/google/i18n/phonenumbers/MultiFileMetadataSourceImplTest.java
  3. BIN
      tools/java/java-build/target/java-build-1.0-SNAPSHOT-jar-with-dependencies.jar

+ 6
- 8
java/libphonenumber/src/com/google/i18n/phonenumbers/MultiFileMetadataSourceImpl.java View File

@ -55,14 +55,14 @@ final class MultiFileMetadataSourceImpl implements MetadataSource {
Collections.synchronizedMap(new HashMap<Integer, PhoneMetadata>());
// The prefix of the metadata files from which region data is loaded.
private final String currentFilePrefix;
private final String filePrefix;
// The metadata loader used to inject alternative metadata sources.
private final MetadataLoader metadataLoader;
// It is assumed that metadataLoader is not null.
public MultiFileMetadataSourceImpl(String currentFilePrefix, MetadataLoader metadataLoader) {
this.currentFilePrefix = currentFilePrefix;
public MultiFileMetadataSourceImpl(String filePrefix, MetadataLoader metadataLoader) {
this.filePrefix = filePrefix;
this.metadataLoader = metadataLoader;
}
@ -77,7 +77,7 @@ final class MultiFileMetadataSourceImpl implements MetadataSource {
if (!regionToMetadataMap.containsKey(regionCode)) {
// The regionCode here will be valid and won't be '001', so we don't need to worry about
// what to pass in for the country calling code.
loadMetadataFromFile(currentFilePrefix, regionCode, 0, metadataLoader);
loadMetadataFromFile(regionCode, 0);
}
}
return regionToMetadataMap.get(regionCode);
@ -87,16 +87,14 @@ final class MultiFileMetadataSourceImpl implements MetadataSource {
public PhoneMetadata getMetadataForNonGeographicalRegion(int countryCallingCode) {
synchronized (countryCodeToNonGeographicalMetadataMap) {
if (!countryCodeToNonGeographicalMetadataMap.containsKey(countryCallingCode)) {
loadMetadataFromFile(currentFilePrefix, PhoneNumberUtil.REGION_CODE_FOR_NON_GEO_ENTITY,
countryCallingCode, metadataLoader);
loadMetadataFromFile(PhoneNumberUtil.REGION_CODE_FOR_NON_GEO_ENTITY, countryCallingCode);
}
}
return countryCodeToNonGeographicalMetadataMap.get(countryCallingCode);
}
// @VisibleForTesting
void loadMetadataFromFile(String filePrefix, String regionCode, int countryCallingCode,
MetadataLoader metadataLoader) {
void loadMetadataFromFile(String regionCode, int countryCallingCode) {
boolean isNonGeoRegion = PhoneNumberUtil.REGION_CODE_FOR_NON_GEO_ENTITY.equals(regionCode);
String fileName = filePrefix + "_" +
(isNonGeoRegion ? String.valueOf(countryCallingCode) : regionCode);


+ 5
- 7
java/libphonenumber/test/com/google/i18n/phonenumbers/MultiFileMetadataSourceImplTest.java View File

@ -24,8 +24,8 @@ public class MultiFileMetadataSourceImplTest extends TestMetadataTestCase {
private final MultiFileMetadataSourceImpl multiFileMetadataSource;
public MultiFileMetadataSourceImplTest() {
multiFileMetadataSource = new MultiFileMetadataSourceImpl(TEST_META_DATA_FILE_PREFIX,
PhoneNumberUtil.DEFAULT_METADATA_LOADER);
multiFileMetadataSource = new MultiFileMetadataSourceImpl(
"no/such/file", PhoneNumberUtil.DEFAULT_METADATA_LOADER);
}
public void testMissingMetadataFileThrowsRuntimeException() {
@ -33,16 +33,14 @@ public class MultiFileMetadataSourceImplTest extends TestMetadataTestCase {
// exist. However if the library is packaged incorrectly in the jar, this could happen and the
// best we can do is make sure the exception has the file name in it.
try {
multiFileMetadataSource.loadMetadataFromFile(
"no/such/file", "XX", -1, PhoneNumberUtil.DEFAULT_METADATA_LOADER);
multiFileMetadataSource.loadMetadataFromFile("XX", -1);
fail("expected exception");
} catch (RuntimeException e) {
assertTrue("Unexpected error: " + e, e.toString().contains("no/such/file_XX"));
}
try {
multiFileMetadataSource.loadMetadataFromFile("no/such/file",
PhoneNumberUtil.REGION_CODE_FOR_NON_GEO_ENTITY, 123,
PhoneNumberUtil.DEFAULT_METADATA_LOADER);
multiFileMetadataSource.loadMetadataFromFile(
PhoneNumberUtil.REGION_CODE_FOR_NON_GEO_ENTITY, 123);
fail("expected exception");
} catch (RuntimeException e) {
assertTrue("Unexpected error: " + e, e.getMessage().contains("no/such/file_123"));


BIN
tools/java/java-build/target/java-build-1.0-SNAPSHOT-jar-with-dependencies.jar View File


Loading…
Cancel
Save