diff options
author | roubert@google.com <roubert@google.com@ee073f10-1060-11df-b6a4-87a95322a99c> | 2012-07-04 10:08:41 +0000 |
---|---|---|
committer | roubert@google.com <roubert@google.com@ee073f10-1060-11df-b6a4-87a95322a99c> | 2012-07-04 10:08:41 +0000 |
commit | 80c24f1c9bf324e87ab6c2b5ce760f2562e3a10a (patch) | |
tree | 085b64aceeb4867ebaebaae7d728349d9f0ff879 /java/libphonenumber | |
parent | 1a458bcb40a693710bde54cd25446362a18e869c (diff) | |
download | android_external_libphonenumbergoogle-80c24f1c9bf324e87ab6c2b5ce760f2562e3a10a.tar.gz android_external_libphonenumbergoogle-80c24f1c9bf324e87ab6c2b5ce760f2562e3a10a.tar.bz2 android_external_libphonenumbergoogle-80c24f1c9bf324e87ab6c2b5ce760f2562e3a10a.zip |
JAVA/METADATA: Port MetadataManager and data files to open source.
Review URL: https://codereview.appspot.com/6345053
git-svn-id: http://libphonenumber.googlecode.com/svn/trunk@486 ee073f10-1060-11df-b6a4-87a95322a99c
Diffstat (limited to 'java/libphonenumber')
8 files changed, 178 insertions, 0 deletions
diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/AlternateFormatsCountryCodeSet.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/AlternateFormatsCountryCodeSet.java new file mode 100644 index 0000000..6fe7c86 --- /dev/null +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/AlternateFormatsCountryCodeSet.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2012 The Libphonenumber Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* This file is automatically generated by {@link BuildMetadataProtoFromXml}. + * Please don't modify it directly. + */ + +package com.google.i18n.phonenumbers; + +import java.util.HashSet; +import java.util.Set; + +public class AlternateFormatsCountryCodeSet { + // A set of all country codes for which data is available. + static Set<Integer> getCountryCodeSet() { + // The capacity is set to 6 as there are 5 different country codes, + // and this offers a load factor of roughly 0.75. + Set<Integer> countryCodeSet = new HashSet<Integer>(6); + + countryCodeSet.add(44); + countryCodeSet.add(49); + countryCodeSet.add(55); + countryCodeSet.add(61); + countryCodeSet.add(81); + + return countryCodeSet; + } +} diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/MetadataManager.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/MetadataManager.java new file mode 100644 index 0000000..73e5de9 --- /dev/null +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/MetadataManager.java @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2012 The Libphonenumber Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.i18n.phonenumbers; + +import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; +import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadataCollection; + +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Class encapsulating loading of PhoneNumber Metadata information. Currently this is used only for + * additional data files such as PhoneNumberAlternateFormats, but in the future it is envisaged it + * would handle the main metadata file (PhoneNumberMetaData.xml) as well. + * + * @author Lara Rennie + */ +class MetadataManager { + private static final String ALTERNATE_FORMATS_FILE_PREFIX = + "/com/google/i18n/phonenumbers/data/PhoneNumberAlternateFormatsProto"; + + private static final Logger LOGGER = Logger.getLogger(MetadataManager.class.getName()); + + private static final Map<Integer, PhoneMetadata> callingCodeToAlternateFormatsMap = + Collections.synchronizedMap(new HashMap<Integer, PhoneMetadata>()); + + // A set of which country calling codes there are alternate format data for. If the set has an + // entry for a code, then there should be data for that code linked into the resources. + private static final Set<Integer> countryCodeSet = + AlternateFormatsCountryCodeSet.getCountryCodeSet(); + + private MetadataManager() { + } + + private static void close(InputStream in) { + if (in != null) { + try { + in.close(); + } catch (IOException e) { + LOGGER.log(Level.WARNING, e.toString()); + } + } + } + + private static void loadMetadataFromFile(int countryCallingCode) { + InputStream source = PhoneNumberMatcher.class.getResourceAsStream( + ALTERNATE_FORMATS_FILE_PREFIX + "_" + countryCallingCode); + ObjectInputStream in = null; + try { + in = new ObjectInputStream(source); + PhoneMetadataCollection alternateFormats = new PhoneMetadataCollection(); + alternateFormats.readExternal(in); + for (PhoneMetadata metadata : alternateFormats.getMetadataList()) { + callingCodeToAlternateFormatsMap.put(metadata.getCountryCode(), metadata); + } + } catch (IOException e) { + LOGGER.log(Level.WARNING, e.toString()); + } finally { + close(in); + } + } + + static PhoneMetadata getAlternateFormatsForCountry(int countryCallingCode) { + if (!countryCodeSet.contains(countryCallingCode)) { + return null; + } + synchronized (callingCodeToAlternateFormatsMap) { + if (!callingCodeToAlternateFormatsMap.containsKey(countryCallingCode)) { + loadMetadataFromFile(countryCallingCode); + } + } + return callingCodeToAlternateFormatsMap.get(countryCallingCode); + } +} diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberAlternateFormatsProto_44 b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberAlternateFormatsProto_44 Binary files differnew file mode 100644 index 0000000..9eff94e --- /dev/null +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberAlternateFormatsProto_44 diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberAlternateFormatsProto_49 b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberAlternateFormatsProto_49 Binary files differnew file mode 100644 index 0000000..5c2a493 --- /dev/null +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberAlternateFormatsProto_49 diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberAlternateFormatsProto_55 b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberAlternateFormatsProto_55 Binary files differnew file mode 100644 index 0000000..7e928a2 --- /dev/null +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberAlternateFormatsProto_55 diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberAlternateFormatsProto_61 b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberAlternateFormatsProto_61 Binary files differnew file mode 100644 index 0000000..b685e8f --- /dev/null +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberAlternateFormatsProto_61 diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberAlternateFormatsProto_81 b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberAlternateFormatsProto_81 Binary files differnew file mode 100644 index 0000000..11a75b9 --- /dev/null +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberAlternateFormatsProto_81 diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/MetadataManagerTest.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/MetadataManagerTest.java new file mode 100644 index 0000000..ebe1bcb --- /dev/null +++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/MetadataManagerTest.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2012 The Libphonenumber Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.i18n.phonenumbers; + +import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; + +import junit.framework.TestCase; + +/** + * Some basic tests to check that the phone number metadata can be correctly loaded. + * + * @author Lara Rennie + */ +public class MetadataManagerTest extends TestCase { + + public void testAlternateFormatsContainsData() throws Exception { + // We should have some data for Germany. + PhoneMetadata germanyAlternateFormats = MetadataManager.getAlternateFormatsForCountry(49); + assertNotNull(germanyAlternateFormats); + assertTrue(germanyAlternateFormats.numberFormats().size() > 0); + } + + public void testAlternateFormatsFailsGracefully() throws Exception { + PhoneMetadata noAlternateFormats = MetadataManager.getAlternateFormatsForCountry(999); + assertNull(noAlternateFormats); + } +} |