2 * Copyright (C) 2013 The Libphonenumber Authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.i18n.phonenumbers;
19 import com.google.i18n.phonenumbers.PhoneNumberUtil;
20 import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberType;
21 import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
22 import com.google.i18n.phonenumbers.prefixmapper.PrefixFileReader;
24 import java.util.Locale;
27 * A phone prefix mapper which provides carrier information related to a phone number.
29 * @author Cecilia Roes
31 public class PhoneNumberToCarrierMapper {
32 private static PhoneNumberToCarrierMapper instance = null;
33 private static final String MAPPING_DATA_DIRECTORY =
34 "/com/google/i18n/phonenumbers/carrier/data/";
35 private PrefixFileReader prefixFileReader = null;
37 private final PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
40 PhoneNumberToCarrierMapper(String phonePrefixDataDirectory) {
41 prefixFileReader = new PrefixFileReader(phonePrefixDataDirectory);
45 * Gets a {@link PhoneNumberToCarrierMapper} instance to carry out international carrier lookup.
47 * <p> The {@link PhoneNumberToCarrierMapper} is implemented as a singleton. Therefore, calling
48 * this method multiple times will only result in one instance being created.
50 * @return a {@link PhoneNumberToCarrierMapper} instance
52 public static synchronized PhoneNumberToCarrierMapper getInstance() {
53 if (instance == null) {
54 instance = new PhoneNumberToCarrierMapper(MAPPING_DATA_DIRECTORY);
60 * Returns a text description for the given phone number, in the language provided. The
61 * description consists of the name of the carrier the number was originally allocated to, however
62 * if the country supports mobile number portability the number might not belong to the returned
63 * carrier anymore. If no mapping is found an empty string is returned.
65 * <p>This method assumes the validity of the number passed in has already been checked, and that
66 * the number is suitable for carrier lookup. We consider mobile and pager numbers possible
67 * candidates for carrier lookup.
69 * @param number a valid phone number for which we want to get a text description
70 * @param languageCode the language code for which the description should be written
71 * @return a text description for the given language code for the given phone number
73 public String getDescriptionForValidNumber(PhoneNumber number, Locale languageCode) {
74 String langStr = languageCode.getLanguage();
75 String scriptStr = ""; // No script is specified
76 String regionStr = languageCode.getCountry();
78 return prefixFileReader.getDescriptionForNumber(number, langStr, scriptStr, regionStr);
82 * As per {@link #getDescriptionForValidNumber(PhoneNumber, Locale)} but explicitly checks
83 * the validity of the number passed in.
85 * @param number the phone number for which we want to get a text description
86 * @param languageCode the language code for which the description should be written
87 * @return a text description for the given language code for the given phone number, or empty
88 * string if the number passed in is invalid
90 public String getDescriptionForNumber(PhoneNumber number, Locale languageCode) {
91 PhoneNumberType numberType = phoneUtil.getNumberType(number);
92 if (isMobile(numberType)) {
93 return getDescriptionForValidNumber(number, languageCode);
99 * Checks if the supplied number type supports carrier lookup.
101 private boolean isMobile(PhoneNumberType numberType) {
102 return (numberType == PhoneNumberType.MOBILE ||
103 numberType == PhoneNumberType.FIXED_LINE_OR_MOBILE ||
104 numberType == PhoneNumberType.PAGER);