1 /* GENERATED SOURCE. DO NOT MODIFY. */ 2 /* 3 * Copyright (C) 2015 The Libphonenumber Authors 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.i18n.phonenumbers; 19 20 import com.android.i18n.phonenumbers.Phonemetadata.PhoneMetadata; 21 import java.util.List; 22 import java.util.concurrent.ConcurrentHashMap; 23 24 /** 25 * Implementation of {@link MetadataSource} that reads from multiple resource files. 26 */ 27 final class MultiFileMetadataSourceImpl implements MetadataSource { 28 // The prefix of the binary files containing phone number metadata for different regions. 29 // This enables us to set up with different metadata, such as for testing. 30 private final String phoneNumberMetadataFilePrefix; 31 32 // The {@link MetadataLoader} used to inject alternative metadata sources. 33 private final MetadataLoader metadataLoader; 34 35 // A mapping from a region code to the phone number metadata for that region code. 36 // Unlike the mappings for alternate formats and short number metadata, the phone number metadata 37 // is loaded from a non-statically determined file prefix; therefore this map is bound to the 38 // instance and not static. 39 private final ConcurrentHashMap<String, PhoneMetadata> geographicalRegions = 40 new ConcurrentHashMap<String, PhoneMetadata>(); 41 42 // A mapping from a country calling code for a non-geographical entity to the phone number 43 // metadata for that country calling code. Examples of the country calling codes include 800 44 // (International Toll Free Service) and 808 (International Shared Cost Service). 45 // Unlike the mappings for alternate formats and short number metadata, the phone number metadata 46 // is loaded from a non-statically determined file prefix; therefore this map is bound to the 47 // instance and not static. 48 private final ConcurrentHashMap<Integer, PhoneMetadata> nonGeographicalRegions = 49 new ConcurrentHashMap<Integer, PhoneMetadata>(); 50 51 // It is assumed that metadataLoader is not null. Checks should happen before passing it in here. 52 // @VisibleForTesting 53 MultiFileMetadataSourceImpl(String phoneNumberMetadataFilePrefix, MetadataLoader metadataLoader) { 54 this.phoneNumberMetadataFilePrefix = phoneNumberMetadataFilePrefix; 55 this.metadataLoader = metadataLoader; 56 } 57 58 // It is assumed that metadataLoader is not null. Checks should happen before passing it in here. 59 MultiFileMetadataSourceImpl(MetadataLoader metadataLoader) { 60 this(MetadataManager.MULTI_FILE_PHONE_NUMBER_METADATA_FILE_PREFIX, metadataLoader); 61 } 62 63 @Override 64 public PhoneMetadata getMetadataForRegion(String regionCode) { 65 return MetadataManager.getMetadataFromMultiFilePrefix(regionCode, geographicalRegions, 66 phoneNumberMetadataFilePrefix, metadataLoader); 67 } 68 69 @Override 70 public PhoneMetadata getMetadataForNonGeographicalRegion(int countryCallingCode) { 71 if (!isNonGeographical(countryCallingCode)) { 72 // The given country calling code was for a geographical region. 73 return null; 74 } 75 return MetadataManager.getMetadataFromMultiFilePrefix(countryCallingCode, nonGeographicalRegions, 76 phoneNumberMetadataFilePrefix, metadataLoader); 77 } 78 79 // A country calling code is non-geographical if it only maps to the non-geographical region code, 80 // i.e. "001". 81 private boolean isNonGeographical(int countryCallingCode) { 82 List<String> regionCodes = 83 CountryCodeToRegionCodeMap.getCountryCodeToRegionCodeMap().get(countryCallingCode); 84 return (regionCodes.size() == 1 85 && PhoneNumberUtil.REGION_CODE_FOR_NON_GEO_ENTITY.equals(regionCodes.get(0))); 86 } 87 } 88