• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.internal;
18 
19 import com.google.i18n.phonenumbers.CountryCodeToRegionCodeMap;
20 import java.util.List;
21 
22 /**
23  * Utility class for checking whether identifiers region code and country calling code belong
24  * to geographical entities. For more information about geo vs. non-geo entities see {@link
25  * com.google.i18n.phonenumbers.metadata.source.RegionMetadataSource} and {@link
26  * com.google.i18n.phonenumbers.metadata.source.NonGeographicalEntityMetadataSource}
27  */
28 public final class GeoEntityUtility {
29 
30   /** Region code with a special meaning, used to mark non-geographical entities */
31   public static final String REGION_CODE_FOR_NON_GEO_ENTITIES = "001";
32 
33   /** Determines whether {@code regionCode} belongs to a geographical entity. */
isGeoEntity(String regionCode)34   public static boolean isGeoEntity(String regionCode) {
35     return !regionCode.equals(REGION_CODE_FOR_NON_GEO_ENTITIES);
36   }
37 
38   /**
39    * Determines whether {@code countryCallingCode} belongs to a geographical entity.
40    *
41    * <p>A single country calling code could map to several different regions. It is considered that
42    * {@code countryCallingCode} belongs to a geo entity if all of these regions are geo entities
43    *
44    * <p>Note that this method will not throw an exception even when the underlying mapping for the
45    * {@code countryCallingCode} does not exist, instead it will return {@code false}
46    */
isGeoEntity(int countryCallingCode)47   public static boolean isGeoEntity(int countryCallingCode) {
48     List<String> regionCodesForCountryCallingCode =
49         CountryCodeToRegionCodeMap.getCountryCodeToRegionCodeMap().get(countryCallingCode);
50 
51     return regionCodesForCountryCallingCode != null
52         && !regionCodesForCountryCallingCode.contains(REGION_CODE_FOR_NON_GEO_ENTITIES);
53   }
54 
GeoEntityUtility()55   private GeoEntityUtility() {}
56 }
57