Home | History | Annotate | Download | only in phonenumbers
      1 /*
      2  * Copyright (C) 2015 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.google.i18n.phonenumbers;
     18 
     19 import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata;
     20 import java.util.List;
     21 import java.util.concurrent.ConcurrentHashMap;
     22 
     23 /**
     24  * Implementation of {@link MetadataSource} that reads from multiple resource files.
     25  */
     26 final class MultiFileMetadataSourceImpl implements MetadataSource {
     27   // The prefix of the binary files containing phone number metadata for different regions.
     28   // This enables us to set up with different metadata, such as for testing.
     29   private final String phoneNumberMetadataFilePrefix;
     30 
     31   // The {@link MetadataLoader} used to inject alternative metadata sources.
     32   private final MetadataLoader metadataLoader;
     33 
     34   // A mapping from a region code to the phone number metadata for that region code.
     35   // Unlike the mappings for alternate formats and short number metadata, the phone number metadata
     36   // is loaded from a non-statically determined file prefix; therefore this map is bound to the
     37   // instance and not static.
     38   private final ConcurrentHashMap<String, PhoneMetadata> geographicalRegions =
     39       new ConcurrentHashMap<String, PhoneMetadata>();
     40 
     41   // A mapping from a country calling code for a non-geographical entity to the phone number
     42   // metadata for that country calling code. Examples of the country calling codes include 800
     43   // (International Toll Free Service) and 808 (International Shared Cost Service).
     44   // Unlike the mappings for alternate formats and short number metadata, the phone number metadata
     45   // is loaded from a non-statically determined file prefix; therefore this map is bound to the
     46   // instance and not static.
     47   private final ConcurrentHashMap<Integer, PhoneMetadata> nonGeographicalRegions =
     48       new ConcurrentHashMap<Integer, PhoneMetadata>();
     49 
     50   // It is assumed that metadataLoader is not null. Checks should happen before passing it in here.
     51   // @VisibleForTesting
     52   MultiFileMetadataSourceImpl(String phoneNumberMetadataFilePrefix, MetadataLoader metadataLoader) {
     53     this.phoneNumberMetadataFilePrefix = phoneNumberMetadataFilePrefix;
     54     this.metadataLoader = metadataLoader;
     55   }
     56 
     57   // It is assumed that metadataLoader is not null. Checks should happen before passing it in here.
     58   MultiFileMetadataSourceImpl(MetadataLoader metadataLoader) {
     59     this(MetadataManager.MULTI_FILE_PHONE_NUMBER_METADATA_FILE_PREFIX, metadataLoader);
     60   }
     61 
     62   @Override
     63   public PhoneMetadata getMetadataForRegion(String regionCode) {
     64     return MetadataManager.getMetadataFromMultiFilePrefix(regionCode, geographicalRegions,
     65         phoneNumberMetadataFilePrefix, metadataLoader);
     66   }
     67 
     68   @Override
     69   public PhoneMetadata getMetadataForNonGeographicalRegion(int countryCallingCode) {
     70     if (!isNonGeographical(countryCallingCode)) {
     71       // The given country calling code was for a geographical region.
     72       return null;
     73     }
     74     return MetadataManager.getMetadataFromMultiFilePrefix(countryCallingCode, nonGeographicalRegions,
     75         phoneNumberMetadataFilePrefix, metadataLoader);
     76   }
     77 
     78   // A country calling code is non-geographical if it only maps to the non-geographical region code,
     79   // i.e. "001".
     80   private boolean isNonGeographical(int countryCallingCode) {
     81     List<String> regionCodes =
     82         CountryCodeToRegionCodeMap.getCountryCodeToRegionCodeMap().get(countryCallingCode);
     83     return (regionCodes.size() == 1
     84         && PhoneNumberUtil.REGION_CODE_FOR_NON_GEO_ENTITY.equals(regionCodes.get(0)));
     85   }
     86 }
     87