• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Libphonenumber Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.i18n.phonenumbers;
18 
19 import com.android.i18n.phonenumbers.Phonemetadata.PhoneMetadata;
20 import com.android.i18n.phonenumbers.Phonemetadata.PhoneMetadataCollection;
21 
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.ObjectInputStream;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
32 
33 /**
34  * Class encapsulating loading of PhoneNumber Metadata information. Currently this is used only for
35  * additional data files such as PhoneNumberAlternateFormats, but in the future it is envisaged it
36  * would handle the main metadata file (PhoneNumberMetaData.xml) as well.
37  *
38  * @author Lara Rennie
39  */
40 class MetadataManager {
41   private static final String ALTERNATE_FORMATS_FILE_PREFIX =
42       "/com/android/i18n/phonenumbers/data/PhoneNumberAlternateFormatsProto";
43 
44   private static final Logger LOGGER = Logger.getLogger(MetadataManager.class.getName());
45 
46   private static final Map<Integer, PhoneMetadata> callingCodeToAlternateFormatsMap =
47       Collections.synchronizedMap(new HashMap<Integer, PhoneMetadata>());
48 
49   // A set of which country calling codes there are alternate format data for. If the set has an
50   // entry for a code, then there should be data for that code linked into the resources.
51   private static final Set<Integer> countryCodeSet =
52       AlternateFormatsCountryCodeSet.getCountryCodeSet();
53 
MetadataManager()54   private MetadataManager() {
55   }
56 
close(InputStream in)57   private static void close(InputStream in) {
58     if (in != null) {
59       try {
60         in.close();
61       } catch (IOException e) {
62         LOGGER.log(Level.WARNING, e.toString());
63       }
64     }
65   }
66 
loadMetadataFromFile(int countryCallingCode)67   private static void loadMetadataFromFile(int countryCallingCode) {
68     InputStream source = PhoneNumberMatcher.class.getResourceAsStream(
69         ALTERNATE_FORMATS_FILE_PREFIX + "_" + countryCallingCode);
70     ObjectInputStream in = null;
71     try {
72       in = new ObjectInputStream(source);
73       PhoneMetadataCollection alternateFormats = new PhoneMetadataCollection();
74       alternateFormats.readExternal(in);
75       for (PhoneMetadata metadata : alternateFormats.getMetadataList()) {
76         callingCodeToAlternateFormatsMap.put(metadata.getCountryCode(), metadata);
77       }
78     } catch (IOException e) {
79       LOGGER.log(Level.WARNING, e.toString());
80     } finally {
81       close(in);
82     }
83   }
84 
getAlternateFormatsForCountry(int countryCallingCode)85   static PhoneMetadata getAlternateFormatsForCountry(int countryCallingCode) {
86     if (!countryCodeSet.contains(countryCallingCode)) {
87       return null;
88     }
89     synchronized (callingCodeToAlternateFormatsMap) {
90       if (!callingCodeToAlternateFormatsMap.containsKey(countryCallingCode)) {
91         loadMetadataFromFile(countryCallingCode);
92       }
93     }
94     return callingCodeToAlternateFormatsMap.get(countryCallingCode);
95   }
96 }
97