getMetadataFromSingleFileName(String fileName,
MetadataLoader metadataLoader) {
InputStream source = metadataLoader.loadMetadata(fileName);
if (source == null) {
diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/MultiFileMetadataSourceImpl.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/MultiFileMetadataSourceImpl.java
index 3b0dcba36..0c77c89e6 100644
--- a/java/libphonenumber/src/com/google/i18n/phonenumbers/MultiFileMetadataSourceImpl.java
+++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/MultiFileMetadataSourceImpl.java
@@ -62,7 +62,7 @@ final class MultiFileMetadataSourceImpl implements MetadataSource {
@Override
public PhoneMetadata getMetadataForRegion(String regionCode) {
- return MetadataManager.getMultiFileMetadata(regionCode, geographicalRegions,
+ return MetadataManager.getMetadataFromMultiFilePrefix(regionCode, geographicalRegions,
multiFilePhoneNumberMetadataFilePrefix, metadataLoader);
}
@@ -72,7 +72,7 @@ final class MultiFileMetadataSourceImpl implements MetadataSource {
// The given country calling code was for a geographical region.
return null;
}
- return MetadataManager.getMultiFileMetadata(countryCallingCode, nonGeographicalRegions,
+ return MetadataManager.getMetadataFromMultiFilePrefix(countryCallingCode, nonGeographicalRegions,
multiFilePhoneNumberMetadataFilePrefix, metadataLoader);
}
diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java
index 6a5910b2d..705d657be 100644
--- a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java
+++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java
@@ -22,7 +22,6 @@ import com.google.i18n.phonenumbers.Phonemetadata.PhoneNumberDesc;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber.CountryCodeSource;
-import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -52,14 +51,6 @@ import java.util.regex.Pattern;
* @author Shaopeng Jia
*/
public class PhoneNumberUtil {
- // @VisibleForTesting
- static final MetadataLoader DEFAULT_METADATA_LOADER = new MetadataLoader() {
- @Override
- public InputStream loadMetadata(String metadataFileName) {
- return PhoneNumberUtil.class.getResourceAsStream(metadataFileName);
- }
- };
-
private static final Logger logger = Logger.getLogger(PhoneNumberUtil.class.getName());
/** Flags to use when compiling regular expressions for phone numbers. */
@@ -958,8 +949,7 @@ public class PhoneNumberUtil {
/**
* Gets a {@link PhoneNumberUtil} instance to carry out international phone number formatting,
- * parsing, or validation. The instance is loaded with phone number metadata for a number of most
- * commonly used regions.
+ * parsing, or validation. The instance is loaded with all phone number metadata.
*
* The {@link PhoneNumberUtil} is implemented as a singleton. Therefore, calling getInstance
* multiple times will only result in one instance being created.
@@ -968,7 +958,7 @@ public class PhoneNumberUtil {
*/
public static synchronized PhoneNumberUtil getInstance() {
if (instance == null) {
- setInstance(createInstance(DEFAULT_METADATA_LOADER));
+ setInstance(createInstance(MetadataManager.DEFAULT_METADATA_LOADER));
}
return instance;
}
@@ -976,40 +966,40 @@ public class PhoneNumberUtil {
/**
* Create a new {@link PhoneNumberUtil} instance to carry out international phone number
* formatting, parsing, or validation. The instance is loaded with all metadata by
- * using the metadataSource specified.
+ * using the metadataLoader specified.
*
*
This method should only be used in the rare case in which you want to manage your own
* metadata loading. Calling this method multiple times is very expensive, as each time
* a new instance is created from scratch. When in doubt, use {@link #getInstance}.
*
- * @param metadataSource customized metadata source. This should not be null.
+ * @param metadataLoader customized metadata loader. This should not be null
* @return a PhoneNumberUtil instance
*/
- public static PhoneNumberUtil createInstance(MetadataSource metadataSource) {
- if (metadataSource == null) {
- throw new IllegalArgumentException("metadataSource could not be null.");
+ public static PhoneNumberUtil createInstance(MetadataLoader metadataLoader) {
+ if (metadataLoader == null) {
+ throw new IllegalArgumentException("metadataLoader could not be null.");
}
- return new PhoneNumberUtil(metadataSource,
- CountryCodeToRegionCodeMap.getCountryCodeToRegionCodeMap());
+ return createInstance(new MultiFileMetadataSourceImpl(metadataLoader));
}
/**
* Create a new {@link PhoneNumberUtil} instance to carry out international phone number
* formatting, parsing, or validation. The instance is loaded with all metadata by
- * using the metadataLoader specified.
+ * using the metadataSource specified.
*
- * This method should only be used in the rare case in which you want to manage your own
+ *
This method should only be used in the rare case in which you want to manage your own
* metadata loading. Calling this method multiple times is very expensive, as each time
* a new instance is created from scratch. When in doubt, use {@link #getInstance}.
*
- * @param metadataLoader Customized metadata loader. This should not be null.
- * @return a PhoneNumberUtil instance
+ * @param metadataSource customized metadata source. This should not be null
+ * @return a PhoneNumberUtil instance
*/
- public static PhoneNumberUtil createInstance(MetadataLoader metadataLoader) {
- if (metadataLoader == null) {
- throw new IllegalArgumentException("metadataLoader could not be null.");
+ private static PhoneNumberUtil createInstance(MetadataSource metadataSource) {
+ if (metadataSource == null) {
+ throw new IllegalArgumentException("metadataSource could not be null.");
}
- return createInstance(new MultiFileMetadataSourceImpl(metadataLoader));
+ return new PhoneNumberUtil(metadataSource,
+ CountryCodeToRegionCodeMap.getCountryCodeToRegionCodeMap());
}
/**
@@ -1078,7 +1068,7 @@ public class PhoneNumberUtil {
// This is the only case where a number can be formatted as E164 without a
// leading '+' symbol (but the original number wasn't parseable anyway).
// TODO: Consider removing the 'if' above so that unparseable
- // strings without raw input format to the empty string instead of "+00"
+ // strings without raw input format to the empty string instead of "+00".
String rawInput = number.getRawInput();
if (rawInput.length() > 0) {
return rawInput;
@@ -1148,7 +1138,7 @@ public class PhoneNumberUtil {
// share a country calling code is contained by only one region for performance reasons. For
// example, for NANPA regions it will be contained in the metadata for US.
String regionCode = getRegionCodeForCountryCode(countryCallingCode);
- // Metadata cannot be null because the country calling code is valid
+ // Metadata cannot be null because the country calling code is valid.
PhoneMetadata metadata =
getMetadataForRegionOrCallingCode(countryCallingCode, regionCode);
@@ -1306,8 +1296,8 @@ public class PhoneNumberUtil {
// dialing from a mobile phone, except for short numbers. As a result, we add it back here
// if it is a valid regular length phone number.
formattedNumber =
- getNddPrefixForRegion(regionCode, true /* strip non-digits */) +
- " " + format(numberNoExt, PhoneNumberFormat.NATIONAL);
+ getNddPrefixForRegion(regionCode, true /* strip non-digits */) + " "
+ + format(numberNoExt, PhoneNumberFormat.NATIONAL);
} else if (countryCallingCode == NANPA_COUNTRY_CODE) {
// For NANPA countries, we output international format for numbers that can be dialed
// internationally, since that always works, except for numbers which might potentially be
diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/SingleFileMetadataSourceImpl.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/SingleFileMetadataSourceImpl.java
index 1fecac2b7..a988a543e 100644
--- a/java/libphonenumber/src/com/google/i18n/phonenumbers/SingleFileMetadataSourceImpl.java
+++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/SingleFileMetadataSourceImpl.java
@@ -41,7 +41,7 @@ final class SingleFileMetadataSourceImpl implements MetadataSource {
}
// It is assumed that metadataLoader is not null. Checks should happen before passing it in here.
- public SingleFileMetadataSourceImpl(MetadataLoader metadataLoader) {
+ SingleFileMetadataSourceImpl(MetadataLoader metadataLoader) {
this(MetadataManager.SINGLE_FILE_PHONE_NUMBER_METADATA_FILE_NAME, metadataLoader);
}
diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/ExampleNumbersTest.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/ExampleNumbersTest.java
index 93f84b1f5..d21d9ba3b 100644
--- a/java/libphonenumber/test/com/google/i18n/phonenumbers/ExampleNumbersTest.java
+++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/ExampleNumbersTest.java
@@ -37,8 +37,7 @@ import java.util.logging.Logger;
*/
public class ExampleNumbersTest extends TestCase {
private static final Logger logger = Logger.getLogger(ExampleNumbersTest.class.getName());
- private PhoneNumberUtil phoneNumberUtil =
- PhoneNumberUtil.createInstance(PhoneNumberUtil.DEFAULT_METADATA_LOADER);
+ private PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
private ShortNumberInfo shortNumberInfo = ShortNumberInfo.getInstance();
private List invalidCases = new ArrayList();
private List wrongTypeCases = new ArrayList();
diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/MetadataManagerTest.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/MetadataManagerTest.java
index d632b63a4..f49055323 100644
--- a/java/libphonenumber/test/com/google/i18n/phonenumbers/MetadataManagerTest.java
+++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/MetadataManagerTest.java
@@ -23,7 +23,7 @@ import junit.framework.TestCase;
/**
* Some basic tests to check that metadata can be correctly loaded.
*/
-public final class MetadataManagerTest extends TestCase {
+public class MetadataManagerTest extends TestCase {
public void testAlternateFormatsLoadCorrectly() {
// We should have some data for Germany.
PhoneMetadata germanyMetadata = MetadataManager.getAlternateFormatsForCountry(49);
@@ -48,36 +48,38 @@ public final class MetadataManagerTest extends TestCase {
assertNull(noShortNumberMetadata);
}
- public void testGetMultiFileMetadata_regionCode() {
+ public void testGetMetadataFromMultiFilePrefix_regionCode() {
ConcurrentHashMap map = new ConcurrentHashMap();
- PhoneMetadata metadata = MetadataManager.getMultiFileMetadata("CA", map,
+ PhoneMetadata metadata = MetadataManager.getMetadataFromMultiFilePrefix("CA", map,
"/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting",
- PhoneNumberUtil.DEFAULT_METADATA_LOADER);
+ MetadataManager.DEFAULT_METADATA_LOADER);
assertEquals(metadata, map.get("CA"));
}
- public void testGetMultiFileMetadata_countryCallingCode() {
+ public void testGetMetadataFromMultiFilePrefix_countryCallingCode() {
ConcurrentHashMap map = new ConcurrentHashMap();
- PhoneMetadata metadata = MetadataManager.getMultiFileMetadata(800, map,
+ PhoneMetadata metadata = MetadataManager.getMetadataFromMultiFilePrefix(800, map,
"/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting",
- PhoneNumberUtil.DEFAULT_METADATA_LOADER);
+ MetadataManager.DEFAULT_METADATA_LOADER);
assertEquals(metadata, map.get(800));
}
- public void testGetMultiFileMetadata_missingMetadataFileThrowsRuntimeException() {
+ public void testGetMetadataFromMultiFilePrefix_missingMetadataFileThrowsRuntimeException() {
// In normal usage we should never get a state where we are asking to load metadata that doesn't
// 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 {
- MetadataManager.getMultiFileMetadata("XX", new ConcurrentHashMap(),
- "no/such/file", PhoneNumberUtil.DEFAULT_METADATA_LOADER);
+ MetadataManager.getMetadataFromMultiFilePrefix("XX",
+ new ConcurrentHashMap(), "no/such/file",
+ MetadataManager.DEFAULT_METADATA_LOADER);
fail("expected exception");
} catch (RuntimeException e) {
assertTrue("Unexpected error: " + e, e.getMessage().contains("no/such/file_XX"));
}
try {
- MetadataManager.getMultiFileMetadata(123, new ConcurrentHashMap(),
- "no/such/file", PhoneNumberUtil.DEFAULT_METADATA_LOADER);
+ MetadataManager.getMetadataFromMultiFilePrefix(123,
+ new ConcurrentHashMap(), "no/such/file",
+ MetadataManager.DEFAULT_METADATA_LOADER);
fail("expected exception");
} catch (RuntimeException e) {
assertTrue("Unexpected error: " + e, e.getMessage().contains("no/such/file_123"));
diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/MultiFileMetadataSourceImplTest.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/MultiFileMetadataSourceImplTest.java
index a8925ab80..239db3df5 100644
--- a/java/libphonenumber/test/com/google/i18n/phonenumbers/MultiFileMetadataSourceImplTest.java
+++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/MultiFileMetadataSourceImplTest.java
@@ -24,21 +24,21 @@ import junit.framework.TestCase;
* Unit tests for MultiFileMetadataSourceImpl.java.
*/
public class MultiFileMetadataSourceImplTest extends TestCase {
- private static final MultiFileMetadataSourceImpl source =
- new MultiFileMetadataSourceImpl(PhoneNumberUtil.DEFAULT_METADATA_LOADER);
- private static final MultiFileMetadataSourceImpl missingFileSource =
- new MultiFileMetadataSourceImpl("no/such/file", PhoneNumberUtil.DEFAULT_METADATA_LOADER);
+ private static final MultiFileMetadataSourceImpl SOURCE =
+ new MultiFileMetadataSourceImpl(MetadataManager.DEFAULT_METADATA_LOADER);
+ private static final MultiFileMetadataSourceImpl MISSING_FILE_SOURCE =
+ new MultiFileMetadataSourceImpl("no/such/file", MetadataManager.DEFAULT_METADATA_LOADER);
public void testGeoPhoneNumberMetadataLoadCorrectly() {
// We should have some data for the UAE.
- PhoneMetadata uaeMetadata = source.getMetadataForRegion("AE");
+ PhoneMetadata uaeMetadata = SOURCE.getMetadataForRegion("AE");
assertEquals(uaeMetadata.getCountryCode(), 971);
assertTrue(uaeMetadata.hasGeneralDesc());
}
public void testGeoPhoneNumberMetadataLoadFromMissingFileThrowsException() throws Exception {
try {
- missingFileSource.getMetadataForRegion("AE");
+ MISSING_FILE_SOURCE.getMetadataForRegion("AE");
fail("expected exception");
} catch (RuntimeException e) {
assertTrue("Unexpected error: " + e, e.getMessage().contains("no/such/file"));
@@ -47,14 +47,14 @@ public class MultiFileMetadataSourceImplTest extends TestCase {
public void testNonGeoPhoneNumberMetadataLoadCorrectly() {
// We should have some data for international toll-free numbers.
- PhoneMetadata intlMetadata = source.getMetadataForNonGeographicalRegion(800);
+ PhoneMetadata intlMetadata = SOURCE.getMetadataForNonGeographicalRegion(800);
assertEquals(intlMetadata.getId(), "001");
assertTrue(intlMetadata.hasGeneralDesc());
}
public void testNonGeoPhoneNumberMetadataLoadFromMissingFileThrowsException() throws Exception {
try {
- missingFileSource.getMetadataForNonGeographicalRegion(800);
+ MISSING_FILE_SOURCE.getMetadataForNonGeographicalRegion(800);
fail("expected exception");
} catch (RuntimeException e) {
assertTrue("Unexpected error: " + e, e.getMessage().contains("no/such/file"));
diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/SingleFileMetadataSourceImplTest.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/SingleFileMetadataSourceImplTest.java
index ce2efc113..664fc52d5 100644
--- a/java/libphonenumber/test/com/google/i18n/phonenumbers/SingleFileMetadataSourceImplTest.java
+++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/SingleFileMetadataSourceImplTest.java
@@ -26,7 +26,7 @@ import junit.framework.TestCase;
*/
public class SingleFileMetadataSourceImplTest extends TestCase {
private static final SingleFileMetadataSourceImpl MISSING_FILE_SOURCE =
- new SingleFileMetadataSourceImpl("no/such/file", PhoneNumberUtil.DEFAULT_METADATA_LOADER);
+ new SingleFileMetadataSourceImpl("no/such/file", MetadataManager.DEFAULT_METADATA_LOADER);
public void testGeoPhoneNumberMetadataLoadFromMissingFileThrowsException() throws Exception {
try {
diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/TestMetadataTestCase.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/TestMetadataTestCase.java
index ca9014d44..51360d71a 100644
--- a/java/libphonenumber/test/com/google/i18n/phonenumbers/TestMetadataTestCase.java
+++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/TestMetadataTestCase.java
@@ -23,25 +23,37 @@ import junit.framework.TestCase;
*
* Note since tests that extend this class do not use the normal metadata file, they should not be
* used for regression test purposes.
+ *
+ * Ideally the {@code phoneUtil} field (which uses test metadata) would be the only way that tests
+ * need to interact with a PhoneNumberUtil instance. However as some static methods in the library
+ * invoke "getInstance()" internally, we must also inject the test instance as the PhoneNumberUtil
+ * singleton. This means it is unsafe to run tests derived from this class in parallel with each
+ * other or at the same time as other tests which might require the singleton instance.
*
* @author Shaopeng Jia
*/
public class TestMetadataTestCase extends TestCase {
- private static final String TEST_META_DATA_FILE_PREFIX =
+ private static final String TEST_METADATA_FILE_PREFIX =
"/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting";
+ /** An instance of PhoneNumberUtil that uses test metadata. */
protected final PhoneNumberUtil phoneUtil;
public TestMetadataTestCase() {
- phoneUtil = initializePhoneUtilForTesting();
+ phoneUtil = new PhoneNumberUtil(new MultiFileMetadataSourceImpl(TEST_METADATA_FILE_PREFIX,
+ MetadataManager.DEFAULT_METADATA_LOADER),
+ CountryCodeToRegionCodeMapForTesting.getCountryCodeToRegionCodeMap());
}
- static PhoneNumberUtil initializePhoneUtilForTesting() {
- PhoneNumberUtil phoneUtil = new PhoneNumberUtil(
- new MultiFileMetadataSourceImpl(TEST_META_DATA_FILE_PREFIX,
- PhoneNumberUtil.DEFAULT_METADATA_LOADER),
- CountryCodeToRegionCodeMapForTesting.getCountryCodeToRegionCodeMap());
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
PhoneNumberUtil.setInstance(phoneUtil);
- return phoneUtil;
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ PhoneNumberUtil.setInstance(null);
+ super.tearDown();
}
}
diff --git a/java/pending_code_changes.txt b/java/pending_code_changes.txt
index 40ac6b7b7..32d6b0394 100644
--- a/java/pending_code_changes.txt
+++ b/java/pending_code_changes.txt
@@ -5,3 +5,7 @@ Code changes:
- Refactored metadata loading and removed synchronization for all kinds of
metadata. Clients may experience quicker loading of alternate formats and
short number metadata, but no change is required for callers of the library.
+ - Reduced visibility of `public` internal API
+ `PhoneNumberUtil.createInstance(MetadataSource)` to `private`. MetadataSource
+ and all its implementations are non-public so this should not affect public
+ usage of the library.
diff --git a/tools/java/common/test/com/google/i18n/phonenumbers/MetadataFilterTest.java b/tools/java/common/test/com/google/i18n/phonenumbers/MetadataFilterTest.java
index f3f591f95..8c8494c79 100644
--- a/tools/java/common/test/com/google/i18n/phonenumbers/MetadataFilterTest.java
+++ b/tools/java/common/test/com/google/i18n/phonenumbers/MetadataFilterTest.java
@@ -140,8 +140,7 @@ public class MetadataFilterTest extends TestCase {
public void testParseFieldMapFromString_mixOfGroups() {
TreeMap> fieldMap = new TreeMap>();
fieldMap.put("uan", new TreeSet(Arrays.asList(
- "possibleLength", "exampleNumber", "possibleLengthLocalOnly",
- "nationalNumberPattern")));
+ "possibleLength", "exampleNumber", "possibleLengthLocalOnly", "nationalNumberPattern")));
fieldMap.put("pager", new TreeSet(Arrays.asList(
"exampleNumber", "nationalNumberPattern")));
fieldMap.put("fixedLine", new TreeSet(Arrays.asList(
@@ -223,8 +222,7 @@ public class MetadataFilterTest extends TestCase {
// All parent's children covered, some implicitly and some explicitly.
assertEquals(
MetadataFilter.parseFieldMapFromString(
- "uan(nationalNumberPattern,possibleLength,exampleNumber)"
- + ":possibleLengthLocalOnly"),
+ "uan(nationalNumberPattern,possibleLength,exampleNumber):possibleLengthLocalOnly"),
MetadataFilter.parseFieldMapFromString("uan:possibleLengthLocalOnly"));
// Child field covered by all parents explicitly.
diff --git a/tools/java/cpp-build/target/cpp-build-1.0-SNAPSHOT-jar-with-dependencies.jar b/tools/java/cpp-build/target/cpp-build-1.0-SNAPSHOT-jar-with-dependencies.jar
index b9818a77a..48dd0e36a 100644
Binary files a/tools/java/cpp-build/target/cpp-build-1.0-SNAPSHOT-jar-with-dependencies.jar and b/tools/java/cpp-build/target/cpp-build-1.0-SNAPSHOT-jar-with-dependencies.jar differ
diff --git a/tools/java/java-build/target/java-build-1.0-SNAPSHOT-jar-with-dependencies.jar b/tools/java/java-build/target/java-build-1.0-SNAPSHOT-jar-with-dependencies.jar
index 30d4b8c04..31889b4d7 100644
Binary files a/tools/java/java-build/target/java-build-1.0-SNAPSHOT-jar-with-dependencies.jar and b/tools/java/java-build/target/java-build-1.0-SNAPSHOT-jar-with-dependencies.jar differ