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.concurrent.atomic.AtomicReference;
     21 
     22 /**
     23  * Implementation of {@link MetadataSource} that reads from a single resource file.
     24  */
     25 final class SingleFileMetadataSourceImpl implements MetadataSource {
     26   // The name of the binary file containing phone number metadata for different regions.
     27   // This enables us to set up with different metadata, such as for testing.
     28   private final String phoneNumberMetadataFileName;
     29 
     30   // The {@link MetadataLoader} used to inject alternative metadata sources.
     31   private final MetadataLoader metadataLoader;
     32 
     33   private final AtomicReference<MetadataManager.SingleFileMetadataMaps> phoneNumberMetadataRef =
     34       new AtomicReference<MetadataManager.SingleFileMetadataMaps>();
     35 
     36   // It is assumed that metadataLoader is not null. Checks should happen before passing it in here.
     37   // @VisibleForTesting
     38   SingleFileMetadataSourceImpl(String phoneNumberMetadataFileName, MetadataLoader metadataLoader) {
     39     this.phoneNumberMetadataFileName = phoneNumberMetadataFileName;
     40     this.metadataLoader = metadataLoader;
     41   }
     42 
     43   // It is assumed that metadataLoader is not null. Checks should happen before passing it in here.
     44   SingleFileMetadataSourceImpl(MetadataLoader metadataLoader) {
     45     this(MetadataManager.SINGLE_FILE_PHONE_NUMBER_METADATA_FILE_NAME, metadataLoader);
     46   }
     47 
     48   @Override
     49   public PhoneMetadata getMetadataForRegion(String regionCode) {
     50     return MetadataManager.getSingleFileMetadataMaps(phoneNumberMetadataRef,
     51         phoneNumberMetadataFileName, metadataLoader).get(regionCode);
     52   }
     53 
     54   @Override
     55   public PhoneMetadata getMetadataForNonGeographicalRegion(int countryCallingCode) {
     56     // A country calling code is non-geographical if it only maps to the non-geographical region
     57     // code, i.e. "001". If this is not true of the given country calling code, then we will return
     58     // null here. If not for the atomic reference, such as if we were loading in multiple stages, we
     59     // would check that the passed in country calling code was indeed non-geographical to avoid
     60     // loading costs for a null result. Here though we do not check this since the entire data must
     61     // be loaded anyway if any of it is needed at some point in the life cycle of this class.
     62     return MetadataManager.getSingleFileMetadataMaps(phoneNumberMetadataRef,
     63         phoneNumberMetadataFileName, metadataLoader).get(countryCallingCode);
     64   }
     65 }
     66