1 /* 2 * Copyright (C) 2022 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.metadata.source; 18 19 import com.google.i18n.phonenumbers.MetadataLoader; 20 import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; 21 import com.google.i18n.phonenumbers.internal.GeoEntityUtility; 22 import com.google.i18n.phonenumbers.metadata.init.MetadataParser; 23 24 /** 25 * Implementation of {@link MetadataSource} guarded by {@link MetadataBootstrappingGuard}. 26 * 27 * <p>By default, a {@link BlockingMetadataBootstrappingGuard} will be used, but any custom 28 * implementation can be injected. 29 */ 30 public final class MetadataSourceImpl implements MetadataSource { 31 32 private final PhoneMetadataFileNameProvider phoneMetadataFileNameProvider; 33 private final MetadataBootstrappingGuard<CompositeMetadataContainer> bootstrappingGuard; 34 MetadataSourceImpl( PhoneMetadataFileNameProvider phoneMetadataFileNameProvider, MetadataBootstrappingGuard<CompositeMetadataContainer> bootstrappingGuard)35 public MetadataSourceImpl( 36 PhoneMetadataFileNameProvider phoneMetadataFileNameProvider, 37 MetadataBootstrappingGuard<CompositeMetadataContainer> bootstrappingGuard) { 38 this.phoneMetadataFileNameProvider = phoneMetadataFileNameProvider; 39 this.bootstrappingGuard = bootstrappingGuard; 40 } 41 MetadataSourceImpl( PhoneMetadataFileNameProvider phoneMetadataFileNameProvider, MetadataLoader metadataLoader, MetadataParser metadataParser)42 public MetadataSourceImpl( 43 PhoneMetadataFileNameProvider phoneMetadataFileNameProvider, 44 MetadataLoader metadataLoader, 45 MetadataParser metadataParser) { 46 this( 47 phoneMetadataFileNameProvider, 48 new BlockingMetadataBootstrappingGuard<>( 49 metadataLoader, metadataParser, new CompositeMetadataContainer())); 50 } 51 52 @Override getMetadataForNonGeographicalRegion(int countryCallingCode)53 public PhoneMetadata getMetadataForNonGeographicalRegion(int countryCallingCode) { 54 if (GeoEntityUtility.isGeoEntity(countryCallingCode)) { 55 throw new IllegalArgumentException( 56 countryCallingCode + " calling code belongs to a geo entity"); 57 } 58 return bootstrappingGuard 59 .getOrBootstrap(phoneMetadataFileNameProvider.getFor(countryCallingCode)) 60 .getMetadataBy(countryCallingCode); 61 } 62 63 @Override getMetadataForRegion(String regionCode)64 public PhoneMetadata getMetadataForRegion(String regionCode) { 65 if (!GeoEntityUtility.isGeoEntity(regionCode)) { 66 throw new IllegalArgumentException(regionCode + " region code is a non-geo entity"); 67 } 68 return bootstrappingGuard 69 .getOrBootstrap(phoneMetadataFileNameProvider.getFor(regionCode)) 70 .getMetadataBy(regionCode); 71 } 72 } 73