1/*
2 * Copyright (C) 2009 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
17package com.google.i18n.phonenumbers;
18
19import com.google.i18n.phonenumbers.Phonemetadata.NumberFormat;
20import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata;
21import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadataCollection;
22import com.google.i18n.phonenumbers.Phonemetadata.PhoneNumberDesc;
23import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
24import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber.CountryCodeSource;
25
26import java.io.IOException;
27import java.io.InputStream;
28import java.io.ObjectInput;
29import java.io.ObjectInputStream;
30import java.util.ArrayList;
31import java.util.Arrays;
32import java.util.Collections;
33import java.util.HashMap;
34import java.util.HashSet;
35import java.util.Iterator;
36import java.util.List;
37import java.util.Map;
38import java.util.Set;
39import java.util.logging.Level;
40import java.util.logging.Logger;
41import java.util.regex.Matcher;
42import java.util.regex.Pattern;
43
44/**
45 * Utility for international phone numbers. Functionality includes formatting, parsing and
46 * validation.
47 *
48 * <p>If you use this library, and want to be notified about important changes, please sign up to
49 * our <a href="http://groups.google.com/group/libphonenumber-discuss/about">mailing list</a>.
50 *
51 * NOTE: A lot of methods in this class require Region Code strings. These must be provided using
52 * ISO 3166-1 two-letter country-code format. These should be in upper-case. The list of the codes
53 * can be found here:
54 * http://www.iso.org/iso/country_codes/iso_3166_code_lists/country_names_and_code_elements.htm
55 *
56 * @author Shaopeng Jia
57 */
58public class PhoneNumberUtil {
59  // @VisibleForTesting
60  static final MetadataLoader DEFAULT_METADATA_LOADER = new MetadataLoader() {
61    @Override
62    public InputStream loadMetadata(String metadataFileName) {
63      return PhoneNumberUtil.class.getResourceAsStream(metadataFileName);
64    }
65  };
66
67  private static final Logger logger = Logger.getLogger(PhoneNumberUtil.class.getName());
68
69  /** Flags to use when compiling regular expressions for phone numbers. */
70  static final int REGEX_FLAGS = Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE;
71  // The minimum and maximum length of the national significant number.
72  private static final int MIN_LENGTH_FOR_NSN = 2;
73  // The ITU says the maximum length should be 15, but we have found longer numbers in Germany.
74  static final int MAX_LENGTH_FOR_NSN = 17;
75  // The maximum length of the country calling code.
76  static final int MAX_LENGTH_COUNTRY_CODE = 3;
77  // We don't allow input strings for parsing to be longer than 250 chars. This prevents malicious
78  // input from overflowing the regular-expression engine.
79  private static final int MAX_INPUT_STRING_LENGTH = 250;
80
81  private static final String META_DATA_FILE_PREFIX =
82      "/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto";
83
84  // Region-code for the unknown region.
85  private static final String UNKNOWN_REGION = "ZZ";
86
87  private static final int NANPA_COUNTRY_CODE = 1;
88
89  // The prefix that needs to be inserted in front of a Colombian landline number when dialed from
90  // a mobile phone in Colombia.
91  private static final String COLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX = "3";
92
93  // Map of country calling codes that use a mobile token before the area code. One example of when
94  // this is relevant is when determining the length of the national destination code, which should
95  // be the length of the area code plus the length of the mobile token.
96  private static final Map<Integer, String> MOBILE_TOKEN_MAPPINGS;
97
98  // The PLUS_SIGN signifies the international prefix.
99  static final char PLUS_SIGN = '+';
100
101  private static final char STAR_SIGN = '*';
102
103  private static final String RFC3966_EXTN_PREFIX = ";ext=";
104  private static final String RFC3966_PREFIX = "tel:";
105  private static final String RFC3966_PHONE_CONTEXT = ";phone-context=";
106  private static final String RFC3966_ISDN_SUBADDRESS = ";isub=";
107
108  // A map that contains characters that are essential when dialling. That means any of the
109  // characters in this map must not be removed from a number when dialling, otherwise the call
110  // will not reach the intended destination.
111  private static final Map<Character, Character> DIALLABLE_CHAR_MAPPINGS;
112
113  // Only upper-case variants of alpha characters are stored.
114  private static final Map<Character, Character> ALPHA_MAPPINGS;
115
116  // For performance reasons, amalgamate both into one map.
117  private static final Map<Character, Character> ALPHA_PHONE_MAPPINGS;
118
119  // Separate map of all symbols that we wish to retain when formatting alpha numbers. This
120  // includes digits, ASCII letters and number grouping symbols such as "-" and " ".
121  private static final Map<Character, Character> ALL_PLUS_NUMBER_GROUPING_SYMBOLS;
122
123  static {
124    HashMap<Integer, String> mobileTokenMap = new HashMap<Integer, String>();
125    mobileTokenMap.put(52, "1");
126    mobileTokenMap.put(54, "9");
127    MOBILE_TOKEN_MAPPINGS = Collections.unmodifiableMap(mobileTokenMap);
128
129    // Simple ASCII digits map used to populate ALPHA_PHONE_MAPPINGS and
130    // ALL_PLUS_NUMBER_GROUPING_SYMBOLS.
131    HashMap<Character, Character> asciiDigitMappings = new HashMap<Character, Character>();
132    asciiDigitMappings.put('0', '0');
133    asciiDigitMappings.put('1', '1');
134    asciiDigitMappings.put('2', '2');
135    asciiDigitMappings.put('3', '3');
136    asciiDigitMappings.put('4', '4');
137    asciiDigitMappings.put('5', '5');
138    asciiDigitMappings.put('6', '6');
139    asciiDigitMappings.put('7', '7');
140    asciiDigitMappings.put('8', '8');
141    asciiDigitMappings.put('9', '9');
142
143    HashMap<Character, Character> alphaMap = new HashMap<Character, Character>(40);
144    alphaMap.put('A', '2');
145    alphaMap.put('B', '2');
146    alphaMap.put('C', '2');
147    alphaMap.put('D', '3');
148    alphaMap.put('E', '3');
149    alphaMap.put('F', '3');
150    alphaMap.put('G', '4');
151    alphaMap.put('H', '4');
152    alphaMap.put('I', '4');
153    alphaMap.put('J', '5');
154    alphaMap.put('K', '5');
155    alphaMap.put('L', '5');
156    alphaMap.put('M', '6');
157    alphaMap.put('N', '6');
158    alphaMap.put('O', '6');
159    alphaMap.put('P', '7');
160    alphaMap.put('Q', '7');
161    alphaMap.put('R', '7');
162    alphaMap.put('S', '7');
163    alphaMap.put('T', '8');
164    alphaMap.put('U', '8');
165    alphaMap.put('V', '8');
166    alphaMap.put('W', '9');
167    alphaMap.put('X', '9');
168    alphaMap.put('Y', '9');
169    alphaMap.put('Z', '9');
170    ALPHA_MAPPINGS = Collections.unmodifiableMap(alphaMap);
171
172    HashMap<Character, Character> combinedMap = new HashMap<Character, Character>(100);
173    combinedMap.putAll(ALPHA_MAPPINGS);
174    combinedMap.putAll(asciiDigitMappings);
175    ALPHA_PHONE_MAPPINGS = Collections.unmodifiableMap(combinedMap);
176
177    HashMap<Character, Character> diallableCharMap = new HashMap<Character, Character>();
178    diallableCharMap.putAll(asciiDigitMappings);
179    diallableCharMap.put(PLUS_SIGN, PLUS_SIGN);
180    diallableCharMap.put('*', '*');
181    DIALLABLE_CHAR_MAPPINGS = Collections.unmodifiableMap(diallableCharMap);
182
183    HashMap<Character, Character> allPlusNumberGroupings = new HashMap<Character, Character>();
184    // Put (lower letter -> upper letter) and (upper letter -> upper letter) mappings.
185    for (char c : ALPHA_MAPPINGS.keySet()) {
186      allPlusNumberGroupings.put(Character.toLowerCase(c), c);
187      allPlusNumberGroupings.put(c, c);
188    }
189    allPlusNumberGroupings.putAll(asciiDigitMappings);
190    // Put grouping symbols.
191    allPlusNumberGroupings.put('-', '-');
192    allPlusNumberGroupings.put('\uFF0D', '-');
193    allPlusNumberGroupings.put('\u2010', '-');
194    allPlusNumberGroupings.put('\u2011', '-');
195    allPlusNumberGroupings.put('\u2012', '-');
196    allPlusNumberGroupings.put('\u2013', '-');
197    allPlusNumberGroupings.put('\u2014', '-');
198    allPlusNumberGroupings.put('\u2015', '-');
199    allPlusNumberGroupings.put('\u2212', '-');
200    allPlusNumberGroupings.put('/', '/');
201    allPlusNumberGroupings.put('\uFF0F', '/');
202    allPlusNumberGroupings.put(' ', ' ');
203    allPlusNumberGroupings.put('\u3000', ' ');
204    allPlusNumberGroupings.put('\u2060', ' ');
205    allPlusNumberGroupings.put('.', '.');
206    allPlusNumberGroupings.put('\uFF0E', '.');
207    ALL_PLUS_NUMBER_GROUPING_SYMBOLS = Collections.unmodifiableMap(allPlusNumberGroupings);
208  }
209
210  // Pattern that makes it easy to distinguish whether a region has a unique international dialing
211  // prefix or not. If a region has a unique international prefix (e.g. 011 in USA), it will be
212  // represented as a string that contains a sequence of ASCII digits. If there are multiple
213  // available international prefixes in a region, they will be represented as a regex string that
214  // always contains character(s) other than ASCII digits.
215  // Note this regex also includes tilde, which signals waiting for the tone.
216  private static final Pattern UNIQUE_INTERNATIONAL_PREFIX =
217      Pattern.compile("[\\d]+(?:[~\u2053\u223C\uFF5E][\\d]+)?");
218
219  // Regular expression of acceptable punctuation found in phone numbers. This excludes punctuation
220  // found as a leading character only.
221  // This consists of dash characters, white space characters, full stops, slashes,
222  // square brackets, parentheses and tildes. It also includes the letter 'x' as that is found as a
223  // placeholder for carrier information in some phone numbers. Full-width variants are also
224  // present.
225  static final String VALID_PUNCTUATION = "-x\u2010-\u2015\u2212\u30FC\uFF0D-\uFF0F " +
226      "\u00A0\u00AD\u200B\u2060\u3000()\uFF08\uFF09\uFF3B\uFF3D.\\[\\]/~\u2053\u223C\uFF5E";
227
228  private static final String DIGITS = "\\p{Nd}";
229  // We accept alpha characters in phone numbers, ASCII only, upper and lower case.
230  private static final String VALID_ALPHA =
231      Arrays.toString(ALPHA_MAPPINGS.keySet().toArray()).replaceAll("[, \\[\\]]", "") +
232      Arrays.toString(ALPHA_MAPPINGS.keySet().toArray()).toLowerCase().replaceAll("[, \\[\\]]", "");
233  static final String PLUS_CHARS = "+\uFF0B";
234  static final Pattern PLUS_CHARS_PATTERN = Pattern.compile("[" + PLUS_CHARS + "]+");
235  private static final Pattern SEPARATOR_PATTERN = Pattern.compile("[" + VALID_PUNCTUATION + "]+");
236  private static final Pattern CAPTURING_DIGIT_PATTERN = Pattern.compile("(" + DIGITS + ")");
237
238  // Regular expression of acceptable characters that may start a phone number for the purposes of
239  // parsing. This allows us to strip away meaningless prefixes to phone numbers that may be
240  // mistakenly given to us. This consists of digits, the plus symbol and arabic-indic digits. This
241  // does not contain alpha characters, although they may be used later in the number. It also does
242  // not include other punctuation, as this will be stripped later during parsing and is of no
243  // information value when parsing a number.
244  private static final String VALID_START_CHAR = "[" + PLUS_CHARS + DIGITS + "]";
245  private static final Pattern VALID_START_CHAR_PATTERN = Pattern.compile(VALID_START_CHAR);
246
247  // Regular expression of characters typically used to start a second phone number for the purposes
248  // of parsing. This allows us to strip off parts of the number that are actually the start of
249  // another number, such as for: (530) 583-6985 x302/x2303 -> the second extension here makes this
250  // actually two phone numbers, (530) 583-6985 x302 and (530) 583-6985 x2303. We remove the second
251  // extension so that the first number is parsed correctly.
252  private static final String SECOND_NUMBER_START = "[\\\\/] *x";
253  static final Pattern SECOND_NUMBER_START_PATTERN = Pattern.compile(SECOND_NUMBER_START);
254
255  // Regular expression of trailing characters that we want to remove. We remove all characters that
256  // are not alpha or numerical characters. The hash character is retained here, as it may signify
257  // the previous block was an extension.
258  private static final String UNWANTED_END_CHARS = "[[\\P{N}&&\\P{L}]&&[^#]]+$";
259  static final Pattern UNWANTED_END_CHAR_PATTERN = Pattern.compile(UNWANTED_END_CHARS);
260
261  // We use this pattern to check if the phone number has at least three letters in it - if so, then
262  // we treat it as a number where some phone-number digits are represented by letters.
263  private static final Pattern VALID_ALPHA_PHONE_PATTERN = Pattern.compile("(?:.*?[A-Za-z]){3}.*");
264
265  // Regular expression of viable phone numbers. This is location independent. Checks we have at
266  // least three leading digits, and only valid punctuation, alpha characters and
267  // digits in the phone number. Does not include extension data.
268  // The symbol 'x' is allowed here as valid punctuation since it is often used as a placeholder for
269  // carrier codes, for example in Brazilian phone numbers. We also allow multiple "+" characters at
270  // the start.
271  // Corresponds to the following:
272  // [digits]{minLengthNsn}|
273  // plus_sign*(([punctuation]|[star])*[digits]){3,}([punctuation]|[star]|[digits]|[alpha])*
274  //
275  // The first reg-ex is to allow short numbers (two digits long) to be parsed if they are entered
276  // as "15" etc, but only if there is no punctuation in them. The second expression restricts the
277  // number of digits to three or more, but then allows them to be in international form, and to
278  // have alpha-characters and punctuation.
279  //
280  // Note VALID_PUNCTUATION starts with a -, so must be the first in the range.
281  private static final String VALID_PHONE_NUMBER =
282      DIGITS + "{" + MIN_LENGTH_FOR_NSN + "}" + "|" +
283      "[" + PLUS_CHARS + "]*+(?:[" + VALID_PUNCTUATION + STAR_SIGN + "]*" + DIGITS + "){3,}[" +
284      VALID_PUNCTUATION + STAR_SIGN + VALID_ALPHA + DIGITS + "]*";
285
286  // Default extension prefix to use when formatting. This will be put in front of any extension
287  // component of the number, after the main national number is formatted. For example, if you wish
288  // the default extension formatting to be " extn: 3456", then you should specify " extn: " here
289  // as the default extension prefix. This can be overridden by region-specific preferences.
290  private static final String DEFAULT_EXTN_PREFIX = " ext. ";
291
292  // Pattern to capture digits used in an extension. Places a maximum length of "7" for an
293  // extension.
294  private static final String CAPTURING_EXTN_DIGITS = "(" + DIGITS + "{1,7})";
295  // Regexp of all possible ways to write extensions, for use when parsing. This will be run as a
296  // case-insensitive regexp match. Wide character versions are also provided after each ASCII
297  // version.
298  private static final String EXTN_PATTERNS_FOR_PARSING;
299  static final String EXTN_PATTERNS_FOR_MATCHING;
300  static {
301    // One-character symbols that can be used to indicate an extension.
302    String singleExtnSymbolsForMatching = "x\uFF58#\uFF03~\uFF5E";
303    // For parsing, we are slightly more lenient in our interpretation than for matching. Here we
304    // allow a "comma" as a possible extension indicator. When matching, this is hardly ever used to
305    // indicate this.
306    String singleExtnSymbolsForParsing = "," + singleExtnSymbolsForMatching;
307
308    EXTN_PATTERNS_FOR_PARSING = createExtnPattern(singleExtnSymbolsForParsing);
309    EXTN_PATTERNS_FOR_MATCHING = createExtnPattern(singleExtnSymbolsForMatching);
310  }
311
312  /**
313   * Helper initialiser method to create the regular-expression pattern to match extensions,
314   * allowing the one-char extension symbols provided by {@code singleExtnSymbols}.
315   */
316  private static String createExtnPattern(String singleExtnSymbols) {
317    // There are three regular expressions here. The first covers RFC 3966 format, where the
318    // extension is added using ";ext=". The second more generic one starts with optional white
319    // space and ends with an optional full stop (.), followed by zero or more spaces/tabs and then
320    // the numbers themselves. The other one covers the special case of American numbers where the
321    // extension is written with a hash at the end, such as "- 503#".
322    // Note that the only capturing groups should be around the digits that you want to capture as
323    // part of the extension, or else parsing will fail!
324    // Canonical-equivalence doesn't seem to be an option with Android java, so we allow two options
325    // for representing the accented o - the character itself, and one in the unicode decomposed
326    // form with the combining acute accent.
327    return (RFC3966_EXTN_PREFIX + CAPTURING_EXTN_DIGITS + "|" + "[ \u00A0\\t,]*" +
328            "(?:e?xt(?:ensi(?:o\u0301?|\u00F3))?n?|\uFF45?\uFF58\uFF54\uFF4E?|" +
329            "[" + singleExtnSymbols + "]|int|anexo|\uFF49\uFF4E\uFF54)" +
330            "[:\\.\uFF0E]?[ \u00A0\\t,-]*" + CAPTURING_EXTN_DIGITS + "#?|" +
331            "[- ]+(" + DIGITS + "{1,5})#");
332  }
333
334  // Regexp of all known extension prefixes used by different regions followed by 1 or more valid
335  // digits, for use when parsing.
336  private static final Pattern EXTN_PATTERN =
337      Pattern.compile("(?:" + EXTN_PATTERNS_FOR_PARSING + ")$", REGEX_FLAGS);
338
339  // We append optionally the extension pattern to the end here, as a valid phone number may
340  // have an extension prefix appended, followed by 1 or more digits.
341  private static final Pattern VALID_PHONE_NUMBER_PATTERN =
342      Pattern.compile(VALID_PHONE_NUMBER + "(?:" + EXTN_PATTERNS_FOR_PARSING + ")?", REGEX_FLAGS);
343
344  static final Pattern NON_DIGITS_PATTERN = Pattern.compile("(\\D+)");
345
346  // The FIRST_GROUP_PATTERN was originally set to $1 but there are some countries for which the
347  // first group is not used in the national pattern (e.g. Argentina) so the $1 group does not match
348  // correctly.  Therefore, we use \d, so that the first group actually used in the pattern will be
349  // matched.
350  private static final Pattern FIRST_GROUP_PATTERN = Pattern.compile("(\\$\\d)");
351  private static final Pattern NP_PATTERN = Pattern.compile("\\$NP");
352  private static final Pattern FG_PATTERN = Pattern.compile("\\$FG");
353  private static final Pattern CC_PATTERN = Pattern.compile("\\$CC");
354
355  // A pattern that is used to determine if the national prefix formatting rule has the first group
356  // only, i.e., does not start with the national prefix. Note that the pattern explicitly allows
357  // for unbalanced parentheses.
358  private static final Pattern FIRST_GROUP_ONLY_PREFIX_PATTERN = Pattern.compile("\\(?\\$1\\)?");
359
360  private static PhoneNumberUtil instance = null;
361
362  public static final String REGION_CODE_FOR_NON_GEO_ENTITY = "001";
363
364  /**
365   * INTERNATIONAL and NATIONAL formats are consistent with the definition in ITU-T Recommendation
366   * E123. For example, the number of the Google Switzerland office will be written as
367   * "+41 44 668 1800" in INTERNATIONAL format, and as "044 668 1800" in NATIONAL format.
368   * E164 format is as per INTERNATIONAL format but with no formatting applied, e.g.
369   * "+41446681800". RFC3966 is as per INTERNATIONAL format, but with all spaces and other
370   * separating symbols replaced with a hyphen, and with any phone number extension appended with
371   * ";ext=". It also will have a prefix of "tel:" added, e.g. "tel:+41-44-668-1800".
372   *
373   * Note: If you are considering storing the number in a neutral format, you are highly advised to
374   * use the PhoneNumber class.
375   */
376  public enum PhoneNumberFormat {
377    E164,
378    INTERNATIONAL,
379    NATIONAL,
380    RFC3966
381  }
382
383  /**
384   * Type of phone numbers.
385   */
386  public enum PhoneNumberType {
387    FIXED_LINE,
388    MOBILE,
389    // In some regions (e.g. the USA), it is impossible to distinguish between fixed-line and
390    // mobile numbers by looking at the phone number itself.
391    FIXED_LINE_OR_MOBILE,
392    // Freephone lines
393    TOLL_FREE,
394    PREMIUM_RATE,
395    // The cost of this call is shared between the caller and the recipient, and is hence typically
396    // less than PREMIUM_RATE calls. See // http://en.wikipedia.org/wiki/Shared_Cost_Service for
397    // more information.
398    SHARED_COST,
399    // Voice over IP numbers. This includes TSoIP (Telephony Service over IP).
400    VOIP,
401    // A personal number is associated with a particular person, and may be routed to either a
402    // MOBILE or FIXED_LINE number. Some more information can be found here:
403    // http://en.wikipedia.org/wiki/Personal_Numbers
404    PERSONAL_NUMBER,
405    PAGER,
406    // Used for "Universal Access Numbers" or "Company Numbers". They may be further routed to
407    // specific offices, but allow one number to be used for a company.
408    UAN,
409    // Used for "Voice Mail Access Numbers".
410    VOICEMAIL,
411    // A phone number is of type UNKNOWN when it does not fit any of the known patterns for a
412    // specific region.
413    UNKNOWN
414  }
415
416  /**
417   * Types of phone number matches. See detailed description beside the isNumberMatch() method.
418   */
419  public enum MatchType {
420    NOT_A_NUMBER,
421    NO_MATCH,
422    SHORT_NSN_MATCH,
423    NSN_MATCH,
424    EXACT_MATCH,
425  }
426
427  /**
428   * Possible outcomes when testing if a PhoneNumber is possible.
429   */
430  public enum ValidationResult {
431    IS_POSSIBLE,
432    INVALID_COUNTRY_CODE,
433    TOO_SHORT,
434    TOO_LONG,
435  }
436
437  /**
438   * Leniency when {@linkplain PhoneNumberUtil#findNumbers finding} potential phone numbers in text
439   * segments. The levels here are ordered in increasing strictness.
440   */
441  public enum Leniency {
442    /**
443     * Phone numbers accepted are {@linkplain PhoneNumberUtil#isPossibleNumber(PhoneNumber)
444     * possible}, but not necessarily {@linkplain PhoneNumberUtil#isValidNumber(PhoneNumber) valid}.
445     */
446    POSSIBLE {
447      @Override
448      boolean verify(PhoneNumber number, String candidate, PhoneNumberUtil util) {
449        return util.isPossibleNumber(number);
450      }
451    },
452    /**
453     * Phone numbers accepted are {@linkplain PhoneNumberUtil#isPossibleNumber(PhoneNumber)
454     * possible} and {@linkplain PhoneNumberUtil#isValidNumber(PhoneNumber) valid}. Numbers written
455     * in national format must have their national-prefix present if it is usually written for a
456     * number of this type.
457     */
458    VALID {
459      @Override
460      boolean verify(PhoneNumber number, String candidate, PhoneNumberUtil util) {
461        if (!util.isValidNumber(number) ||
462            !PhoneNumberMatcher.containsOnlyValidXChars(number, candidate, util)) {
463          return false;
464        }
465        return PhoneNumberMatcher.isNationalPrefixPresentIfRequired(number, util);
466      }
467    },
468    /**
469     * Phone numbers accepted are {@linkplain PhoneNumberUtil#isValidNumber(PhoneNumber) valid} and
470     * are grouped in a possible way for this locale. For example, a US number written as
471     * "65 02 53 00 00" and "650253 0000" are not accepted at this leniency level, whereas
472     * "650 253 0000", "650 2530000" or "6502530000" are.
473     * Numbers with more than one '/' symbol in the national significant number are also dropped at
474     * this level.
475     * <p>
476     * Warning: This level might result in lower coverage especially for regions outside of country
477     * code "+1". If you are not sure about which level to use, email the discussion group
478     * libphonenumber-discuss@googlegroups.com.
479     */
480    STRICT_GROUPING {
481      @Override
482      boolean verify(PhoneNumber number, String candidate, PhoneNumberUtil util) {
483        if (!util.isValidNumber(number) ||
484            !PhoneNumberMatcher.containsOnlyValidXChars(number, candidate, util) ||
485            PhoneNumberMatcher.containsMoreThanOneSlashInNationalNumber(number, candidate) ||
486            !PhoneNumberMatcher.isNationalPrefixPresentIfRequired(number, util)) {
487          return false;
488        }
489        return PhoneNumberMatcher.checkNumberGroupingIsValid(
490            number, candidate, util, new PhoneNumberMatcher.NumberGroupingChecker() {
491              @Override
492              public boolean checkGroups(PhoneNumberUtil util, PhoneNumber number,
493                                         StringBuilder normalizedCandidate,
494                                         String[] expectedNumberGroups) {
495                return PhoneNumberMatcher.allNumberGroupsRemainGrouped(
496                    util, number, normalizedCandidate, expectedNumberGroups);
497              }
498            });
499      }
500    },
501    /**
502     * Phone numbers accepted are {@linkplain PhoneNumberUtil#isValidNumber(PhoneNumber) valid} and
503     * are grouped in the same way that we would have formatted it, or as a single block. For
504     * example, a US number written as "650 2530000" is not accepted at this leniency level, whereas
505     * "650 253 0000" or "6502530000" are.
506     * Numbers with more than one '/' symbol are also dropped at this level.
507     * <p>
508     * Warning: This level might result in lower coverage especially for regions outside of country
509     * code "+1". If you are not sure about which level to use, email the discussion group
510     * libphonenumber-discuss@googlegroups.com.
511     */
512    EXACT_GROUPING {
513      @Override
514      boolean verify(PhoneNumber number, String candidate, PhoneNumberUtil util) {
515        if (!util.isValidNumber(number) ||
516            !PhoneNumberMatcher.containsOnlyValidXChars(number, candidate, util) ||
517            PhoneNumberMatcher.containsMoreThanOneSlashInNationalNumber(number, candidate) ||
518            !PhoneNumberMatcher.isNationalPrefixPresentIfRequired(number, util)) {
519          return false;
520        }
521        return PhoneNumberMatcher.checkNumberGroupingIsValid(
522            number, candidate, util, new PhoneNumberMatcher.NumberGroupingChecker() {
523              @Override
524              public boolean checkGroups(PhoneNumberUtil util, PhoneNumber number,
525                                         StringBuilder normalizedCandidate,
526                                         String[] expectedNumberGroups) {
527                return PhoneNumberMatcher.allNumberGroupsAreExactlyPresent(
528                    util, number, normalizedCandidate, expectedNumberGroups);
529              }
530            });
531      }
532    };
533
534    /** Returns true if {@code number} is a verified number according to this leniency. */
535    abstract boolean verify(PhoneNumber number, String candidate, PhoneNumberUtil util);
536  }
537
538  // A mapping from a country calling code to the region codes which denote the region represented
539  // by that country calling code. In the case of multiple regions sharing a calling code, such as
540  // the NANPA regions, the one indicated with "isMainCountryForCode" in the metadata should be
541  // first.
542  private final Map<Integer, List<String>> countryCallingCodeToRegionCodeMap;
543
544  // The set of regions that share country calling code 1.
545  // There are roughly 26 regions.
546  // We set the initial capacity of the HashSet to 35 to offer a load factor of roughly 0.75.
547  private final Set<String> nanpaRegions = new HashSet<String>(35);
548
549  // A mapping from a region code to the PhoneMetadata for that region.
550  // Note: Synchronization, though only needed for the Android version of the library, is used in
551  // all versions for consistency.
552  private final Map<String, PhoneMetadata> regionToMetadataMap =
553      Collections.synchronizedMap(new HashMap<String, PhoneMetadata>());
554
555  // A mapping from a country calling code for a non-geographical entity to the PhoneMetadata for
556  // that country calling code. Examples of the country calling codes include 800 (International
557  // Toll Free Service) and 808 (International Shared Cost Service).
558  // Note: Synchronization, though only needed for the Android version of the library, is used in
559  // all versions for consistency.
560  private final Map<Integer, PhoneMetadata> countryCodeToNonGeographicalMetadataMap =
561      Collections.synchronizedMap(new HashMap<Integer, PhoneMetadata>());
562
563  // A cache for frequently used region-specific regular expressions.
564  // The initial capacity is set to 100 as this seems to be an optimal value for Android, based on
565  // performance measurements.
566  private final RegexCache regexCache = new RegexCache(100);
567
568  // The set of regions the library supports.
569  // There are roughly 240 of them and we set the initial capacity of the HashSet to 320 to offer a
570  // load factor of roughly 0.75.
571  private final Set<String> supportedRegions = new HashSet<String>(320);
572
573  // The set of county calling codes that map to the non-geo entity region ("001"). This set
574  // currently contains < 12 elements so the default capacity of 16 (load factor=0.75) is fine.
575  private final Set<Integer> countryCodesForNonGeographicalRegion = new HashSet<Integer>();
576
577  // The prefix of the metadata files from which region data is loaded.
578  private final String currentFilePrefix;
579  // The metadata loader used to inject alternative metadata sources.
580  private final MetadataLoader metadataLoader;
581
582  /**
583   * This class implements a singleton, the constructor is only visible to facilitate testing.
584   */
585  // @VisibleForTesting
586  PhoneNumberUtil(String filePrefix, MetadataLoader metadataLoader,
587      Map<Integer, List<String>> countryCallingCodeToRegionCodeMap) {
588    this.currentFilePrefix = filePrefix;
589    this.metadataLoader = metadataLoader;
590    this.countryCallingCodeToRegionCodeMap = countryCallingCodeToRegionCodeMap;
591    for (Map.Entry<Integer, List<String>> entry : countryCallingCodeToRegionCodeMap.entrySet()) {
592      List<String> regionCodes = entry.getValue();
593      // We can assume that if the county calling code maps to the non-geo entity region code then
594      // that's the only region code it maps to.
595      if (regionCodes.size() == 1 && REGION_CODE_FOR_NON_GEO_ENTITY.equals(regionCodes.get(0))) {
596        // This is the subset of all country codes that map to the non-geo entity region code.
597        countryCodesForNonGeographicalRegion.add(entry.getKey());
598      } else {
599        // The supported regions set does not include the "001" non-geo entity region code.
600        supportedRegions.addAll(regionCodes);
601      }
602    }
603    // If the non-geo entity still got added to the set of supported regions it must be because
604    // there are entries that list the non-geo entity alongside normal regions (which is wrong).
605    // If we discover this, remove the non-geo entity from the set of supported regions and log.
606    if (supportedRegions.remove(REGION_CODE_FOR_NON_GEO_ENTITY)) {
607      logger.log(Level.WARNING, "invalid metadata " +
608          "(country calling code was mapped to the non-geo entity as well as specific region(s))");
609    }
610    nanpaRegions.addAll(countryCallingCodeToRegionCodeMap.get(NANPA_COUNTRY_CODE));
611  }
612
613  // @VisibleForTesting
614  void loadMetadataFromFile(String filePrefix, String regionCode, int countryCallingCode,
615      MetadataLoader metadataLoader) {
616    boolean isNonGeoRegion = REGION_CODE_FOR_NON_GEO_ENTITY.equals(regionCode);
617    String fileName = filePrefix + "_" +
618        (isNonGeoRegion ? String.valueOf(countryCallingCode) : regionCode);
619    InputStream source = metadataLoader.loadMetadata(fileName);
620    if (source == null) {
621      logger.log(Level.SEVERE, "missing metadata: " + fileName);
622      throw new IllegalStateException("missing metadata: " + fileName);
623    }
624    ObjectInputStream in = null;
625    try {
626      in = new ObjectInputStream(source);
627      PhoneMetadataCollection metadataCollection = loadMetadataAndCloseInput(in);
628      List<PhoneMetadata> metadataList = metadataCollection.getMetadataList();
629      if (metadataList.isEmpty()) {
630        logger.log(Level.SEVERE, "empty metadata: " + fileName);
631        throw new IllegalStateException("empty metadata: " + fileName);
632      }
633      if (metadataList.size() > 1) {
634        logger.log(Level.WARNING, "invalid metadata (too many entries): " + fileName);
635      }
636      PhoneMetadata metadata = metadataList.get(0);
637      if (isNonGeoRegion) {
638        countryCodeToNonGeographicalMetadataMap.put(countryCallingCode, metadata);
639      } else {
640        regionToMetadataMap.put(regionCode, metadata);
641      }
642    } catch (IOException e) {
643      logger.log(Level.SEVERE, "cannot load/parse metadata: " + fileName, e);
644      throw new RuntimeException("cannot load/parse metadata: " + fileName, e);
645    }
646  }
647
648  /**
649   * Loads the metadata protocol buffer from the given stream and closes the stream afterwards. Any
650   * exceptions that occur while reading the stream are propagated (though exceptions that occur
651   * when the stream is closed will be ignored).
652   *
653   * @param source  the non-null stream from which metadata is to be read.
654   * @return        the loaded metadata protocol buffer.
655   */
656  private static PhoneMetadataCollection loadMetadataAndCloseInput(ObjectInputStream source) {
657    PhoneMetadataCollection metadataCollection = new PhoneMetadataCollection();
658    try {
659      metadataCollection.readExternal(source);
660    } catch (IOException e) {
661      logger.log(Level.WARNING, "error reading input (ignored)", e);
662    } finally {
663      try {
664        source.close();
665      } catch (IOException e) {
666        logger.log(Level.WARNING, "error closing input stream (ignored)", e);
667      } finally {
668        return metadataCollection;
669      }
670    }
671  }
672
673  /**
674   * Attempts to extract a possible number from the string passed in. This currently strips all
675   * leading characters that cannot be used to start a phone number. Characters that can be used to
676   * start a phone number are defined in the VALID_START_CHAR_PATTERN. If none of these characters
677   * are found in the number passed in, an empty string is returned. This function also attempts to
678   * strip off any alternative extensions or endings if two or more are present, such as in the case
679   * of: (530) 583-6985 x302/x2303. The second extension here makes this actually two phone numbers,
680   * (530) 583-6985 x302 and (530) 583-6985 x2303. We remove the second extension so that the first
681   * number is parsed correctly.
682   *
683   * @param number  the string that might contain a phone number
684   * @return        the number, stripped of any non-phone-number prefix (such as "Tel:") or an empty
685   *                string if no character used to start phone numbers (such as + or any digit) is
686   *                found in the number
687   */
688  static String extractPossibleNumber(String number) {
689    Matcher m = VALID_START_CHAR_PATTERN.matcher(number);
690    if (m.find()) {
691      number = number.substring(m.start());
692      // Remove trailing non-alpha non-numerical characters.
693      Matcher trailingCharsMatcher = UNWANTED_END_CHAR_PATTERN.matcher(number);
694      if (trailingCharsMatcher.find()) {
695        number = number.substring(0, trailingCharsMatcher.start());
696        logger.log(Level.FINER, "Stripped trailing characters: " + number);
697      }
698      // Check for extra numbers at the end.
699      Matcher secondNumber = SECOND_NUMBER_START_PATTERN.matcher(number);
700      if (secondNumber.find()) {
701        number = number.substring(0, secondNumber.start());
702      }
703      return number;
704    } else {
705      return "";
706    }
707  }
708
709  /**
710   * Checks to see if the string of characters could possibly be a phone number at all. At the
711   * moment, checks to see that the string begins with at least 2 digits, ignoring any punctuation
712   * commonly found in phone numbers.
713   * This method does not require the number to be normalized in advance - but does assume that
714   * leading non-number symbols have been removed, such as by the method extractPossibleNumber.
715   *
716   * @param number  string to be checked for viability as a phone number
717   * @return        true if the number could be a phone number of some sort, otherwise false
718   */
719  // @VisibleForTesting
720  static boolean isViablePhoneNumber(String number) {
721    if (number.length() < MIN_LENGTH_FOR_NSN) {
722      return false;
723    }
724    Matcher m = VALID_PHONE_NUMBER_PATTERN.matcher(number);
725    return m.matches();
726  }
727
728  /**
729   * Normalizes a string of characters representing a phone number. This performs the following
730   * conversions:
731   *   Punctuation is stripped.
732   *   For ALPHA/VANITY numbers:
733   *   Letters are converted to their numeric representation on a telephone keypad. The keypad
734   *       used here is the one defined in ITU Recommendation E.161. This is only done if there are
735   *       3 or more letters in the number, to lessen the risk that such letters are typos.
736   *   For other numbers:
737   *   Wide-ascii digits are converted to normal ASCII (European) digits.
738   *   Arabic-Indic numerals are converted to European numerals.
739   *   Spurious alpha characters are stripped.
740   *
741   * @param number  a string of characters representing a phone number
742   * @return        the normalized string version of the phone number
743   */
744  static String normalize(String number) {
745    Matcher m = VALID_ALPHA_PHONE_PATTERN.matcher(number);
746    if (m.matches()) {
747      return normalizeHelper(number, ALPHA_PHONE_MAPPINGS, true);
748    } else {
749      return normalizeDigitsOnly(number);
750    }
751  }
752
753  /**
754   * Normalizes a string of characters representing a phone number. This is a wrapper for
755   * normalize(String number) but does in-place normalization of the StringBuilder provided.
756   *
757   * @param number  a StringBuilder of characters representing a phone number that will be
758   *     normalized in place
759   */
760  static void normalize(StringBuilder number) {
761    String normalizedNumber = normalize(number.toString());
762    number.replace(0, number.length(), normalizedNumber);
763  }
764
765  /**
766   * Normalizes a string of characters representing a phone number. This converts wide-ascii and
767   * arabic-indic numerals to European numerals, and strips punctuation and alpha characters.
768   *
769   * @param number  a string of characters representing a phone number
770   * @return        the normalized string version of the phone number
771   */
772  public static String normalizeDigitsOnly(String number) {
773    return normalizeDigits(number, false /* strip non-digits */).toString();
774  }
775
776  static StringBuilder normalizeDigits(String number, boolean keepNonDigits) {
777    StringBuilder normalizedDigits = new StringBuilder(number.length());
778    for (char c : number.toCharArray()) {
779      int digit = Character.digit(c, 10);
780      if (digit != -1) {
781        normalizedDigits.append(digit);
782      } else if (keepNonDigits) {
783        normalizedDigits.append(c);
784      }
785    }
786    return normalizedDigits;
787  }
788
789  /**
790   * Normalizes a string of characters representing a phone number. This strips all characters which
791   * are not diallable on a mobile phone keypad (including all non-ASCII digits).
792   *
793   * @param number  a string of characters representing a phone number
794   * @return        the normalized string version of the phone number
795   */
796  static String normalizeDiallableCharsOnly(String number) {
797    return normalizeHelper(number, DIALLABLE_CHAR_MAPPINGS, true /* remove non matches */);
798  }
799
800  /**
801   * Converts all alpha characters in a number to their respective digits on a keypad, but retains
802   * existing formatting.
803   */
804  public static String convertAlphaCharactersInNumber(String number) {
805    return normalizeHelper(number, ALPHA_PHONE_MAPPINGS, false);
806  }
807
808  /**
809   * Gets the length of the geographical area code from the
810   * PhoneNumber object passed in, so that clients could use it
811   * to split a national significant number into geographical area code and subscriber number. It
812   * works in such a way that the resultant subscriber number should be diallable, at least on some
813   * devices. An example of how this could be used:
814   *
815   * <pre>
816   * PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
817   * PhoneNumber number = phoneUtil.parse("16502530000", "US");
818   * String nationalSignificantNumber = phoneUtil.getNationalSignificantNumber(number);
819   * String areaCode;
820   * String subscriberNumber;
821   *
822   * int areaCodeLength = phoneUtil.getLengthOfGeographicalAreaCode(number);
823   * if (areaCodeLength > 0) {
824   *   areaCode = nationalSignificantNumber.substring(0, areaCodeLength);
825   *   subscriberNumber = nationalSignificantNumber.substring(areaCodeLength);
826   * } else {
827   *   areaCode = "";
828   *   subscriberNumber = nationalSignificantNumber;
829   * }
830   * </pre>
831   *
832   * N.B.: area code is a very ambiguous concept, so the I18N team generally recommends against
833   * using it for most purposes, but recommends using the more general {@code national_number}
834   * instead. Read the following carefully before deciding to use this method:
835   * <ul>
836   *  <li> geographical area codes change over time, and this method honors those changes;
837   *    therefore, it doesn't guarantee the stability of the result it produces.
838   *  <li> subscriber numbers may not be diallable from all devices (notably mobile devices, which
839   *    typically requires the full national_number to be dialled in most regions).
840   *  <li> most non-geographical numbers have no area codes, including numbers from non-geographical
841   *    entities
842   *  <li> some geographical numbers have no area codes.
843   * </ul>
844   * @param number  the PhoneNumber object for which clients
845   *     want to know the length of the area code.
846   * @return  the length of area code of the PhoneNumber object
847   *     passed in.
848   */
849  public int getLengthOfGeographicalAreaCode(PhoneNumber number) {
850    PhoneMetadata metadata = getMetadataForRegion(getRegionCodeForNumber(number));
851    if (metadata == null) {
852      return 0;
853    }
854    // If a country doesn't use a national prefix, and this number doesn't have an Italian leading
855    // zero, we assume it is a closed dialling plan with no area codes.
856    if (!metadata.hasNationalPrefix() && !number.isItalianLeadingZero()) {
857      return 0;
858    }
859
860    if (!isNumberGeographical(number)) {
861      return 0;
862    }
863
864    return getLengthOfNationalDestinationCode(number);
865  }
866
867  /**
868   * Gets the length of the national destination code (NDC) from the
869   * PhoneNumber object passed in, so that clients could use it
870   * to split a national significant number into NDC and subscriber number. The NDC of a phone
871   * number is normally the first group of digit(s) right after the country calling code when the
872   * number is formatted in the international format, if there is a subscriber number part that
873   * follows. An example of how this could be used:
874   *
875   * <pre>
876   * PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
877   * PhoneNumber number = phoneUtil.parse("18002530000", "US");
878   * String nationalSignificantNumber = phoneUtil.getNationalSignificantNumber(number);
879   * String nationalDestinationCode;
880   * String subscriberNumber;
881   *
882   * int nationalDestinationCodeLength = phoneUtil.getLengthOfNationalDestinationCode(number);
883   * if (nationalDestinationCodeLength > 0) {
884   *   nationalDestinationCode = nationalSignificantNumber.substring(0,
885   *       nationalDestinationCodeLength);
886   *   subscriberNumber = nationalSignificantNumber.substring(nationalDestinationCodeLength);
887   * } else {
888   *   nationalDestinationCode = "";
889   *   subscriberNumber = nationalSignificantNumber;
890   * }
891   * </pre>
892   *
893   * Refer to the unittests to see the difference between this function and
894   * {@link #getLengthOfGeographicalAreaCode}.
895   *
896   * @param number  the PhoneNumber object for which clients
897   *     want to know the length of the NDC.
898   * @return  the length of NDC of the PhoneNumber object
899   *     passed in.
900   */
901  public int getLengthOfNationalDestinationCode(PhoneNumber number) {
902    PhoneNumber copiedProto;
903    if (number.hasExtension()) {
904      // We don't want to alter the proto given to us, but we don't want to include the extension
905      // when we format it, so we copy it and clear the extension here.
906      copiedProto = new PhoneNumber();
907      copiedProto.mergeFrom(number);
908      copiedProto.clearExtension();
909    } else {
910      copiedProto = number;
911    }
912
913    String nationalSignificantNumber = format(copiedProto,
914                                              PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL);
915    String[] numberGroups = NON_DIGITS_PATTERN.split(nationalSignificantNumber);
916    // The pattern will start with "+COUNTRY_CODE " so the first group will always be the empty
917    // string (before the + symbol) and the second group will be the country calling code. The third
918    // group will be area code if it is not the last group.
919    if (numberGroups.length <= 3) {
920      return 0;
921    }
922
923    if (getNumberType(number) == PhoneNumberType.MOBILE) {
924      // For example Argentinian mobile numbers, when formatted in the international format, are in
925      // the form of +54 9 NDC XXXX.... As a result, we take the length of the third group (NDC) and
926      // add the length of the second group (which is the mobile token), which also forms part of
927      // the national significant number. This assumes that the mobile token is always formatted
928      // separately from the rest of the phone number.
929      String mobileToken = getCountryMobileToken(number.getCountryCode());
930      if (!mobileToken.equals("")) {
931        return numberGroups[2].length() + numberGroups[3].length();
932      }
933    }
934    return numberGroups[2].length();
935  }
936
937  /**
938   * Returns the mobile token for the provided country calling code if it has one, otherwise
939   * returns an empty string. A mobile token is a number inserted before the area code when dialing
940   * a mobile number from that country from abroad.
941   *
942   * @param countryCallingCode  the country calling code for which we want the mobile token
943   * @return  the mobile token, as a string, for the given country calling code
944   */
945  public static String getCountryMobileToken(int countryCallingCode) {
946    if (MOBILE_TOKEN_MAPPINGS.containsKey(countryCallingCode)) {
947      return MOBILE_TOKEN_MAPPINGS.get(countryCallingCode);
948    }
949    return "";
950  }
951
952  /**
953   * Normalizes a string of characters representing a phone number by replacing all characters found
954   * in the accompanying map with the values therein, and stripping all other characters if
955   * removeNonMatches is true.
956   *
957   * @param number                     a string of characters representing a phone number
958   * @param normalizationReplacements  a mapping of characters to what they should be replaced by in
959   *                                   the normalized version of the phone number
960   * @param removeNonMatches           indicates whether characters that are not able to be replaced
961   *                                   should be stripped from the number. If this is false, they
962   *                                   will be left unchanged in the number.
963   * @return  the normalized string version of the phone number
964   */
965  private static String normalizeHelper(String number,
966                                        Map<Character, Character> normalizationReplacements,
967                                        boolean removeNonMatches) {
968    StringBuilder normalizedNumber = new StringBuilder(number.length());
969    for (int i = 0; i < number.length(); i++) {
970      char character = number.charAt(i);
971      Character newDigit = normalizationReplacements.get(Character.toUpperCase(character));
972      if (newDigit != null) {
973        normalizedNumber.append(newDigit);
974      } else if (!removeNonMatches) {
975        normalizedNumber.append(character);
976      }
977      // If neither of the above are true, we remove this character.
978    }
979    return normalizedNumber.toString();
980  }
981
982  /**
983   * Sets or resets the PhoneNumberUtil singleton instance. If set to null, the next call to
984   * {@code getInstance()} will load (and return) the default instance.
985   */
986  // @VisibleForTesting
987  static synchronized void setInstance(PhoneNumberUtil util) {
988    instance = util;
989  }
990
991  /**
992   * Convenience method to get a list of what regions the library has metadata for.
993   */
994  public Set<String> getSupportedRegions() {
995    return Collections.unmodifiableSet(supportedRegions);
996  }
997
998  /**
999   * Convenience method to get a list of what global network calling codes the library has metadata
1000   * for.
1001   */
1002  public Set<Integer> getSupportedGlobalNetworkCallingCodes() {
1003    return Collections.unmodifiableSet(countryCodesForNonGeographicalRegion);
1004  }
1005
1006  /**
1007   * Gets a {@link PhoneNumberUtil} instance to carry out international phone number formatting,
1008   * parsing, or validation. The instance is loaded with phone number metadata for a number of most
1009   * commonly used regions.
1010   *
1011   * <p>The {@link PhoneNumberUtil} is implemented as a singleton. Therefore, calling getInstance
1012   * multiple times will only result in one instance being created.
1013   *
1014   * @return a PhoneNumberUtil instance
1015   */
1016  public static synchronized PhoneNumberUtil getInstance() {
1017    if (instance == null) {
1018      setInstance(createInstance(DEFAULT_METADATA_LOADER));
1019    }
1020    return instance;
1021  }
1022
1023  /**
1024   * Create a new {@link PhoneNumberUtil} instance to carry out international phone number
1025   * formatting, parsing, or validation. The instance is loaded with all metadata by
1026   * using the metadataLoader specified.
1027   *
1028   * This method should only be used in the rare case in which you want to manage your own
1029   * metadata loading. Calling this method multiple times is very expensive, as each time
1030   * a new instance is created from scratch. When in doubt, use {@link #getInstance}.
1031   *
1032   * @param metadataLoader Customized metadata loader. If null, default metadata loader will
1033   *     be used. This should not be null.
1034   * @return a PhoneNumberUtil instance
1035   */
1036  public static PhoneNumberUtil createInstance(MetadataLoader metadataLoader) {
1037    if (metadataLoader == null) {
1038      throw new IllegalArgumentException("metadataLoader could not be null.");
1039    }
1040    return new PhoneNumberUtil(META_DATA_FILE_PREFIX, metadataLoader,
1041        CountryCodeToRegionCodeMap.getCountryCodeToRegionCodeMap());
1042  }
1043
1044  /**
1045   * Helper function to check if the national prefix formatting rule has the first group only, i.e.,
1046   * does not start with the national prefix.
1047   */
1048  static boolean formattingRuleHasFirstGroupOnly(String nationalPrefixFormattingRule) {
1049    return nationalPrefixFormattingRule.length() == 0 ||
1050        FIRST_GROUP_ONLY_PREFIX_PATTERN.matcher(nationalPrefixFormattingRule).matches();
1051  }
1052
1053  /**
1054   * Tests whether a phone number has a geographical association. It checks if the number is
1055   * associated to a certain region in the country where it belongs to. Note that this doesn't
1056   * verify if the number is actually in use.
1057   *
1058   * A similar method is implemented as PhoneNumberOfflineGeocoder.canBeGeocoded, which performs a
1059   * looser check, since it only prevents cases where prefixes overlap for geocodable and
1060   * non-geocodable numbers. Also, if new phone number types were added, we should check if this
1061   * other method should be updated too.
1062   */
1063  boolean isNumberGeographical(PhoneNumber phoneNumber) {
1064    PhoneNumberType numberType = getNumberType(phoneNumber);
1065    // TODO: Include mobile phone numbers from countries like Indonesia, which has some
1066    // mobile numbers that are geographical.
1067    return numberType == PhoneNumberType.FIXED_LINE ||
1068        numberType == PhoneNumberType.FIXED_LINE_OR_MOBILE;
1069  }
1070
1071  /**
1072   * Helper function to check region code is not unknown or null.
1073   */
1074  private boolean isValidRegionCode(String regionCode) {
1075    return regionCode != null && supportedRegions.contains(regionCode);
1076  }
1077
1078  /**
1079   * Helper function to check the country calling code is valid.
1080   */
1081  private boolean hasValidCountryCallingCode(int countryCallingCode) {
1082    return countryCallingCodeToRegionCodeMap.containsKey(countryCallingCode);
1083  }
1084
1085  /**
1086   * Formats a phone number in the specified format using default rules. Note that this does not
1087   * promise to produce a phone number that the user can dial from where they are - although we do
1088   * format in either 'national' or 'international' format depending on what the client asks for, we
1089   * do not currently support a more abbreviated format, such as for users in the same "area" who
1090   * could potentially dial the number without area code. Note that if the phone number has a
1091   * country calling code of 0 or an otherwise invalid country calling code, we cannot work out
1092   * which formatting rules to apply so we return the national significant number with no formatting
1093   * applied.
1094   *
1095   * @param number         the phone number to be formatted
1096   * @param numberFormat   the format the phone number should be formatted into
1097   * @return  the formatted phone number
1098   */
1099  public String format(PhoneNumber number, PhoneNumberFormat numberFormat) {
1100    if (number.getNationalNumber() == 0 && number.hasRawInput()) {
1101      // Unparseable numbers that kept their raw input just use that.
1102      // This is the only case where a number can be formatted as E164 without a
1103      // leading '+' symbol (but the original number wasn't parseable anyway).
1104      // TODO: Consider removing the 'if' above so that unparseable
1105      // strings without raw input format to the empty string instead of "+00"
1106      String rawInput = number.getRawInput();
1107      if (rawInput.length() > 0) {
1108        return rawInput;
1109      }
1110    }
1111    StringBuilder formattedNumber = new StringBuilder(20);
1112    format(number, numberFormat, formattedNumber);
1113    return formattedNumber.toString();
1114  }
1115
1116  /**
1117   * Same as {@link #format(PhoneNumber, PhoneNumberFormat)}, but accepts a mutable StringBuilder as
1118   * a parameter to decrease object creation when invoked many times.
1119   */
1120  public void format(PhoneNumber number, PhoneNumberFormat numberFormat,
1121                     StringBuilder formattedNumber) {
1122    // Clear the StringBuilder first.
1123    formattedNumber.setLength(0);
1124    int countryCallingCode = number.getCountryCode();
1125    String nationalSignificantNumber = getNationalSignificantNumber(number);
1126
1127    if (numberFormat == PhoneNumberFormat.E164) {
1128      // Early exit for E164 case (even if the country calling code is invalid) since no formatting
1129      // of the national number needs to be applied. Extensions are not formatted.
1130      formattedNumber.append(nationalSignificantNumber);
1131      prefixNumberWithCountryCallingCode(countryCallingCode, PhoneNumberFormat.E164,
1132                                         formattedNumber);
1133      return;
1134    }
1135    if (!hasValidCountryCallingCode(countryCallingCode)) {
1136      formattedNumber.append(nationalSignificantNumber);
1137      return;
1138    }
1139    // Note getRegionCodeForCountryCode() is used because formatting information for regions which
1140    // share a country calling code is contained by only one region for performance reasons. For
1141    // example, for NANPA regions it will be contained in the metadata for US.
1142    String regionCode = getRegionCodeForCountryCode(countryCallingCode);
1143    // Metadata cannot be null because the country calling code is valid (which means that the
1144    // region code cannot be ZZ and must be one of our supported region codes).
1145    PhoneMetadata metadata =
1146        getMetadataForRegionOrCallingCode(countryCallingCode, regionCode);
1147    formattedNumber.append(formatNsn(nationalSignificantNumber, metadata, numberFormat));
1148    maybeAppendFormattedExtension(number, metadata, numberFormat, formattedNumber);
1149    prefixNumberWithCountryCallingCode(countryCallingCode, numberFormat, formattedNumber);
1150  }
1151
1152  /**
1153   * Formats a phone number in the specified format using client-defined formatting rules. Note that
1154   * if the phone number has a country calling code of zero or an otherwise invalid country calling
1155   * code, we cannot work out things like whether there should be a national prefix applied, or how
1156   * to format extensions, so we return the national significant number with no formatting applied.
1157   *
1158   * @param number                        the phone number to be formatted
1159   * @param numberFormat                  the format the phone number should be formatted into
1160   * @param userDefinedFormats            formatting rules specified by clients
1161   * @return  the formatted phone number
1162   */
1163  public String formatByPattern(PhoneNumber number,
1164                                PhoneNumberFormat numberFormat,
1165                                List<NumberFormat> userDefinedFormats) {
1166    int countryCallingCode = number.getCountryCode();
1167    String nationalSignificantNumber = getNationalSignificantNumber(number);
1168    if (!hasValidCountryCallingCode(countryCallingCode)) {
1169      return nationalSignificantNumber;
1170    }
1171    // Note getRegionCodeForCountryCode() is used because formatting information for regions which
1172    // share a country calling code is contained by only one region for performance reasons. For
1173    // example, for NANPA regions it will be contained in the metadata for US.
1174    String regionCode = getRegionCodeForCountryCode(countryCallingCode);
1175    // Metadata cannot be null because the country calling code is valid
1176    PhoneMetadata metadata =
1177        getMetadataForRegionOrCallingCode(countryCallingCode, regionCode);
1178
1179    StringBuilder formattedNumber = new StringBuilder(20);
1180
1181    NumberFormat formattingPattern =
1182        chooseFormattingPatternForNumber(userDefinedFormats, nationalSignificantNumber);
1183    if (formattingPattern == null) {
1184      // If no pattern above is matched, we format the number as a whole.
1185      formattedNumber.append(nationalSignificantNumber);
1186    } else {
1187      NumberFormat numFormatCopy = new NumberFormat();
1188      // Before we do a replacement of the national prefix pattern $NP with the national prefix, we
1189      // need to copy the rule so that subsequent replacements for different numbers have the
1190      // appropriate national prefix.
1191      numFormatCopy.mergeFrom(formattingPattern);
1192      String nationalPrefixFormattingRule = formattingPattern.getNationalPrefixFormattingRule();
1193      if (nationalPrefixFormattingRule.length() > 0) {
1194        String nationalPrefix = metadata.getNationalPrefix();
1195        if (nationalPrefix.length() > 0) {
1196          // Replace $NP with national prefix and $FG with the first group ($1).
1197          nationalPrefixFormattingRule =
1198              NP_PATTERN.matcher(nationalPrefixFormattingRule).replaceFirst(nationalPrefix);
1199          nationalPrefixFormattingRule =
1200              FG_PATTERN.matcher(nationalPrefixFormattingRule).replaceFirst("\\$1");
1201          numFormatCopy.setNationalPrefixFormattingRule(nationalPrefixFormattingRule);
1202        } else {
1203          // We don't want to have a rule for how to format the national prefix if there isn't one.
1204          numFormatCopy.clearNationalPrefixFormattingRule();
1205        }
1206      }
1207      formattedNumber.append(
1208          formatNsnUsingPattern(nationalSignificantNumber, numFormatCopy, numberFormat));
1209    }
1210    maybeAppendFormattedExtension(number, metadata, numberFormat, formattedNumber);
1211    prefixNumberWithCountryCallingCode(countryCallingCode, numberFormat, formattedNumber);
1212    return formattedNumber.toString();
1213  }
1214
1215  /**
1216   * Formats a phone number in national format for dialing using the carrier as specified in the
1217   * {@code carrierCode}. The {@code carrierCode} will always be used regardless of whether the
1218   * phone number already has a preferred domestic carrier code stored. If {@code carrierCode}
1219   * contains an empty string, returns the number in national format without any carrier code.
1220   *
1221   * @param number  the phone number to be formatted
1222   * @param carrierCode  the carrier selection code to be used
1223   * @return  the formatted phone number in national format for dialing using the carrier as
1224   *          specified in the {@code carrierCode}
1225   */
1226  public String formatNationalNumberWithCarrierCode(PhoneNumber number, String carrierCode) {
1227    int countryCallingCode = number.getCountryCode();
1228    String nationalSignificantNumber = getNationalSignificantNumber(number);
1229    if (!hasValidCountryCallingCode(countryCallingCode)) {
1230      return nationalSignificantNumber;
1231    }
1232
1233    // Note getRegionCodeForCountryCode() is used because formatting information for regions which
1234    // share a country calling code is contained by only one region for performance reasons. For
1235    // example, for NANPA regions it will be contained in the metadata for US.
1236    String regionCode = getRegionCodeForCountryCode(countryCallingCode);
1237    // Metadata cannot be null because the country calling code is valid.
1238    PhoneMetadata metadata = getMetadataForRegionOrCallingCode(countryCallingCode, regionCode);
1239
1240    StringBuilder formattedNumber = new StringBuilder(20);
1241    formattedNumber.append(formatNsn(nationalSignificantNumber, metadata,
1242                                     PhoneNumberFormat.NATIONAL, carrierCode));
1243    maybeAppendFormattedExtension(number, metadata, PhoneNumberFormat.NATIONAL, formattedNumber);
1244    prefixNumberWithCountryCallingCode(countryCallingCode, PhoneNumberFormat.NATIONAL,
1245                                       formattedNumber);
1246    return formattedNumber.toString();
1247  }
1248
1249  private PhoneMetadata getMetadataForRegionOrCallingCode(
1250      int countryCallingCode, String regionCode) {
1251    return REGION_CODE_FOR_NON_GEO_ENTITY.equals(regionCode)
1252        ? getMetadataForNonGeographicalRegion(countryCallingCode)
1253        : getMetadataForRegion(regionCode);
1254  }
1255
1256  /**
1257   * Formats a phone number in national format for dialing using the carrier as specified in the
1258   * preferredDomesticCarrierCode field of the PhoneNumber object passed in. If that is missing,
1259   * use the {@code fallbackCarrierCode} passed in instead. If there is no
1260   * {@code preferredDomesticCarrierCode}, and the {@code fallbackCarrierCode} contains an empty
1261   * string, return the number in national format without any carrier code.
1262   *
1263   * <p>Use {@link #formatNationalNumberWithCarrierCode} instead if the carrier code passed in
1264   * should take precedence over the number's {@code preferredDomesticCarrierCode} when formatting.
1265   *
1266   * @param number  the phone number to be formatted
1267   * @param fallbackCarrierCode  the carrier selection code to be used, if none is found in the
1268   *     phone number itself
1269   * @return  the formatted phone number in national format for dialing using the number's
1270   *     {@code preferredDomesticCarrierCode}, or the {@code fallbackCarrierCode} passed in if
1271   *     none is found
1272   */
1273  public String formatNationalNumberWithPreferredCarrierCode(PhoneNumber number,
1274                                                             String fallbackCarrierCode) {
1275    return formatNationalNumberWithCarrierCode(number, number.hasPreferredDomesticCarrierCode()
1276                                                       ? number.getPreferredDomesticCarrierCode()
1277                                                       : fallbackCarrierCode);
1278  }
1279
1280  /**
1281   * Returns a number formatted in such a way that it can be dialed from a mobile phone in a
1282   * specific region. If the number cannot be reached from the region (e.g. some countries block
1283   * toll-free numbers from being called outside of the country), the method returns an empty
1284   * string.
1285   *
1286   * @param number  the phone number to be formatted
1287   * @param regionCallingFrom  the region where the call is being placed
1288   * @param withFormatting  whether the number should be returned with formatting symbols, such as
1289   *     spaces and dashes.
1290   * @return  the formatted phone number
1291   */
1292  public String formatNumberForMobileDialing(PhoneNumber number, String regionCallingFrom,
1293                                             boolean withFormatting) {
1294    int countryCallingCode = number.getCountryCode();
1295    if (!hasValidCountryCallingCode(countryCallingCode)) {
1296      return number.hasRawInput() ? number.getRawInput() : "";
1297    }
1298
1299    String formattedNumber = "";
1300    // Clear the extension, as that part cannot normally be dialed together with the main number.
1301    PhoneNumber numberNoExt = new PhoneNumber().mergeFrom(number).clearExtension();
1302    String regionCode = getRegionCodeForCountryCode(countryCallingCode);
1303    PhoneNumberType numberType = getNumberType(numberNoExt);
1304    boolean isValidNumber = (numberType != PhoneNumberType.UNKNOWN);
1305    if (regionCallingFrom.equals(regionCode)) {
1306      boolean isFixedLineOrMobile =
1307          (numberType == PhoneNumberType.FIXED_LINE) || (numberType == PhoneNumberType.MOBILE) ||
1308          (numberType == PhoneNumberType.FIXED_LINE_OR_MOBILE);
1309      // Carrier codes may be needed in some countries. We handle this here.
1310      if (regionCode.equals("CO") && numberType == PhoneNumberType.FIXED_LINE) {
1311        formattedNumber =
1312            formatNationalNumberWithCarrierCode(numberNoExt, COLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX);
1313      } else if (regionCode.equals("BR") && isFixedLineOrMobile) {
1314        formattedNumber = numberNoExt.hasPreferredDomesticCarrierCode()
1315            ? formattedNumber = formatNationalNumberWithPreferredCarrierCode(numberNoExt, "")
1316            // Brazilian fixed line and mobile numbers need to be dialed with a carrier code when
1317            // called within Brazil. Without that, most of the carriers won't connect the call.
1318            // Because of that, we return an empty string here.
1319            : "";
1320      } else if (isValidNumber && regionCode.equals("HU")) {
1321        // The national format for HU numbers doesn't contain the national prefix, because that is
1322        // how numbers are normally written down. However, the national prefix is obligatory when
1323        // dialing from a mobile phone, except for short numbers. As a result, we add it back here
1324        // if it is a valid regular length phone number.
1325        formattedNumber =
1326            getNddPrefixForRegion(regionCode, true /* strip non-digits */) +
1327            " " + format(numberNoExt, PhoneNumberFormat.NATIONAL);
1328      } else if (countryCallingCode == NANPA_COUNTRY_CODE) {
1329        // For NANPA countries, we output international format for numbers that can be dialed
1330        // internationally, since that always works, except for numbers which might potentially be
1331        // short numbers, which are always dialled in national format.
1332        PhoneMetadata regionMetadata = getMetadataForRegion(regionCallingFrom);
1333        if (canBeInternationallyDialled(numberNoExt) &&
1334            !isShorterThanPossibleNormalNumber(regionMetadata,
1335                getNationalSignificantNumber(numberNoExt))) {
1336          formattedNumber = format(numberNoExt, PhoneNumberFormat.INTERNATIONAL);
1337        } else {
1338          formattedNumber = format(numberNoExt, PhoneNumberFormat.NATIONAL);
1339        }
1340      } else {
1341        // For non-geographical countries, and Mexican and Chilean fixed line and mobile numbers, we
1342        // output international format for numbers that can be dialed internationally as that always
1343        // works.
1344        if ((regionCode.equals(REGION_CODE_FOR_NON_GEO_ENTITY) ||
1345            // MX fixed line and mobile numbers should always be formatted in international format,
1346            // even when dialed within MX. For national format to work, a carrier code needs to be
1347            // used, and the correct carrier code depends on if the caller and callee are from the
1348            // same local area. It is trickier to get that to work correctly than using
1349            // international format, which is tested to work fine on all carriers.
1350            // CL fixed line numbers need the national prefix when dialing in the national format,
1351            // but don't have it when used for display. The reverse is true for mobile numbers.
1352            // As a result, we output them in the international format to make it work.
1353            ((regionCode.equals("MX") || regionCode.equals("CL")) &&
1354             isFixedLineOrMobile)) &&
1355            canBeInternationallyDialled(numberNoExt)) {
1356          formattedNumber = format(numberNoExt, PhoneNumberFormat.INTERNATIONAL);
1357        } else {
1358          formattedNumber = format(numberNoExt, PhoneNumberFormat.NATIONAL);
1359        }
1360      }
1361    } else if (isValidNumber && canBeInternationallyDialled(numberNoExt)) {
1362      // We assume that short numbers are not diallable from outside their region, so if a number
1363      // is not a valid regular length phone number, we treat it as if it cannot be internationally
1364      // dialled.
1365      return withFormatting ? format(numberNoExt, PhoneNumberFormat.INTERNATIONAL)
1366                            : format(numberNoExt, PhoneNumberFormat.E164);
1367    }
1368    return withFormatting ? formattedNumber
1369                          : normalizeDiallableCharsOnly(formattedNumber);
1370  }
1371
1372  /**
1373   * Formats a phone number for out-of-country dialing purposes. If no regionCallingFrom is
1374   * supplied, we format the number in its INTERNATIONAL format. If the country calling code is the
1375   * same as that of the region where the number is from, then NATIONAL formatting will be applied.
1376   *
1377   * <p>If the number itself has a country calling code of zero or an otherwise invalid country
1378   * calling code, then we return the number with no formatting applied.
1379   *
1380   * <p>Note this function takes care of the case for calling inside of NANPA and between Russia and
1381   * Kazakhstan (who share the same country calling code). In those cases, no international prefix
1382   * is used. For regions which have multiple international prefixes, the number in its
1383   * INTERNATIONAL format will be returned instead.
1384   *
1385   * @param number               the phone number to be formatted
1386   * @param regionCallingFrom    the region where the call is being placed
1387   * @return  the formatted phone number
1388   */
1389  public String formatOutOfCountryCallingNumber(PhoneNumber number,
1390                                                String regionCallingFrom) {
1391    if (!isValidRegionCode(regionCallingFrom)) {
1392      logger.log(Level.WARNING,
1393                 "Trying to format number from invalid region "
1394                 + regionCallingFrom
1395                 + ". International formatting applied.");
1396      return format(number, PhoneNumberFormat.INTERNATIONAL);
1397    }
1398    int countryCallingCode = number.getCountryCode();
1399    String nationalSignificantNumber = getNationalSignificantNumber(number);
1400    if (!hasValidCountryCallingCode(countryCallingCode)) {
1401      return nationalSignificantNumber;
1402    }
1403    if (countryCallingCode == NANPA_COUNTRY_CODE) {
1404      if (isNANPACountry(regionCallingFrom)) {
1405        // For NANPA regions, return the national format for these regions but prefix it with the
1406        // country calling code.
1407        return countryCallingCode + " " + format(number, PhoneNumberFormat.NATIONAL);
1408      }
1409    } else if (countryCallingCode == getCountryCodeForValidRegion(regionCallingFrom)) {
1410      // If regions share a country calling code, the country calling code need not be dialled.
1411      // This also applies when dialling within a region, so this if clause covers both these cases.
1412      // Technically this is the case for dialling from La Reunion to other overseas departments of
1413      // France (French Guiana, Martinique, Guadeloupe), but not vice versa - so we don't cover this
1414      // edge case for now and for those cases return the version including country calling code.
1415      // Details here: http://www.petitfute.com/voyage/225-info-pratiques-reunion
1416      return format(number, PhoneNumberFormat.NATIONAL);
1417    }
1418    // Metadata cannot be null because we checked 'isValidRegionCode()' above.
1419    PhoneMetadata metadataForRegionCallingFrom = getMetadataForRegion(regionCallingFrom);
1420    String internationalPrefix = metadataForRegionCallingFrom.getInternationalPrefix();
1421
1422    // For regions that have multiple international prefixes, the international format of the
1423    // number is returned, unless there is a preferred international prefix.
1424    String internationalPrefixForFormatting = "";
1425    if (UNIQUE_INTERNATIONAL_PREFIX.matcher(internationalPrefix).matches()) {
1426      internationalPrefixForFormatting = internationalPrefix;
1427    } else if (metadataForRegionCallingFrom.hasPreferredInternationalPrefix()) {
1428      internationalPrefixForFormatting =
1429          metadataForRegionCallingFrom.getPreferredInternationalPrefix();
1430    }
1431
1432    String regionCode = getRegionCodeForCountryCode(countryCallingCode);
1433    // Metadata cannot be null because the country calling code is valid.
1434    PhoneMetadata metadataForRegion =
1435        getMetadataForRegionOrCallingCode(countryCallingCode, regionCode);
1436    String formattedNationalNumber =
1437        formatNsn(nationalSignificantNumber, metadataForRegion, PhoneNumberFormat.INTERNATIONAL);
1438    StringBuilder formattedNumber = new StringBuilder(formattedNationalNumber);
1439    maybeAppendFormattedExtension(number, metadataForRegion, PhoneNumberFormat.INTERNATIONAL,
1440                                  formattedNumber);
1441    if (internationalPrefixForFormatting.length() > 0) {
1442      formattedNumber.insert(0, " ").insert(0, countryCallingCode).insert(0, " ")
1443          .insert(0, internationalPrefixForFormatting);
1444    } else {
1445      prefixNumberWithCountryCallingCode(countryCallingCode,
1446                                         PhoneNumberFormat.INTERNATIONAL,
1447                                         formattedNumber);
1448    }
1449    return formattedNumber.toString();
1450  }
1451
1452  /**
1453   * Formats a phone number using the original phone number format that the number is parsed from.
1454   * The original format is embedded in the country_code_source field of the PhoneNumber object
1455   * passed in. If such information is missing, the number will be formatted into the NATIONAL
1456   * format by default. When the number contains a leading zero and this is unexpected for this
1457   * country, or we don't have a formatting pattern for the number, the method returns the raw input
1458   * when it is available.
1459   *
1460   * Note this method guarantees no digit will be inserted, removed or modified as a result of
1461   * formatting.
1462   *
1463   * @param number  the phone number that needs to be formatted in its original number format
1464   * @param regionCallingFrom  the region whose IDD needs to be prefixed if the original number
1465   *     has one
1466   * @return  the formatted phone number in its original number format
1467   */
1468  public String formatInOriginalFormat(PhoneNumber number, String regionCallingFrom) {
1469    if (number.hasRawInput() &&
1470        (hasUnexpectedItalianLeadingZero(number) || !hasFormattingPatternForNumber(number))) {
1471      // We check if we have the formatting pattern because without that, we might format the number
1472      // as a group without national prefix.
1473      return number.getRawInput();
1474    }
1475    if (!number.hasCountryCodeSource()) {
1476      return format(number, PhoneNumberFormat.NATIONAL);
1477    }
1478    String formattedNumber;
1479    switch (number.getCountryCodeSource()) {
1480      case FROM_NUMBER_WITH_PLUS_SIGN:
1481        formattedNumber = format(number, PhoneNumberFormat.INTERNATIONAL);
1482        break;
1483      case FROM_NUMBER_WITH_IDD:
1484        formattedNumber = formatOutOfCountryCallingNumber(number, regionCallingFrom);
1485        break;
1486      case FROM_NUMBER_WITHOUT_PLUS_SIGN:
1487        formattedNumber = format(number, PhoneNumberFormat.INTERNATIONAL).substring(1);
1488        break;
1489      case FROM_DEFAULT_COUNTRY:
1490        // Fall-through to default case.
1491      default:
1492        String regionCode = getRegionCodeForCountryCode(number.getCountryCode());
1493        // We strip non-digits from the NDD here, and from the raw input later, so that we can
1494        // compare them easily.
1495        String nationalPrefix = getNddPrefixForRegion(regionCode, true /* strip non-digits */);
1496        String nationalFormat = format(number, PhoneNumberFormat.NATIONAL);
1497        if (nationalPrefix == null || nationalPrefix.length() == 0) {
1498          // If the region doesn't have a national prefix at all, we can safely return the national
1499          // format without worrying about a national prefix being added.
1500          formattedNumber = nationalFormat;
1501          break;
1502        }
1503        // Otherwise, we check if the original number was entered with a national prefix.
1504        if (rawInputContainsNationalPrefix(
1505            number.getRawInput(), nationalPrefix, regionCode)) {
1506          // If so, we can safely return the national format.
1507          formattedNumber = nationalFormat;
1508          break;
1509        }
1510        // Metadata cannot be null here because getNddPrefixForRegion() (above) returns null if
1511        // there is no metadata for the region.
1512        PhoneMetadata metadata = getMetadataForRegion(regionCode);
1513        String nationalNumber = getNationalSignificantNumber(number);
1514        NumberFormat formatRule =
1515            chooseFormattingPatternForNumber(metadata.numberFormats(), nationalNumber);
1516        // The format rule could still be null here if the national number was 0 and there was no
1517        // raw input (this should not be possible for numbers generated by the phonenumber library
1518        // as they would also not have a country calling code and we would have exited earlier).
1519        if (formatRule == null) {
1520          formattedNumber = nationalFormat;
1521          break;
1522        }
1523        // When the format we apply to this number doesn't contain national prefix, we can just
1524        // return the national format.
1525        // TODO: Refactor the code below with the code in
1526        // isNationalPrefixPresentIfRequired.
1527        String candidateNationalPrefixRule = formatRule.getNationalPrefixFormattingRule();
1528        // We assume that the first-group symbol will never be _before_ the national prefix.
1529        int indexOfFirstGroup = candidateNationalPrefixRule.indexOf("$1");
1530        if (indexOfFirstGroup <= 0) {
1531          formattedNumber = nationalFormat;
1532          break;
1533        }
1534        candidateNationalPrefixRule =
1535            candidateNationalPrefixRule.substring(0, indexOfFirstGroup);
1536        candidateNationalPrefixRule = normalizeDigitsOnly(candidateNationalPrefixRule);
1537        if (candidateNationalPrefixRule.length() == 0) {
1538          // National prefix not used when formatting this number.
1539          formattedNumber = nationalFormat;
1540          break;
1541        }
1542        // Otherwise, we need to remove the national prefix from our output.
1543        NumberFormat numFormatCopy = new NumberFormat();
1544        numFormatCopy.mergeFrom(formatRule);
1545        numFormatCopy.clearNationalPrefixFormattingRule();
1546        List<NumberFormat> numberFormats = new ArrayList<NumberFormat>(1);
1547        numberFormats.add(numFormatCopy);
1548        formattedNumber = formatByPattern(number, PhoneNumberFormat.NATIONAL, numberFormats);
1549        break;
1550    }
1551    String rawInput = number.getRawInput();
1552    // If no digit is inserted/removed/modified as a result of our formatting, we return the
1553    // formatted phone number; otherwise we return the raw input the user entered.
1554    if (formattedNumber != null && rawInput.length() > 0) {
1555      String normalizedFormattedNumber = normalizeDiallableCharsOnly(formattedNumber);
1556      String normalizedRawInput = normalizeDiallableCharsOnly(rawInput);
1557      if (!normalizedFormattedNumber.equals(normalizedRawInput)) {
1558        formattedNumber = rawInput;
1559      }
1560    }
1561    return formattedNumber;
1562  }
1563
1564  // Check if rawInput, which is assumed to be in the national format, has a national prefix. The
1565  // national prefix is assumed to be in digits-only form.
1566  private boolean rawInputContainsNationalPrefix(String rawInput, String nationalPrefix,
1567      String regionCode) {
1568    String normalizedNationalNumber = normalizeDigitsOnly(rawInput);
1569    if (normalizedNationalNumber.startsWith(nationalPrefix)) {
1570      try {
1571        // Some Japanese numbers (e.g. 00777123) might be mistaken to contain the national prefix
1572        // when written without it (e.g. 0777123) if we just do prefix matching. To tackle that, we
1573        // check the validity of the number if the assumed national prefix is removed (777123 won't
1574        // be valid in Japan).
1575        return isValidNumber(
1576            parse(normalizedNationalNumber.substring(nationalPrefix.length()), regionCode));
1577      } catch (NumberParseException e) {
1578        return false;
1579      }
1580    }
1581    return false;
1582  }
1583
1584  /**
1585   * Returns true if a number is from a region whose national significant number couldn't contain a
1586   * leading zero, but has the italian_leading_zero field set to true.
1587   */
1588  private boolean hasUnexpectedItalianLeadingZero(PhoneNumber number) {
1589    return number.isItalianLeadingZero() && !isLeadingZeroPossible(number.getCountryCode());
1590  }
1591
1592  private boolean hasFormattingPatternForNumber(PhoneNumber number) {
1593    int countryCallingCode = number.getCountryCode();
1594    String phoneNumberRegion = getRegionCodeForCountryCode(countryCallingCode);
1595    PhoneMetadata metadata =
1596        getMetadataForRegionOrCallingCode(countryCallingCode, phoneNumberRegion);
1597    if (metadata == null) {
1598      return false;
1599    }
1600    String nationalNumber = getNationalSignificantNumber(number);
1601    NumberFormat formatRule =
1602        chooseFormattingPatternForNumber(metadata.numberFormats(), nationalNumber);
1603    return formatRule != null;
1604  }
1605
1606  /**
1607   * Formats a phone number for out-of-country dialing purposes.
1608   *
1609   * Note that in this version, if the number was entered originally using alpha characters and
1610   * this version of the number is stored in raw_input, this representation of the number will be
1611   * used rather than the digit representation. Grouping information, as specified by characters
1612   * such as "-" and " ", will be retained.
1613   *
1614   * <p><b>Caveats:</b></p>
1615   * <ul>
1616   *  <li> This will not produce good results if the country calling code is both present in the raw
1617   *       input _and_ is the start of the national number. This is not a problem in the regions
1618   *       which typically use alpha numbers.
1619   *  <li> This will also not produce good results if the raw input has any grouping information
1620   *       within the first three digits of the national number, and if the function needs to strip
1621   *       preceding digits/words in the raw input before these digits. Normally people group the
1622   *       first three digits together so this is not a huge problem - and will be fixed if it
1623   *       proves to be so.
1624   * </ul>
1625   *
1626   * @param number  the phone number that needs to be formatted
1627   * @param regionCallingFrom  the region where the call is being placed
1628   * @return  the formatted phone number
1629   */
1630  public String formatOutOfCountryKeepingAlphaChars(PhoneNumber number,
1631                                                    String regionCallingFrom) {
1632    String rawInput = number.getRawInput();
1633    // If there is no raw input, then we can't keep alpha characters because there aren't any.
1634    // In this case, we return formatOutOfCountryCallingNumber.
1635    if (rawInput.length() == 0) {
1636      return formatOutOfCountryCallingNumber(number, regionCallingFrom);
1637    }
1638    int countryCode = number.getCountryCode();
1639    if (!hasValidCountryCallingCode(countryCode)) {
1640      return rawInput;
1641    }
1642    // Strip any prefix such as country calling code, IDD, that was present. We do this by comparing
1643    // the number in raw_input with the parsed number.
1644    // To do this, first we normalize punctuation. We retain number grouping symbols such as " "
1645    // only.
1646    rawInput = normalizeHelper(rawInput, ALL_PLUS_NUMBER_GROUPING_SYMBOLS, true);
1647    // Now we trim everything before the first three digits in the parsed number. We choose three
1648    // because all valid alpha numbers have 3 digits at the start - if it does not, then we don't
1649    // trim anything at all. Similarly, if the national number was less than three digits, we don't
1650    // trim anything at all.
1651    String nationalNumber = getNationalSignificantNumber(number);
1652    if (nationalNumber.length() > 3) {
1653      int firstNationalNumberDigit = rawInput.indexOf(nationalNumber.substring(0, 3));
1654      if (firstNationalNumberDigit != -1) {
1655        rawInput = rawInput.substring(firstNationalNumberDigit);
1656      }
1657    }
1658    PhoneMetadata metadataForRegionCallingFrom = getMetadataForRegion(regionCallingFrom);
1659    if (countryCode == NANPA_COUNTRY_CODE) {
1660      if (isNANPACountry(regionCallingFrom)) {
1661        return countryCode + " " + rawInput;
1662      }
1663    } else if (metadataForRegionCallingFrom != null &&
1664               countryCode == getCountryCodeForValidRegion(regionCallingFrom)) {
1665      NumberFormat formattingPattern =
1666          chooseFormattingPatternForNumber(metadataForRegionCallingFrom.numberFormats(),
1667                                           nationalNumber);
1668      if (formattingPattern == null) {
1669        // If no pattern above is matched, we format the original input.
1670        return rawInput;
1671      }
1672      NumberFormat newFormat = new NumberFormat();
1673      newFormat.mergeFrom(formattingPattern);
1674      // The first group is the first group of digits that the user wrote together.
1675      newFormat.setPattern("(\\d+)(.*)");
1676      // Here we just concatenate them back together after the national prefix has been fixed.
1677      newFormat.setFormat("$1$2");
1678      // Now we format using this pattern instead of the default pattern, but with the national
1679      // prefix prefixed if necessary.
1680      // This will not work in the cases where the pattern (and not the leading digits) decide
1681      // whether a national prefix needs to be used, since we have overridden the pattern to match
1682      // anything, but that is not the case in the metadata to date.
1683      return formatNsnUsingPattern(rawInput, newFormat, PhoneNumberFormat.NATIONAL);
1684    }
1685    String internationalPrefixForFormatting = "";
1686    // If an unsupported region-calling-from is entered, or a country with multiple international
1687    // prefixes, the international format of the number is returned, unless there is a preferred
1688    // international prefix.
1689    if (metadataForRegionCallingFrom != null) {
1690      String internationalPrefix = metadataForRegionCallingFrom.getInternationalPrefix();
1691      internationalPrefixForFormatting =
1692          UNIQUE_INTERNATIONAL_PREFIX.matcher(internationalPrefix).matches()
1693          ? internationalPrefix
1694          : metadataForRegionCallingFrom.getPreferredInternationalPrefix();
1695    }
1696    StringBuilder formattedNumber = new StringBuilder(rawInput);
1697    String regionCode = getRegionCodeForCountryCode(countryCode);
1698    // Metadata cannot be null because the country calling code is valid.
1699    PhoneMetadata metadataForRegion = getMetadataForRegionOrCallingCode(countryCode, regionCode);
1700    maybeAppendFormattedExtension(number, metadataForRegion,
1701                                  PhoneNumberFormat.INTERNATIONAL, formattedNumber);
1702    if (internationalPrefixForFormatting.length() > 0) {
1703      formattedNumber.insert(0, " ").insert(0, countryCode).insert(0, " ")
1704          .insert(0, internationalPrefixForFormatting);
1705    } else {
1706      // Invalid region entered as country-calling-from (so no metadata was found for it) or the
1707      // region chosen has multiple international dialling prefixes.
1708      logger.log(Level.WARNING,
1709                 "Trying to format number from invalid region "
1710                 + regionCallingFrom
1711                 + ". International formatting applied.");
1712      prefixNumberWithCountryCallingCode(countryCode,
1713                                         PhoneNumberFormat.INTERNATIONAL,
1714                                         formattedNumber);
1715    }
1716    return formattedNumber.toString();
1717  }
1718
1719  /**
1720   * Gets the national significant number of the a phone number. Note a national significant number
1721   * doesn't contain a national prefix or any formatting.
1722   *
1723   * @param number  the phone number for which the national significant number is needed
1724   * @return  the national significant number of the PhoneNumber object passed in
1725   */
1726  public String getNationalSignificantNumber(PhoneNumber number) {
1727    // If leading zero(s) have been set, we prefix this now. Note this is not a national prefix.
1728    StringBuilder nationalNumber = new StringBuilder();
1729    if (number.isItalianLeadingZero()) {
1730      char[] zeros = new char[number.getNumberOfLeadingZeros()];
1731      Arrays.fill(zeros, '0');
1732      nationalNumber.append(new String(zeros));
1733    }
1734    nationalNumber.append(number.getNationalNumber());
1735    return nationalNumber.toString();
1736  }
1737
1738  /**
1739   * A helper function that is used by format and formatByPattern.
1740   */
1741  private void prefixNumberWithCountryCallingCode(int countryCallingCode,
1742                                                  PhoneNumberFormat numberFormat,
1743                                                  StringBuilder formattedNumber) {
1744    switch (numberFormat) {
1745      case E164:
1746        formattedNumber.insert(0, countryCallingCode).insert(0, PLUS_SIGN);
1747        return;
1748      case INTERNATIONAL:
1749        formattedNumber.insert(0, " ").insert(0, countryCallingCode).insert(0, PLUS_SIGN);
1750        return;
1751      case RFC3966:
1752        formattedNumber.insert(0, "-").insert(0, countryCallingCode).insert(0, PLUS_SIGN)
1753            .insert(0, RFC3966_PREFIX);
1754        return;
1755      case NATIONAL:
1756      default:
1757        return;
1758    }
1759  }
1760
1761  // Simple wrapper of formatNsn for the common case of no carrier code.
1762  private String formatNsn(String number, PhoneMetadata metadata, PhoneNumberFormat numberFormat) {
1763    return formatNsn(number, metadata, numberFormat, null);
1764  }
1765
1766  // Note in some regions, the national number can be written in two completely different ways
1767  // depending on whether it forms part of the NATIONAL format or INTERNATIONAL format. The
1768  // numberFormat parameter here is used to specify which format to use for those cases. If a
1769  // carrierCode is specified, this will be inserted into the formatted string to replace $CC.
1770  private String formatNsn(String number,
1771                           PhoneMetadata metadata,
1772                           PhoneNumberFormat numberFormat,
1773                           String carrierCode) {
1774    List<NumberFormat> intlNumberFormats = metadata.intlNumberFormats();
1775    // When the intlNumberFormats exists, we use that to format national number for the
1776    // INTERNATIONAL format instead of using the numberDesc.numberFormats.
1777    List<NumberFormat> availableFormats =
1778        (intlNumberFormats.size() == 0 || numberFormat == PhoneNumberFormat.NATIONAL)
1779        ? metadata.numberFormats()
1780        : metadata.intlNumberFormats();
1781    NumberFormat formattingPattern = chooseFormattingPatternForNumber(availableFormats, number);
1782    return (formattingPattern == null)
1783        ? number
1784        : formatNsnUsingPattern(number, formattingPattern, numberFormat, carrierCode);
1785  }
1786
1787  NumberFormat chooseFormattingPatternForNumber(List<NumberFormat> availableFormats,
1788                                                String nationalNumber) {
1789    for (NumberFormat numFormat : availableFormats) {
1790      int size = numFormat.leadingDigitsPatternSize();
1791      if (size == 0 || regexCache.getPatternForRegex(
1792              // We always use the last leading_digits_pattern, as it is the most detailed.
1793              numFormat.getLeadingDigitsPattern(size - 1)).matcher(nationalNumber).lookingAt()) {
1794        Matcher m = regexCache.getPatternForRegex(numFormat.getPattern()).matcher(nationalNumber);
1795        if (m.matches()) {
1796          return numFormat;
1797        }
1798      }
1799    }
1800    return null;
1801  }
1802
1803  // Simple wrapper of formatNsnUsingPattern for the common case of no carrier code.
1804  String formatNsnUsingPattern(String nationalNumber,
1805                               NumberFormat formattingPattern,
1806                               PhoneNumberFormat numberFormat) {
1807    return formatNsnUsingPattern(nationalNumber, formattingPattern, numberFormat, null);
1808  }
1809
1810  // Note that carrierCode is optional - if null or an empty string, no carrier code replacement
1811  // will take place.
1812  private String formatNsnUsingPattern(String nationalNumber,
1813                                       NumberFormat formattingPattern,
1814                                       PhoneNumberFormat numberFormat,
1815                                       String carrierCode) {
1816    String numberFormatRule = formattingPattern.getFormat();
1817    Matcher m =
1818        regexCache.getPatternForRegex(formattingPattern.getPattern()).matcher(nationalNumber);
1819    String formattedNationalNumber = "";
1820    if (numberFormat == PhoneNumberFormat.NATIONAL &&
1821        carrierCode != null && carrierCode.length() > 0 &&
1822        formattingPattern.getDomesticCarrierCodeFormattingRule().length() > 0) {
1823      // Replace the $CC in the formatting rule with the desired carrier code.
1824      String carrierCodeFormattingRule = formattingPattern.getDomesticCarrierCodeFormattingRule();
1825      carrierCodeFormattingRule =
1826          CC_PATTERN.matcher(carrierCodeFormattingRule).replaceFirst(carrierCode);
1827      // Now replace the $FG in the formatting rule with the first group and the carrier code
1828      // combined in the appropriate way.
1829      numberFormatRule = FIRST_GROUP_PATTERN.matcher(numberFormatRule)
1830          .replaceFirst(carrierCodeFormattingRule);
1831      formattedNationalNumber = m.replaceAll(numberFormatRule);
1832    } else {
1833      // Use the national prefix formatting rule instead.
1834      String nationalPrefixFormattingRule = formattingPattern.getNationalPrefixFormattingRule();
1835      if (numberFormat == PhoneNumberFormat.NATIONAL &&
1836          nationalPrefixFormattingRule != null &&
1837          nationalPrefixFormattingRule.length() > 0) {
1838        Matcher firstGroupMatcher = FIRST_GROUP_PATTERN.matcher(numberFormatRule);
1839        formattedNationalNumber =
1840            m.replaceAll(firstGroupMatcher.replaceFirst(nationalPrefixFormattingRule));
1841      } else {
1842        formattedNationalNumber = m.replaceAll(numberFormatRule);
1843      }
1844    }
1845    if (numberFormat == PhoneNumberFormat.RFC3966) {
1846      // Strip any leading punctuation.
1847      Matcher matcher = SEPARATOR_PATTERN.matcher(formattedNationalNumber);
1848      if (matcher.lookingAt()) {
1849        formattedNationalNumber = matcher.replaceFirst("");
1850      }
1851      // Replace the rest with a dash between each number group.
1852      formattedNationalNumber = matcher.reset(formattedNationalNumber).replaceAll("-");
1853    }
1854    return formattedNationalNumber;
1855  }
1856
1857  /**
1858   * Gets a valid number for the specified region.
1859   *
1860   * @param regionCode  the region for which an example number is needed
1861   * @return  a valid fixed-line number for the specified region. Returns null when the metadata
1862   *    does not contain such information, or the region 001 is passed in. For 001 (representing
1863   *    non-geographical numbers), call {@link #getExampleNumberForNonGeoEntity} instead.
1864   */
1865  public PhoneNumber getExampleNumber(String regionCode) {
1866    return getExampleNumberForType(regionCode, PhoneNumberType.FIXED_LINE);
1867  }
1868
1869  /**
1870   * Gets a valid number for the specified region and number type.
1871   *
1872   * @param regionCode  the region for which an example number is needed
1873   * @param type  the type of number that is needed
1874   * @return  a valid number for the specified region and type. Returns null when the metadata
1875   *     does not contain such information or if an invalid region or region 001 was entered.
1876   *     For 001 (representing non-geographical numbers), call
1877   *     {@link #getExampleNumberForNonGeoEntity} instead.
1878   */
1879  public PhoneNumber getExampleNumberForType(String regionCode, PhoneNumberType type) {
1880    // Check the region code is valid.
1881    if (!isValidRegionCode(regionCode)) {
1882      logger.log(Level.WARNING, "Invalid or unknown region code provided: " + regionCode);
1883      return null;
1884    }
1885    PhoneNumberDesc desc = getNumberDescByType(getMetadataForRegion(regionCode), type);
1886    try {
1887      if (desc.hasExampleNumber()) {
1888        return parse(desc.getExampleNumber(), regionCode);
1889      }
1890    } catch (NumberParseException e) {
1891      logger.log(Level.SEVERE, e.toString());
1892    }
1893    return null;
1894  }
1895
1896  /**
1897   * Gets a valid number for the specified country calling code for a non-geographical entity.
1898   *
1899   * @param countryCallingCode  the country calling code for a non-geographical entity
1900   * @return  a valid number for the non-geographical entity. Returns null when the metadata
1901   *    does not contain such information, or the country calling code passed in does not belong
1902   *    to a non-geographical entity.
1903   */
1904  public PhoneNumber getExampleNumberForNonGeoEntity(int countryCallingCode) {
1905    PhoneMetadata metadata = getMetadataForNonGeographicalRegion(countryCallingCode);
1906    if (metadata != null) {
1907      PhoneNumberDesc desc = metadata.getGeneralDesc();
1908      try {
1909        if (desc.hasExampleNumber()) {
1910          return parse("+" + countryCallingCode + desc.getExampleNumber(), "ZZ");
1911        }
1912      } catch (NumberParseException e) {
1913        logger.log(Level.SEVERE, e.toString());
1914      }
1915    } else {
1916      logger.log(Level.WARNING,
1917                 "Invalid or unknown country calling code provided: " + countryCallingCode);
1918    }
1919    return null;
1920  }
1921
1922  /**
1923   * Appends the formatted extension of a phone number to formattedNumber, if the phone number had
1924   * an extension specified.
1925   */
1926  private void maybeAppendFormattedExtension(PhoneNumber number, PhoneMetadata metadata,
1927                                             PhoneNumberFormat numberFormat,
1928                                             StringBuilder formattedNumber) {
1929    if (number.hasExtension() && number.getExtension().length() > 0) {
1930      if (numberFormat == PhoneNumberFormat.RFC3966) {
1931        formattedNumber.append(RFC3966_EXTN_PREFIX).append(number.getExtension());
1932      } else {
1933        if (metadata.hasPreferredExtnPrefix()) {
1934          formattedNumber.append(metadata.getPreferredExtnPrefix()).append(number.getExtension());
1935        } else {
1936          formattedNumber.append(DEFAULT_EXTN_PREFIX).append(number.getExtension());
1937        }
1938      }
1939    }
1940  }
1941
1942  PhoneNumberDesc getNumberDescByType(PhoneMetadata metadata, PhoneNumberType type) {
1943    switch (type) {
1944      case PREMIUM_RATE:
1945        return metadata.getPremiumRate();
1946      case TOLL_FREE:
1947        return metadata.getTollFree();
1948      case MOBILE:
1949        return metadata.getMobile();
1950      case FIXED_LINE:
1951      case FIXED_LINE_OR_MOBILE:
1952        return metadata.getFixedLine();
1953      case SHARED_COST:
1954        return metadata.getSharedCost();
1955      case VOIP:
1956        return metadata.getVoip();
1957      case PERSONAL_NUMBER:
1958        return metadata.getPersonalNumber();
1959      case PAGER:
1960        return metadata.getPager();
1961      case UAN:
1962        return metadata.getUan();
1963      case VOICEMAIL:
1964        return metadata.getVoicemail();
1965      default:
1966        return metadata.getGeneralDesc();
1967    }
1968  }
1969
1970  /**
1971   * Gets the type of a phone number.
1972   *
1973   * @param number  the phone number that we want to know the type
1974   * @return  the type of the phone number
1975   */
1976  public PhoneNumberType getNumberType(PhoneNumber number) {
1977    String regionCode = getRegionCodeForNumber(number);
1978    PhoneMetadata metadata = getMetadataForRegionOrCallingCode(number.getCountryCode(), regionCode);
1979    if (metadata == null) {
1980      return PhoneNumberType.UNKNOWN;
1981    }
1982    String nationalSignificantNumber = getNationalSignificantNumber(number);
1983    return getNumberTypeHelper(nationalSignificantNumber, metadata);
1984  }
1985
1986  private PhoneNumberType getNumberTypeHelper(String nationalNumber, PhoneMetadata metadata) {
1987    if (!isNumberMatchingDesc(nationalNumber, metadata.getGeneralDesc())) {
1988      return PhoneNumberType.UNKNOWN;
1989    }
1990
1991    if (isNumberMatchingDesc(nationalNumber, metadata.getPremiumRate())) {
1992      return PhoneNumberType.PREMIUM_RATE;
1993    }
1994    if (isNumberMatchingDesc(nationalNumber, metadata.getTollFree())) {
1995      return PhoneNumberType.TOLL_FREE;
1996    }
1997    if (isNumberMatchingDesc(nationalNumber, metadata.getSharedCost())) {
1998      return PhoneNumberType.SHARED_COST;
1999    }
2000    if (isNumberMatchingDesc(nationalNumber, metadata.getVoip())) {
2001      return PhoneNumberType.VOIP;
2002    }
2003    if (isNumberMatchingDesc(nationalNumber, metadata.getPersonalNumber())) {
2004      return PhoneNumberType.PERSONAL_NUMBER;
2005    }
2006    if (isNumberMatchingDesc(nationalNumber, metadata.getPager())) {
2007      return PhoneNumberType.PAGER;
2008    }
2009    if (isNumberMatchingDesc(nationalNumber, metadata.getUan())) {
2010      return PhoneNumberType.UAN;
2011    }
2012    if (isNumberMatchingDesc(nationalNumber, metadata.getVoicemail())) {
2013      return PhoneNumberType.VOICEMAIL;
2014    }
2015
2016    boolean isFixedLine = isNumberMatchingDesc(nationalNumber, metadata.getFixedLine());
2017    if (isFixedLine) {
2018      if (metadata.isSameMobileAndFixedLinePattern()) {
2019        return PhoneNumberType.FIXED_LINE_OR_MOBILE;
2020      } else if (isNumberMatchingDesc(nationalNumber, metadata.getMobile())) {
2021        return PhoneNumberType.FIXED_LINE_OR_MOBILE;
2022      }
2023      return PhoneNumberType.FIXED_LINE;
2024    }
2025    // Otherwise, test to see if the number is mobile. Only do this if certain that the patterns for
2026    // mobile and fixed line aren't the same.
2027    if (!metadata.isSameMobileAndFixedLinePattern() &&
2028        isNumberMatchingDesc(nationalNumber, metadata.getMobile())) {
2029      return PhoneNumberType.MOBILE;
2030    }
2031    return PhoneNumberType.UNKNOWN;
2032  }
2033
2034  /**
2035   * Returns the metadata for the given region code or {@code null} if the region code is invalid
2036   * or unknown.
2037   */
2038  PhoneMetadata getMetadataForRegion(String regionCode) {
2039    if (!isValidRegionCode(regionCode)) {
2040      return null;
2041    }
2042    synchronized (regionToMetadataMap) {
2043      if (!regionToMetadataMap.containsKey(regionCode)) {
2044        // The regionCode here will be valid and won't be '001', so we don't need to worry about
2045        // what to pass in for the country calling code.
2046        loadMetadataFromFile(currentFilePrefix, regionCode, 0, metadataLoader);
2047      }
2048    }
2049    return regionToMetadataMap.get(regionCode);
2050  }
2051
2052  PhoneMetadata getMetadataForNonGeographicalRegion(int countryCallingCode) {
2053    synchronized (countryCodeToNonGeographicalMetadataMap) {
2054      if (!countryCallingCodeToRegionCodeMap.containsKey(countryCallingCode)) {
2055        return null;
2056      }
2057      if (!countryCodeToNonGeographicalMetadataMap.containsKey(countryCallingCode)) {
2058        loadMetadataFromFile(
2059            currentFilePrefix, REGION_CODE_FOR_NON_GEO_ENTITY, countryCallingCode, metadataLoader);
2060      }
2061    }
2062    return countryCodeToNonGeographicalMetadataMap.get(countryCallingCode);
2063  }
2064
2065  boolean isNumberPossibleForDesc(String nationalNumber, PhoneNumberDesc numberDesc) {
2066    Matcher possibleNumberPatternMatcher =
2067        regexCache.getPatternForRegex(numberDesc.getPossibleNumberPattern())
2068            .matcher(nationalNumber);
2069    return possibleNumberPatternMatcher.matches();
2070  }
2071
2072  boolean isNumberMatchingDesc(String nationalNumber, PhoneNumberDesc numberDesc) {
2073    Matcher nationalNumberPatternMatcher =
2074        regexCache.getPatternForRegex(numberDesc.getNationalNumberPattern())
2075            .matcher(nationalNumber);
2076    return isNumberPossibleForDesc(nationalNumber, numberDesc) &&
2077        nationalNumberPatternMatcher.matches();
2078  }
2079
2080  /**
2081   * Tests whether a phone number matches a valid pattern. Note this doesn't verify the number
2082   * is actually in use, which is impossible to tell by just looking at a number itself.
2083   *
2084   * @param number       the phone number that we want to validate
2085   * @return  a boolean that indicates whether the number is of a valid pattern
2086   */
2087  public boolean isValidNumber(PhoneNumber number) {
2088    String regionCode = getRegionCodeForNumber(number);
2089    return isValidNumberForRegion(number, regionCode);
2090  }
2091
2092  /**
2093   * Tests whether a phone number is valid for a certain region. Note this doesn't verify the number
2094   * is actually in use, which is impossible to tell by just looking at a number itself. If the
2095   * country calling code is not the same as the country calling code for the region, this
2096   * immediately exits with false. After this, the specific number pattern rules for the region are
2097   * examined. This is useful for determining for example whether a particular number is valid for
2098   * Canada, rather than just a valid NANPA number.
2099   * Warning: In most cases, you want to use {@link #isValidNumber} instead. For example, this
2100   * method will mark numbers from British Crown dependencies such as the Isle of Man as invalid for
2101   * the region "GB" (United Kingdom), since it has its own region code, "IM", which may be
2102   * undesirable.
2103   *
2104   * @param number       the phone number that we want to validate
2105   * @param regionCode   the region that we want to validate the phone number for
2106   * @return  a boolean that indicates whether the number is of a valid pattern
2107   */
2108  public boolean isValidNumberForRegion(PhoneNumber number, String regionCode) {
2109    int countryCode = number.getCountryCode();
2110    PhoneMetadata metadata = getMetadataForRegionOrCallingCode(countryCode, regionCode);
2111    if ((metadata == null) ||
2112        (!REGION_CODE_FOR_NON_GEO_ENTITY.equals(regionCode) &&
2113         countryCode != getCountryCodeForValidRegion(regionCode))) {
2114      // Either the region code was invalid, or the country calling code for this number does not
2115      // match that of the region code.
2116      return false;
2117    }
2118    String nationalSignificantNumber = getNationalSignificantNumber(number);
2119    return getNumberTypeHelper(nationalSignificantNumber, metadata) != PhoneNumberType.UNKNOWN;
2120  }
2121
2122  /**
2123   * Returns the region where a phone number is from. This could be used for geocoding at the region
2124   * level.
2125   *
2126   * @param number  the phone number whose origin we want to know
2127   * @return  the region where the phone number is from, or null if no region matches this calling
2128   *     code
2129   */
2130  public String getRegionCodeForNumber(PhoneNumber number) {
2131    int countryCode = number.getCountryCode();
2132    List<String> regions = countryCallingCodeToRegionCodeMap.get(countryCode);
2133    if (regions == null) {
2134      String numberString = getNationalSignificantNumber(number);
2135      logger.log(Level.WARNING,
2136                 "Missing/invalid country_code (" + countryCode + ") for number " + numberString);
2137      return null;
2138    }
2139    if (regions.size() == 1) {
2140      return regions.get(0);
2141    } else {
2142      return getRegionCodeForNumberFromRegionList(number, regions);
2143    }
2144  }
2145
2146  private String getRegionCodeForNumberFromRegionList(PhoneNumber number,
2147                                                      List<String> regionCodes) {
2148    String nationalNumber = getNationalSignificantNumber(number);
2149    for (String regionCode : regionCodes) {
2150      // If leadingDigits is present, use this. Otherwise, do full validation.
2151      // Metadata cannot be null because the region codes come from the country calling code map.
2152      PhoneMetadata metadata = getMetadataForRegion(regionCode);
2153      if (metadata.hasLeadingDigits()) {
2154        if (regexCache.getPatternForRegex(metadata.getLeadingDigits())
2155                .matcher(nationalNumber).lookingAt()) {
2156          return regionCode;
2157        }
2158      } else if (getNumberTypeHelper(nationalNumber, metadata) != PhoneNumberType.UNKNOWN) {
2159        return regionCode;
2160      }
2161    }
2162    return null;
2163  }
2164
2165  /**
2166   * Returns the region code that matches the specific country calling code. In the case of no
2167   * region code being found, ZZ will be returned. In the case of multiple regions, the one
2168   * designated in the metadata as the "main" region for this calling code will be returned. If the
2169   * countryCallingCode entered is valid but doesn't match a specific region (such as in the case of
2170   * non-geographical calling codes like 800) the value "001" will be returned (corresponding to
2171   * the value for World in the UN M.49 schema).
2172   */
2173  public String getRegionCodeForCountryCode(int countryCallingCode) {
2174    List<String> regionCodes = countryCallingCodeToRegionCodeMap.get(countryCallingCode);
2175    return regionCodes == null ? UNKNOWN_REGION : regionCodes.get(0);
2176  }
2177
2178  /**
2179   * Returns a list with the region codes that match the specific country calling code. For
2180   * non-geographical country calling codes, the region code 001 is returned. Also, in the case
2181   * of no region code being found, an empty list is returned.
2182   */
2183  public List<String> getRegionCodesForCountryCode(int countryCallingCode) {
2184    List<String> regionCodes = countryCallingCodeToRegionCodeMap.get(countryCallingCode);
2185    return Collections.unmodifiableList(regionCodes == null ? new ArrayList<String>(0)
2186                                                            : regionCodes);
2187  }
2188
2189  /**
2190   * Returns the country calling code for a specific region. For example, this would be 1 for the
2191   * United States, and 64 for New Zealand.
2192   *
2193   * @param regionCode  the region that we want to get the country calling code for
2194   * @return  the country calling code for the region denoted by regionCode
2195   */
2196  public int getCountryCodeForRegion(String regionCode) {
2197    if (!isValidRegionCode(regionCode)) {
2198      logger.log(Level.WARNING,
2199                 "Invalid or missing region code ("
2200                  + ((regionCode == null) ? "null" : regionCode)
2201                  + ") provided.");
2202      return 0;
2203    }
2204    return getCountryCodeForValidRegion(regionCode);
2205  }
2206
2207  /**
2208   * Returns the country calling code for a specific region. For example, this would be 1 for the
2209   * United States, and 64 for New Zealand. Assumes the region is already valid.
2210   *
2211   * @param regionCode  the region that we want to get the country calling code for
2212   * @return  the country calling code for the region denoted by regionCode
2213   * @throws IllegalArgumentException if the region is invalid
2214   */
2215  private int getCountryCodeForValidRegion(String regionCode) {
2216    PhoneMetadata metadata = getMetadataForRegion(regionCode);
2217    if (metadata == null) {
2218      throw new IllegalArgumentException("Invalid region code: " + regionCode);
2219    }
2220    return metadata.getCountryCode();
2221  }
2222
2223  /**
2224   * Returns the national dialling prefix for a specific region. For example, this would be 1 for
2225   * the United States, and 0 for New Zealand. Set stripNonDigits to true to strip symbols like "~"
2226   * (which indicates a wait for a dialling tone) from the prefix returned. If no national prefix is
2227   * present, we return null.
2228   *
2229   * <p>Warning: Do not use this method for do-your-own formatting - for some regions, the
2230   * national dialling prefix is used only for certain types of numbers. Use the library's
2231   * formatting functions to prefix the national prefix when required.
2232   *
2233   * @param regionCode  the region that we want to get the dialling prefix for
2234   * @param stripNonDigits  true to strip non-digits from the national dialling prefix
2235   * @return  the dialling prefix for the region denoted by regionCode
2236   */
2237  public String getNddPrefixForRegion(String regionCode, boolean stripNonDigits) {
2238    PhoneMetadata metadata = getMetadataForRegion(regionCode);
2239    if (metadata == null) {
2240      logger.log(Level.WARNING,
2241                 "Invalid or missing region code ("
2242                  + ((regionCode == null) ? "null" : regionCode)
2243                  + ") provided.");
2244      return null;
2245    }
2246    String nationalPrefix = metadata.getNationalPrefix();
2247    // If no national prefix was found, we return null.
2248    if (nationalPrefix.length() == 0) {
2249      return null;
2250    }
2251    if (stripNonDigits) {
2252      // Note: if any other non-numeric symbols are ever used in national prefixes, these would have
2253      // to be removed here as well.
2254      nationalPrefix = nationalPrefix.replace("~", "");
2255    }
2256    return nationalPrefix;
2257  }
2258
2259  /**
2260   * Checks if this is a region under the North American Numbering Plan Administration (NANPA).
2261   *
2262   * @return  true if regionCode is one of the regions under NANPA
2263   */
2264  public boolean isNANPACountry(String regionCode) {
2265    return nanpaRegions.contains(regionCode);
2266  }
2267
2268  /**
2269   * Checks whether the country calling code is from a region whose national significant number
2270   * could contain a leading zero. An example of such a region is Italy. Returns false if no
2271   * metadata for the country is found.
2272   */
2273  boolean isLeadingZeroPossible(int countryCallingCode) {
2274    PhoneMetadata mainMetadataForCallingCode =
2275        getMetadataForRegionOrCallingCode(countryCallingCode,
2276                                          getRegionCodeForCountryCode(countryCallingCode));
2277    if (mainMetadataForCallingCode == null) {
2278      return false;
2279    }
2280    return mainMetadataForCallingCode.isLeadingZeroPossible();
2281  }
2282
2283  /**
2284   * Checks if the number is a valid vanity (alpha) number such as 800 MICROSOFT. A valid vanity
2285   * number will start with at least 3 digits and will have three or more alpha characters. This
2286   * does not do region-specific checks - to work out if this number is actually valid for a region,
2287   * it should be parsed and methods such as {@link #isPossibleNumberWithReason} and
2288   * {@link #isValidNumber} should be used.
2289   *
2290   * @param number  the number that needs to be checked
2291   * @return  true if the number is a valid vanity number
2292   */
2293  public boolean isAlphaNumber(String number) {
2294    if (!isViablePhoneNumber(number)) {
2295      // Number is too short, or doesn't match the basic phone number pattern.
2296      return false;
2297    }
2298    StringBuilder strippedNumber = new StringBuilder(number);
2299    maybeStripExtension(strippedNumber);
2300    return VALID_ALPHA_PHONE_PATTERN.matcher(strippedNumber).matches();
2301  }
2302
2303  /**
2304   * Convenience wrapper around {@link #isPossibleNumberWithReason}. Instead of returning the reason
2305   * for failure, this method returns a boolean value.
2306   * @param number  the number that needs to be checked
2307   * @return  true if the number is possible
2308   */
2309  public boolean isPossibleNumber(PhoneNumber number) {
2310    return isPossibleNumberWithReason(number) == ValidationResult.IS_POSSIBLE;
2311  }
2312
2313  /**
2314   * Helper method to check a number against a particular pattern and determine whether it matches,
2315   * or is too short or too long. Currently, if a number pattern suggests that numbers of length 7
2316   * and 10 are possible, and a number in between these possible lengths is entered, such as of
2317   * length 8, this will return TOO_LONG.
2318   */
2319  private ValidationResult testNumberLengthAgainstPattern(Pattern numberPattern, String number) {
2320    Matcher numberMatcher = numberPattern.matcher(number);
2321    if (numberMatcher.matches()) {
2322      return ValidationResult.IS_POSSIBLE;
2323    }
2324    if (numberMatcher.lookingAt()) {
2325      return ValidationResult.TOO_LONG;
2326    } else {
2327      return ValidationResult.TOO_SHORT;
2328    }
2329  }
2330
2331  /**
2332   * Helper method to check whether a number is too short to be a regular length phone number in a
2333   * region.
2334   */
2335  private boolean isShorterThanPossibleNormalNumber(PhoneMetadata regionMetadata, String number) {
2336    Pattern possibleNumberPattern = regexCache.getPatternForRegex(
2337        regionMetadata.getGeneralDesc().getPossibleNumberPattern());
2338    return testNumberLengthAgainstPattern(possibleNumberPattern, number) ==
2339        ValidationResult.TOO_SHORT;
2340  }
2341
2342  /**
2343   * Check whether a phone number is a possible number. It provides a more lenient check than
2344   * {@link #isValidNumber} in the following sense:
2345   *<ol>
2346   * <li> It only checks the length of phone numbers. In particular, it doesn't check starting
2347   *      digits of the number.
2348   * <li> It doesn't attempt to figure out the type of the number, but uses general rules which
2349   *      applies to all types of phone numbers in a region. Therefore, it is much faster than
2350   *      isValidNumber.
2351   * <li> For fixed line numbers, many regions have the concept of area code, which together with
2352   *      subscriber number constitute the national significant number. It is sometimes okay to dial
2353   *      the subscriber number only when dialing in the same area. This function will return
2354   *      true if the subscriber-number-only version is passed in. On the other hand, because
2355   *      isValidNumber validates using information on both starting digits (for fixed line
2356   *      numbers, that would most likely be area codes) and length (obviously includes the
2357   *      length of area codes for fixed line numbers), it will return false for the
2358   *      subscriber-number-only version.
2359   * </ol>
2360   * @param number  the number that needs to be checked
2361   * @return  a ValidationResult object which indicates whether the number is possible
2362   */
2363  public ValidationResult isPossibleNumberWithReason(PhoneNumber number) {
2364    String nationalNumber = getNationalSignificantNumber(number);
2365    int countryCode = number.getCountryCode();
2366    // Note: For Russian Fed and NANPA numbers, we just use the rules from the default region (US or
2367    // Russia) since the getRegionCodeForNumber will not work if the number is possible but not
2368    // valid. This would need to be revisited if the possible number pattern ever differed between
2369    // various regions within those plans.
2370    if (!hasValidCountryCallingCode(countryCode)) {
2371      return ValidationResult.INVALID_COUNTRY_CODE;
2372    }
2373    String regionCode = getRegionCodeForCountryCode(countryCode);
2374    // Metadata cannot be null because the country calling code is valid.
2375    PhoneMetadata metadata = getMetadataForRegionOrCallingCode(countryCode, regionCode);
2376    Pattern possibleNumberPattern =
2377        regexCache.getPatternForRegex(metadata.getGeneralDesc().getPossibleNumberPattern());
2378    return testNumberLengthAgainstPattern(possibleNumberPattern, nationalNumber);
2379  }
2380
2381  /**
2382   * Check whether a phone number is a possible number given a number in the form of a string, and
2383   * the region where the number could be dialed from. It provides a more lenient check than
2384   * {@link #isValidNumber}. See {@link #isPossibleNumber(PhoneNumber)} for details.
2385   *
2386   * <p>This method first parses the number, then invokes {@link #isPossibleNumber(PhoneNumber)}
2387   * with the resultant PhoneNumber object.
2388   *
2389   * @param number  the number that needs to be checked, in the form of a string
2390   * @param regionDialingFrom  the region that we are expecting the number to be dialed from.
2391   *     Note this is different from the region where the number belongs.  For example, the number
2392   *     +1 650 253 0000 is a number that belongs to US. When written in this form, it can be
2393   *     dialed from any region. When it is written as 00 1 650 253 0000, it can be dialed from any
2394   *     region which uses an international dialling prefix of 00. When it is written as
2395   *     650 253 0000, it can only be dialed from within the US, and when written as 253 0000, it
2396   *     can only be dialed from within a smaller area in the US (Mountain View, CA, to be more
2397   *     specific).
2398   * @return  true if the number is possible
2399   */
2400  public boolean isPossibleNumber(String number, String regionDialingFrom) {
2401    try {
2402      return isPossibleNumber(parse(number, regionDialingFrom));
2403    } catch (NumberParseException e) {
2404      return false;
2405    }
2406  }
2407
2408  /**
2409   * Attempts to extract a valid number from a phone number that is too long to be valid, and resets
2410   * the PhoneNumber object passed in to that valid version. If no valid number could be extracted,
2411   * the PhoneNumber object passed in will not be modified.
2412   * @param number a PhoneNumber object which contains a number that is too long to be valid.
2413   * @return  true if a valid phone number can be successfully extracted.
2414   */
2415  public boolean truncateTooLongNumber(PhoneNumber number) {
2416    if (isValidNumber(number)) {
2417      return true;
2418    }
2419    PhoneNumber numberCopy = new PhoneNumber();
2420    numberCopy.mergeFrom(number);
2421    long nationalNumber = number.getNationalNumber();
2422    do {
2423      nationalNumber /= 10;
2424      numberCopy.setNationalNumber(nationalNumber);
2425      if (isPossibleNumberWithReason(numberCopy) == ValidationResult.TOO_SHORT ||
2426          nationalNumber == 0) {
2427        return false;
2428      }
2429    } while (!isValidNumber(numberCopy));
2430    number.setNationalNumber(nationalNumber);
2431    return true;
2432  }
2433
2434  /**
2435   * Gets an {@link com.google.i18n.phonenumbers.AsYouTypeFormatter} for the specific region.
2436   *
2437   * @param regionCode  the region where the phone number is being entered
2438   * @return  an {@link com.google.i18n.phonenumbers.AsYouTypeFormatter} object, which can be used
2439   *     to format phone numbers in the specific region "as you type"
2440   */
2441  public AsYouTypeFormatter getAsYouTypeFormatter(String regionCode) {
2442    return new AsYouTypeFormatter(regionCode);
2443  }
2444
2445  // Extracts country calling code from fullNumber, returns it and places the remaining number in
2446  // nationalNumber. It assumes that the leading plus sign or IDD has already been removed. Returns
2447  // 0 if fullNumber doesn't start with a valid country calling code, and leaves nationalNumber
2448  // unmodified.
2449  int extractCountryCode(StringBuilder fullNumber, StringBuilder nationalNumber) {
2450    if ((fullNumber.length() == 0) || (fullNumber.charAt(0) == '0')) {
2451      // Country codes do not begin with a '0'.
2452      return 0;
2453    }
2454    int potentialCountryCode;
2455    int numberLength = fullNumber.length();
2456    for (int i = 1; i <= MAX_LENGTH_COUNTRY_CODE && i <= numberLength; i++) {
2457      potentialCountryCode = Integer.parseInt(fullNumber.substring(0, i));
2458      if (countryCallingCodeToRegionCodeMap.containsKey(potentialCountryCode)) {
2459        nationalNumber.append(fullNumber.substring(i));
2460        return potentialCountryCode;
2461      }
2462    }
2463    return 0;
2464  }
2465
2466  /**
2467   * Tries to extract a country calling code from a number. This method will return zero if no
2468   * country calling code is considered to be present. Country calling codes are extracted in the
2469   * following ways:
2470   * <ul>
2471   *  <li> by stripping the international dialing prefix of the region the person is dialing from,
2472   *       if this is present in the number, and looking at the next digits
2473   *  <li> by stripping the '+' sign if present and then looking at the next digits
2474   *  <li> by comparing the start of the number and the country calling code of the default region.
2475   *       If the number is not considered possible for the numbering plan of the default region
2476   *       initially, but starts with the country calling code of this region, validation will be
2477   *       reattempted after stripping this country calling code. If this number is considered a
2478   *       possible number, then the first digits will be considered the country calling code and
2479   *       removed as such.
2480   * </ul>
2481   * It will throw a NumberParseException if the number starts with a '+' but the country calling
2482   * code supplied after this does not match that of any known region.
2483   *
2484   * @param number  non-normalized telephone number that we wish to extract a country calling
2485   *     code from - may begin with '+'
2486   * @param defaultRegionMetadata  metadata about the region this number may be from
2487   * @param nationalNumber  a string buffer to store the national significant number in, in the case
2488   *     that a country calling code was extracted. The number is appended to any existing contents.
2489   *     If no country calling code was extracted, this will be left unchanged.
2490   * @param keepRawInput  true if the country_code_source and preferred_carrier_code fields of
2491   *     phoneNumber should be populated.
2492   * @param phoneNumber  the PhoneNumber object where the country_code and country_code_source need
2493   *     to be populated. Note the country_code is always populated, whereas country_code_source is
2494   *     only populated when keepCountryCodeSource is true.
2495   * @return  the country calling code extracted or 0 if none could be extracted
2496   */
2497  // @VisibleForTesting
2498  int maybeExtractCountryCode(String number, PhoneMetadata defaultRegionMetadata,
2499                              StringBuilder nationalNumber, boolean keepRawInput,
2500                              PhoneNumber phoneNumber)
2501      throws NumberParseException {
2502    if (number.length() == 0) {
2503      return 0;
2504    }
2505    StringBuilder fullNumber = new StringBuilder(number);
2506    // Set the default prefix to be something that will never match.
2507    String possibleCountryIddPrefix = "NonMatch";
2508    if (defaultRegionMetadata != null) {
2509      possibleCountryIddPrefix = defaultRegionMetadata.getInternationalPrefix();
2510    }
2511
2512    CountryCodeSource countryCodeSource =
2513        maybeStripInternationalPrefixAndNormalize(fullNumber, possibleCountryIddPrefix);
2514    if (keepRawInput) {
2515      phoneNumber.setCountryCodeSource(countryCodeSource);
2516    }
2517    if (countryCodeSource != CountryCodeSource.FROM_DEFAULT_COUNTRY) {
2518      if (fullNumber.length() <= MIN_LENGTH_FOR_NSN) {
2519        throw new NumberParseException(NumberParseException.ErrorType.TOO_SHORT_AFTER_IDD,
2520                                       "Phone number had an IDD, but after this was not "
2521                                       + "long enough to be a viable phone number.");
2522      }
2523      int potentialCountryCode = extractCountryCode(fullNumber, nationalNumber);
2524      if (potentialCountryCode != 0) {
2525        phoneNumber.setCountryCode(potentialCountryCode);
2526        return potentialCountryCode;
2527      }
2528
2529      // If this fails, they must be using a strange country calling code that we don't recognize,
2530      // or that doesn't exist.
2531      throw new NumberParseException(NumberParseException.ErrorType.INVALID_COUNTRY_CODE,
2532                                     "Country calling code supplied was not recognised.");
2533    } else if (defaultRegionMetadata != null) {
2534      // Check to see if the number starts with the country calling code for the default region. If
2535      // so, we remove the country calling code, and do some checks on the validity of the number
2536      // before and after.
2537      int defaultCountryCode = defaultRegionMetadata.getCountryCode();
2538      String defaultCountryCodeString = String.valueOf(defaultCountryCode);
2539      String normalizedNumber = fullNumber.toString();
2540      if (normalizedNumber.startsWith(defaultCountryCodeString)) {
2541        StringBuilder potentialNationalNumber =
2542            new StringBuilder(normalizedNumber.substring(defaultCountryCodeString.length()));
2543        PhoneNumberDesc generalDesc = defaultRegionMetadata.getGeneralDesc();
2544        Pattern validNumberPattern =
2545            regexCache.getPatternForRegex(generalDesc.getNationalNumberPattern());
2546        maybeStripNationalPrefixAndCarrierCode(
2547            potentialNationalNumber, defaultRegionMetadata, null /* Don't need the carrier code */);
2548        Pattern possibleNumberPattern =
2549            regexCache.getPatternForRegex(generalDesc.getPossibleNumberPattern());
2550        // If the number was not valid before but is valid now, or if it was too long before, we
2551        // consider the number with the country calling code stripped to be a better result and
2552        // keep that instead.
2553        if ((!validNumberPattern.matcher(fullNumber).matches() &&
2554             validNumberPattern.matcher(potentialNationalNumber).matches()) ||
2555             testNumberLengthAgainstPattern(possibleNumberPattern, fullNumber.toString())
2556                  == ValidationResult.TOO_LONG) {
2557          nationalNumber.append(potentialNationalNumber);
2558          if (keepRawInput) {
2559            phoneNumber.setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN);
2560          }
2561          phoneNumber.setCountryCode(defaultCountryCode);
2562          return defaultCountryCode;
2563        }
2564      }
2565    }
2566    // No country calling code present.
2567    phoneNumber.setCountryCode(0);
2568    return 0;
2569  }
2570
2571  /**
2572   * Strips the IDD from the start of the number if present. Helper function used by
2573   * maybeStripInternationalPrefixAndNormalize.
2574   */
2575  private boolean parsePrefixAsIdd(Pattern iddPattern, StringBuilder number) {
2576    Matcher m = iddPattern.matcher(number);
2577    if (m.lookingAt()) {
2578      int matchEnd = m.end();
2579      // Only strip this if the first digit after the match is not a 0, since country calling codes
2580      // cannot begin with 0.
2581      Matcher digitMatcher = CAPTURING_DIGIT_PATTERN.matcher(number.substring(matchEnd));
2582      if (digitMatcher.find()) {
2583        String normalizedGroup = normalizeDigitsOnly(digitMatcher.group(1));
2584        if (normalizedGroup.equals("0")) {
2585          return false;
2586        }
2587      }
2588      number.delete(0, matchEnd);
2589      return true;
2590    }
2591    return false;
2592  }
2593
2594  /**
2595   * Strips any international prefix (such as +, 00, 011) present in the number provided, normalizes
2596   * the resulting number, and indicates if an international prefix was present.
2597   *
2598   * @param number  the non-normalized telephone number that we wish to strip any international
2599   *     dialing prefix from.
2600   * @param possibleIddPrefix  the international direct dialing prefix from the region we
2601   *     think this number may be dialed in
2602   * @return  the corresponding CountryCodeSource if an international dialing prefix could be
2603   *     removed from the number, otherwise CountryCodeSource.FROM_DEFAULT_COUNTRY if the number did
2604   *     not seem to be in international format.
2605   */
2606  // @VisibleForTesting
2607  CountryCodeSource maybeStripInternationalPrefixAndNormalize(
2608      StringBuilder number,
2609      String possibleIddPrefix) {
2610    if (number.length() == 0) {
2611      return CountryCodeSource.FROM_DEFAULT_COUNTRY;
2612    }
2613    // Check to see if the number begins with one or more plus signs.
2614    Matcher m = PLUS_CHARS_PATTERN.matcher(number);
2615    if (m.lookingAt()) {
2616      number.delete(0, m.end());
2617      // Can now normalize the rest of the number since we've consumed the "+" sign at the start.
2618      normalize(number);
2619      return CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN;
2620    }
2621    // Attempt to parse the first digits as an international prefix.
2622    Pattern iddPattern = regexCache.getPatternForRegex(possibleIddPrefix);
2623    normalize(number);
2624    return parsePrefixAsIdd(iddPattern, number)
2625           ? CountryCodeSource.FROM_NUMBER_WITH_IDD
2626           : CountryCodeSource.FROM_DEFAULT_COUNTRY;
2627  }
2628
2629  /**
2630   * Strips any national prefix (such as 0, 1) present in the number provided.
2631   *
2632   * @param number  the normalized telephone number that we wish to strip any national
2633   *     dialing prefix from
2634   * @param metadata  the metadata for the region that we think this number is from
2635   * @param carrierCode  a place to insert the carrier code if one is extracted
2636   * @return true if a national prefix or carrier code (or both) could be extracted.
2637   */
2638  // @VisibleForTesting
2639  boolean maybeStripNationalPrefixAndCarrierCode(
2640      StringBuilder number, PhoneMetadata metadata, StringBuilder carrierCode) {
2641    int numberLength = number.length();
2642    String possibleNationalPrefix = metadata.getNationalPrefixForParsing();
2643    if (numberLength == 0 || possibleNationalPrefix.length() == 0) {
2644      // Early return for numbers of zero length.
2645      return false;
2646    }
2647    // Attempt to parse the first digits as a national prefix.
2648    Matcher prefixMatcher = regexCache.getPatternForRegex(possibleNationalPrefix).matcher(number);
2649    if (prefixMatcher.lookingAt()) {
2650      Pattern nationalNumberRule =
2651          regexCache.getPatternForRegex(metadata.getGeneralDesc().getNationalNumberPattern());
2652      // Check if the original number is viable.
2653      boolean isViableOriginalNumber = nationalNumberRule.matcher(number).matches();
2654      // prefixMatcher.group(numOfGroups) == null implies nothing was captured by the capturing
2655      // groups in possibleNationalPrefix; therefore, no transformation is necessary, and we just
2656      // remove the national prefix.
2657      int numOfGroups = prefixMatcher.groupCount();
2658      String transformRule = metadata.getNationalPrefixTransformRule();
2659      if (transformRule == null || transformRule.length() == 0 ||
2660          prefixMatcher.group(numOfGroups) == null) {
2661        // If the original number was viable, and the resultant number is not, we return.
2662        if (isViableOriginalNumber &&
2663            !nationalNumberRule.matcher(number.substring(prefixMatcher.end())).matches()) {
2664          return false;
2665        }
2666        if (carrierCode != null && numOfGroups > 0 && prefixMatcher.group(numOfGroups) != null) {
2667          carrierCode.append(prefixMatcher.group(1));
2668        }
2669        number.delete(0, prefixMatcher.end());
2670        return true;
2671      } else {
2672        // Check that the resultant number is still viable. If not, return. Check this by copying
2673        // the string buffer and making the transformation on the copy first.
2674        StringBuilder transformedNumber = new StringBuilder(number);
2675        transformedNumber.replace(0, numberLength, prefixMatcher.replaceFirst(transformRule));
2676        if (isViableOriginalNumber &&
2677            !nationalNumberRule.matcher(transformedNumber.toString()).matches()) {
2678          return false;
2679        }
2680        if (carrierCode != null && numOfGroups > 1) {
2681          carrierCode.append(prefixMatcher.group(1));
2682        }
2683        number.replace(0, number.length(), transformedNumber.toString());
2684        return true;
2685      }
2686    }
2687    return false;
2688  }
2689
2690  /**
2691   * Strips any extension (as in, the part of the number dialled after the call is connected,
2692   * usually indicated with extn, ext, x or similar) from the end of the number, and returns it.
2693   *
2694   * @param number  the non-normalized telephone number that we wish to strip the extension from
2695   * @return        the phone extension
2696   */
2697  // @VisibleForTesting
2698  String maybeStripExtension(StringBuilder number) {
2699    Matcher m = EXTN_PATTERN.matcher(number);
2700    // If we find a potential extension, and the number preceding this is a viable number, we assume
2701    // it is an extension.
2702    if (m.find() && isViablePhoneNumber(number.substring(0, m.start()))) {
2703      // The numbers are captured into groups in the regular expression.
2704      for (int i = 1, length = m.groupCount(); i <= length; i++) {
2705        if (m.group(i) != null) {
2706          // We go through the capturing groups until we find one that captured some digits. If none
2707          // did, then we will return the empty string.
2708          String extension = m.group(i);
2709          number.delete(m.start(), number.length());
2710          return extension;
2711        }
2712      }
2713    }
2714    return "";
2715  }
2716
2717  /**
2718   * Checks to see that the region code used is valid, or if it is not valid, that the number to
2719   * parse starts with a + symbol so that we can attempt to infer the region from the number.
2720   * Returns false if it cannot use the region provided and the region cannot be inferred.
2721   */
2722  private boolean checkRegionForParsing(String numberToParse, String defaultRegion) {
2723    if (!isValidRegionCode(defaultRegion)) {
2724      // If the number is null or empty, we can't infer the region.
2725      if ((numberToParse == null) || (numberToParse.length() == 0) ||
2726          !PLUS_CHARS_PATTERN.matcher(numberToParse).lookingAt()) {
2727        return false;
2728      }
2729    }
2730    return true;
2731  }
2732
2733  /**
2734   * Parses a string and returns it in proto buffer format. This method will throw a
2735   * {@link com.google.i18n.phonenumbers.NumberParseException} if the number is not considered to be
2736   * a possible number. Note that validation of whether the number is actually a valid number for a
2737   * particular region is not performed. This can be done separately with {@link #isValidNumber}.
2738   *
2739   * @param numberToParse     number that we are attempting to parse. This can contain formatting
2740   *                          such as +, ( and -, as well as a phone number extension. It can also
2741   *                          be provided in RFC3966 format.
2742   * @param defaultRegion     region that we are expecting the number to be from. This is only used
2743   *                          if the number being parsed is not written in international format.
2744   *                          The country_code for the number in this case would be stored as that
2745   *                          of the default region supplied. If the number is guaranteed to
2746   *                          start with a '+' followed by the country calling code, then
2747   *                          "ZZ" or null can be supplied.
2748   * @return                  a phone number proto buffer filled with the parsed number
2749   * @throws NumberParseException  if the string is not considered to be a viable phone number or if
2750   *                               no default region was supplied and the number is not in
2751   *                               international format (does not start with +)
2752   */
2753  public PhoneNumber parse(String numberToParse, String defaultRegion)
2754      throws NumberParseException {
2755    PhoneNumber phoneNumber = new PhoneNumber();
2756    parse(numberToParse, defaultRegion, phoneNumber);
2757    return phoneNumber;
2758  }
2759
2760  /**
2761   * Same as {@link #parse(String, String)}, but accepts mutable PhoneNumber as a parameter to
2762   * decrease object creation when invoked many times.
2763   */
2764  public void parse(String numberToParse, String defaultRegion, PhoneNumber phoneNumber)
2765      throws NumberParseException {
2766    parseHelper(numberToParse, defaultRegion, false, true, phoneNumber);
2767  }
2768
2769  /**
2770   * Parses a string and returns it in proto buffer format. This method differs from {@link #parse}
2771   * in that it always populates the raw_input field of the protocol buffer with numberToParse as
2772   * well as the country_code_source field.
2773   *
2774   * @param numberToParse     number that we are attempting to parse. This can contain formatting
2775   *                          such as +, ( and -, as well as a phone number extension.
2776   * @param defaultRegion     region that we are expecting the number to be from. This is only used
2777   *                          if the number being parsed is not written in international format.
2778   *                          The country calling code for the number in this case would be stored
2779   *                          as that of the default region supplied.
2780   * @return                  a phone number proto buffer filled with the parsed number
2781   * @throws NumberParseException  if the string is not considered to be a viable phone number or if
2782   *                               no default region was supplied
2783   */
2784  public PhoneNumber parseAndKeepRawInput(String numberToParse, String defaultRegion)
2785      throws NumberParseException {
2786    PhoneNumber phoneNumber = new PhoneNumber();
2787    parseAndKeepRawInput(numberToParse, defaultRegion, phoneNumber);
2788    return phoneNumber;
2789  }
2790
2791  /**
2792   * Same as{@link #parseAndKeepRawInput(String, String)}, but accepts a mutable PhoneNumber as
2793   * a parameter to decrease object creation when invoked many times.
2794   */
2795  public void parseAndKeepRawInput(String numberToParse, String defaultRegion,
2796                                   PhoneNumber phoneNumber)
2797      throws NumberParseException {
2798    parseHelper(numberToParse, defaultRegion, true, true, phoneNumber);
2799  }
2800
2801  /**
2802   * Returns an iterable over all {@link PhoneNumberMatch PhoneNumberMatches} in {@code text}. This
2803   * is a shortcut for {@link #findNumbers(CharSequence, String, Leniency, long)
2804   * getMatcher(text, defaultRegion, Leniency.VALID, Long.MAX_VALUE)}.
2805   *
2806   * @param text              the text to search for phone numbers, null for no text
2807   * @param defaultRegion     region that we are expecting the number to be from. This is only used
2808   *                          if the number being parsed is not written in international format. The
2809   *                          country_code for the number in this case would be stored as that of
2810   *                          the default region supplied. May be null if only international
2811   *                          numbers are expected.
2812   */
2813  public Iterable<PhoneNumberMatch> findNumbers(CharSequence text, String defaultRegion) {
2814    return findNumbers(text, defaultRegion, Leniency.VALID, Long.MAX_VALUE);
2815  }
2816
2817  /**
2818   * Returns an iterable over all {@link PhoneNumberMatch PhoneNumberMatches} in {@code text}.
2819   *
2820   * @param text              the text to search for phone numbers, null for no text
2821   * @param defaultRegion     region that we are expecting the number to be from. This is only used
2822   *                          if the number being parsed is not written in international format. The
2823   *                          country_code for the number in this case would be stored as that of
2824   *                          the default region supplied. May be null if only international
2825   *                          numbers are expected.
2826   * @param leniency          the leniency to use when evaluating candidate phone numbers
2827   * @param maxTries          the maximum number of invalid numbers to try before giving up on the
2828   *                          text. This is to cover degenerate cases where the text has a lot of
2829   *                          false positives in it. Must be {@code >= 0}.
2830   */
2831  public Iterable<PhoneNumberMatch> findNumbers(
2832      final CharSequence text, final String defaultRegion, final Leniency leniency,
2833      final long maxTries) {
2834
2835    return new Iterable<PhoneNumberMatch>() {
2836      @Override
2837      public Iterator<PhoneNumberMatch> iterator() {
2838        return new PhoneNumberMatcher(
2839            PhoneNumberUtil.this, text, defaultRegion, leniency, maxTries);
2840      }
2841    };
2842  }
2843
2844  /**
2845   * A helper function to set the values related to leading zeros in a PhoneNumber.
2846   */
2847  static void setItalianLeadingZerosForPhoneNumber(String nationalNumber, PhoneNumber phoneNumber) {
2848    if (nationalNumber.length() > 1 && nationalNumber.charAt(0) == '0') {
2849      phoneNumber.setItalianLeadingZero(true);
2850      int numberOfLeadingZeros = 1;
2851      // Note that if the national number is all "0"s, the last "0" is not counted as a leading
2852      // zero.
2853      while (numberOfLeadingZeros < nationalNumber.length() - 1 &&
2854             nationalNumber.charAt(numberOfLeadingZeros) == '0') {
2855        numberOfLeadingZeros++;
2856      }
2857      if (numberOfLeadingZeros != 1) {
2858        phoneNumber.setNumberOfLeadingZeros(numberOfLeadingZeros);
2859      }
2860    }
2861  }
2862
2863  /**
2864   * Parses a string and fills up the phoneNumber. This method is the same as the public
2865   * parse() method, with the exception that it allows the default region to be null, for use by
2866   * isNumberMatch(). checkRegion should be set to false if it is permitted for the default region
2867   * to be null or unknown ("ZZ").
2868   */
2869  private void parseHelper(String numberToParse, String defaultRegion, boolean keepRawInput,
2870                           boolean checkRegion, PhoneNumber phoneNumber)
2871      throws NumberParseException {
2872    if (numberToParse == null) {
2873      throw new NumberParseException(NumberParseException.ErrorType.NOT_A_NUMBER,
2874                                     "The phone number supplied was null.");
2875    } else if (numberToParse.length() > MAX_INPUT_STRING_LENGTH) {
2876      throw new NumberParseException(NumberParseException.ErrorType.TOO_LONG,
2877                                     "The string supplied was too long to parse.");
2878    }
2879
2880    StringBuilder nationalNumber = new StringBuilder();
2881    buildNationalNumberForParsing(numberToParse, nationalNumber);
2882
2883    if (!isViablePhoneNumber(nationalNumber.toString())) {
2884      throw new NumberParseException(NumberParseException.ErrorType.NOT_A_NUMBER,
2885                                     "The string supplied did not seem to be a phone number.");
2886    }
2887
2888    // Check the region supplied is valid, or that the extracted number starts with some sort of +
2889    // sign so the number's region can be determined.
2890    if (checkRegion && !checkRegionForParsing(nationalNumber.toString(), defaultRegion)) {
2891      throw new NumberParseException(NumberParseException.ErrorType.INVALID_COUNTRY_CODE,
2892                                     "Missing or invalid default region.");
2893    }
2894
2895    if (keepRawInput) {
2896      phoneNumber.setRawInput(numberToParse);
2897    }
2898    // Attempt to parse extension first, since it doesn't require region-specific data and we want
2899    // to have the non-normalised number here.
2900    String extension = maybeStripExtension(nationalNumber);
2901    if (extension.length() > 0) {
2902      phoneNumber.setExtension(extension);
2903    }
2904
2905    PhoneMetadata regionMetadata = getMetadataForRegion(defaultRegion);
2906    // Check to see if the number is given in international format so we know whether this number is
2907    // from the default region or not.
2908    StringBuilder normalizedNationalNumber = new StringBuilder();
2909    int countryCode = 0;
2910    try {
2911      // TODO: This method should really just take in the string buffer that has already
2912      // been created, and just remove the prefix, rather than taking in a string and then
2913      // outputting a string buffer.
2914      countryCode = maybeExtractCountryCode(nationalNumber.toString(), regionMetadata,
2915                                            normalizedNationalNumber, keepRawInput, phoneNumber);
2916    } catch (NumberParseException e) {
2917      Matcher matcher = PLUS_CHARS_PATTERN.matcher(nationalNumber.toString());
2918      if (e.getErrorType() == NumberParseException.ErrorType.INVALID_COUNTRY_CODE &&
2919          matcher.lookingAt()) {
2920        // Strip the plus-char, and try again.
2921        countryCode = maybeExtractCountryCode(nationalNumber.substring(matcher.end()),
2922                                              regionMetadata, normalizedNationalNumber,
2923                                              keepRawInput, phoneNumber);
2924        if (countryCode == 0) {
2925          throw new NumberParseException(NumberParseException.ErrorType.INVALID_COUNTRY_CODE,
2926                                         "Could not interpret numbers after plus-sign.");
2927        }
2928      } else {
2929        throw new NumberParseException(e.getErrorType(), e.getMessage());
2930      }
2931    }
2932    if (countryCode != 0) {
2933      String phoneNumberRegion = getRegionCodeForCountryCode(countryCode);
2934      if (!phoneNumberRegion.equals(defaultRegion)) {
2935        // Metadata cannot be null because the country calling code is valid.
2936        regionMetadata = getMetadataForRegionOrCallingCode(countryCode, phoneNumberRegion);
2937      }
2938    } else {
2939      // If no extracted country calling code, use the region supplied instead. The national number
2940      // is just the normalized version of the number we were given to parse.
2941      normalize(nationalNumber);
2942      normalizedNationalNumber.append(nationalNumber);
2943      if (defaultRegion != null) {
2944        countryCode = regionMetadata.getCountryCode();
2945        phoneNumber.setCountryCode(countryCode);
2946      } else if (keepRawInput) {
2947        phoneNumber.clearCountryCodeSource();
2948      }
2949    }
2950    if (normalizedNationalNumber.length() < MIN_LENGTH_FOR_NSN) {
2951      throw new NumberParseException(NumberParseException.ErrorType.TOO_SHORT_NSN,
2952                                     "The string supplied is too short to be a phone number.");
2953    }
2954    if (regionMetadata != null) {
2955      StringBuilder carrierCode = new StringBuilder();
2956      StringBuilder potentialNationalNumber = new StringBuilder(normalizedNationalNumber);
2957      maybeStripNationalPrefixAndCarrierCode(potentialNationalNumber, regionMetadata, carrierCode);
2958      // We require that the NSN remaining after stripping the national prefix and carrier code be
2959      // of a possible length for the region. Otherwise, we don't do the stripping, since the
2960      // original number could be a valid short number.
2961      if (!isShorterThanPossibleNormalNumber(regionMetadata, potentialNationalNumber.toString())) {
2962        normalizedNationalNumber = potentialNationalNumber;
2963        if (keepRawInput) {
2964          phoneNumber.setPreferredDomesticCarrierCode(carrierCode.toString());
2965        }
2966      }
2967    }
2968    int lengthOfNationalNumber = normalizedNationalNumber.length();
2969    if (lengthOfNationalNumber < MIN_LENGTH_FOR_NSN) {
2970      throw new NumberParseException(NumberParseException.ErrorType.TOO_SHORT_NSN,
2971                                     "The string supplied is too short to be a phone number.");
2972    }
2973    if (lengthOfNationalNumber > MAX_LENGTH_FOR_NSN) {
2974      throw new NumberParseException(NumberParseException.ErrorType.TOO_LONG,
2975                                     "The string supplied is too long to be a phone number.");
2976    }
2977    setItalianLeadingZerosForPhoneNumber(normalizedNationalNumber.toString(), phoneNumber);
2978    phoneNumber.setNationalNumber(Long.parseLong(normalizedNationalNumber.toString()));
2979  }
2980
2981  /**
2982   * Converts numberToParse to a form that we can parse and write it to nationalNumber if it is
2983   * written in RFC3966; otherwise extract a possible number out of it and write to nationalNumber.
2984   */
2985  private void buildNationalNumberForParsing(String numberToParse, StringBuilder nationalNumber) {
2986    int indexOfPhoneContext = numberToParse.indexOf(RFC3966_PHONE_CONTEXT);
2987    if (indexOfPhoneContext > 0) {
2988      int phoneContextStart = indexOfPhoneContext + RFC3966_PHONE_CONTEXT.length();
2989      // If the phone context contains a phone number prefix, we need to capture it, whereas domains
2990      // will be ignored.
2991      if (numberToParse.charAt(phoneContextStart) == PLUS_SIGN) {
2992        // Additional parameters might follow the phone context. If so, we will remove them here
2993        // because the parameters after phone context are not important for parsing the
2994        // phone number.
2995        int phoneContextEnd = numberToParse.indexOf(';', phoneContextStart);
2996        if (phoneContextEnd > 0) {
2997          nationalNumber.append(numberToParse.substring(phoneContextStart, phoneContextEnd));
2998        } else {
2999          nationalNumber.append(numberToParse.substring(phoneContextStart));
3000        }
3001      }
3002
3003      // Now append everything between the "tel:" prefix and the phone-context. This should include
3004      // the national number, an optional extension or isdn-subaddress component. Note we also
3005      // handle the case when "tel:" is missing, as we have seen in some of the phone number inputs.
3006      // In that case, we append everything from the beginning.
3007      int indexOfRfc3966Prefix = numberToParse.indexOf(RFC3966_PREFIX);
3008      int indexOfNationalNumber = (indexOfRfc3966Prefix >= 0) ?
3009          indexOfRfc3966Prefix + RFC3966_PREFIX.length() : 0;
3010      nationalNumber.append(numberToParse.substring(indexOfNationalNumber, indexOfPhoneContext));
3011    } else {
3012      // Extract a possible number from the string passed in (this strips leading characters that
3013      // could not be the start of a phone number.)
3014      nationalNumber.append(extractPossibleNumber(numberToParse));
3015    }
3016
3017    // Delete the isdn-subaddress and everything after it if it is present. Note extension won't
3018    // appear at the same time with isdn-subaddress according to paragraph 5.3 of the RFC3966 spec,
3019    int indexOfIsdn = nationalNumber.indexOf(RFC3966_ISDN_SUBADDRESS);
3020    if (indexOfIsdn > 0) {
3021      nationalNumber.delete(indexOfIsdn, nationalNumber.length());
3022    }
3023    // If both phone context and isdn-subaddress are absent but other parameters are present, the
3024    // parameters are left in nationalNumber. This is because we are concerned about deleting
3025    // content from a potential number string when there is no strong evidence that the number is
3026    // actually written in RFC3966.
3027  }
3028
3029  /**
3030   * Takes two phone numbers and compares them for equality.
3031   *
3032   * <p>Returns EXACT_MATCH if the country_code, NSN, presence of a leading zero for Italian numbers
3033   * and any extension present are the same.
3034   * Returns NSN_MATCH if either or both has no region specified, and the NSNs and extensions are
3035   * the same.
3036   * Returns SHORT_NSN_MATCH if either or both has no region specified, or the region specified is
3037   * the same, and one NSN could be a shorter version of the other number. This includes the case
3038   * where one has an extension specified, and the other does not.
3039   * Returns NO_MATCH otherwise.
3040   * For example, the numbers +1 345 657 1234 and 657 1234 are a SHORT_NSN_MATCH.
3041   * The numbers +1 345 657 1234 and 345 657 are a NO_MATCH.
3042   *
3043   * @param firstNumberIn  first number to compare
3044   * @param secondNumberIn  second number to compare
3045   *
3046   * @return  NO_MATCH, SHORT_NSN_MATCH, NSN_MATCH or EXACT_MATCH depending on the level of equality
3047   *     of the two numbers, described in the method definition.
3048   */
3049  public MatchType isNumberMatch(PhoneNumber firstNumberIn, PhoneNumber secondNumberIn) {
3050    // Make copies of the phone number so that the numbers passed in are not edited.
3051    PhoneNumber firstNumber = new PhoneNumber();
3052    firstNumber.mergeFrom(firstNumberIn);
3053    PhoneNumber secondNumber = new PhoneNumber();
3054    secondNumber.mergeFrom(secondNumberIn);
3055    // First clear raw_input, country_code_source and preferred_domestic_carrier_code fields and any
3056    // empty-string extensions so that we can use the proto-buffer equality method.
3057    firstNumber.clearRawInput();
3058    firstNumber.clearCountryCodeSource();
3059    firstNumber.clearPreferredDomesticCarrierCode();
3060    secondNumber.clearRawInput();
3061    secondNumber.clearCountryCodeSource();
3062    secondNumber.clearPreferredDomesticCarrierCode();
3063    if (firstNumber.hasExtension() &&
3064        firstNumber.getExtension().length() == 0) {
3065        firstNumber.clearExtension();
3066    }
3067    if (secondNumber.hasExtension() &&
3068        secondNumber.getExtension().length() == 0) {
3069        secondNumber.clearExtension();
3070    }
3071    // Early exit if both had extensions and these are different.
3072    if (firstNumber.hasExtension() && secondNumber.hasExtension() &&
3073        !firstNumber.getExtension().equals(secondNumber.getExtension())) {
3074      return MatchType.NO_MATCH;
3075    }
3076    int firstNumberCountryCode = firstNumber.getCountryCode();
3077    int secondNumberCountryCode = secondNumber.getCountryCode();
3078    // Both had country_code specified.
3079    if (firstNumberCountryCode != 0 && secondNumberCountryCode != 0) {
3080      if (firstNumber.exactlySameAs(secondNumber)) {
3081        return MatchType.EXACT_MATCH;
3082      } else if (firstNumberCountryCode == secondNumberCountryCode &&
3083                 isNationalNumberSuffixOfTheOther(firstNumber, secondNumber)) {
3084        // A SHORT_NSN_MATCH occurs if there is a difference because of the presence or absence of
3085        // an 'Italian leading zero', the presence or absence of an extension, or one NSN being a
3086        // shorter variant of the other.
3087        return MatchType.SHORT_NSN_MATCH;
3088      }
3089      // This is not a match.
3090      return MatchType.NO_MATCH;
3091    }
3092    // Checks cases where one or both country_code fields were not specified. To make equality
3093    // checks easier, we first set the country_code fields to be equal.
3094    firstNumber.setCountryCode(secondNumberCountryCode);
3095    // If all else was the same, then this is an NSN_MATCH.
3096    if (firstNumber.exactlySameAs(secondNumber)) {
3097      return MatchType.NSN_MATCH;
3098    }
3099    if (isNationalNumberSuffixOfTheOther(firstNumber, secondNumber)) {
3100      return MatchType.SHORT_NSN_MATCH;
3101    }
3102    return MatchType.NO_MATCH;
3103  }
3104
3105  // Returns true when one national number is the suffix of the other or both are the same.
3106  private boolean isNationalNumberSuffixOfTheOther(PhoneNumber firstNumber,
3107                                                   PhoneNumber secondNumber) {
3108    String firstNumberNationalNumber = String.valueOf(firstNumber.getNationalNumber());
3109    String secondNumberNationalNumber = String.valueOf(secondNumber.getNationalNumber());
3110    // Note that endsWith returns true if the numbers are equal.
3111    return firstNumberNationalNumber.endsWith(secondNumberNationalNumber) ||
3112           secondNumberNationalNumber.endsWith(firstNumberNationalNumber);
3113  }
3114
3115  /**
3116   * Takes two phone numbers as strings and compares them for equality. This is a convenience
3117   * wrapper for {@link #isNumberMatch(PhoneNumber, PhoneNumber)}. No default region is known.
3118   *
3119   * @param firstNumber  first number to compare. Can contain formatting, and can have country
3120   *     calling code specified with + at the start.
3121   * @param secondNumber  second number to compare. Can contain formatting, and can have country
3122   *     calling code specified with + at the start.
3123   * @return  NOT_A_NUMBER, NO_MATCH, SHORT_NSN_MATCH, NSN_MATCH, EXACT_MATCH. See
3124   *     {@link #isNumberMatch(PhoneNumber, PhoneNumber)} for more details.
3125   */
3126  public MatchType isNumberMatch(String firstNumber, String secondNumber) {
3127    try {
3128      PhoneNumber firstNumberAsProto = parse(firstNumber, UNKNOWN_REGION);
3129      return isNumberMatch(firstNumberAsProto, secondNumber);
3130    } catch (NumberParseException e) {
3131      if (e.getErrorType() == NumberParseException.ErrorType.INVALID_COUNTRY_CODE) {
3132        try {
3133          PhoneNumber secondNumberAsProto = parse(secondNumber, UNKNOWN_REGION);
3134          return isNumberMatch(secondNumberAsProto, firstNumber);
3135        } catch (NumberParseException e2) {
3136          if (e2.getErrorType() == NumberParseException.ErrorType.INVALID_COUNTRY_CODE) {
3137            try {
3138              PhoneNumber firstNumberProto = new PhoneNumber();
3139              PhoneNumber secondNumberProto = new PhoneNumber();
3140              parseHelper(firstNumber, null, false, false, firstNumberProto);
3141              parseHelper(secondNumber, null, false, false, secondNumberProto);
3142              return isNumberMatch(firstNumberProto, secondNumberProto);
3143            } catch (NumberParseException e3) {
3144              // Fall through and return MatchType.NOT_A_NUMBER.
3145            }
3146          }
3147        }
3148      }
3149    }
3150    // One or more of the phone numbers we are trying to match is not a viable phone number.
3151    return MatchType.NOT_A_NUMBER;
3152  }
3153
3154  /**
3155   * Takes two phone numbers and compares them for equality. This is a convenience wrapper for
3156   * {@link #isNumberMatch(PhoneNumber, PhoneNumber)}. No default region is known.
3157   *
3158   * @param firstNumber  first number to compare in proto buffer format.
3159   * @param secondNumber  second number to compare. Can contain formatting, and can have country
3160   *     calling code specified with + at the start.
3161   * @return  NOT_A_NUMBER, NO_MATCH, SHORT_NSN_MATCH, NSN_MATCH, EXACT_MATCH. See
3162   *     {@link #isNumberMatch(PhoneNumber, PhoneNumber)} for more details.
3163   */
3164  public MatchType isNumberMatch(PhoneNumber firstNumber, String secondNumber) {
3165    // First see if the second number has an implicit country calling code, by attempting to parse
3166    // it.
3167    try {
3168      PhoneNumber secondNumberAsProto = parse(secondNumber, UNKNOWN_REGION);
3169      return isNumberMatch(firstNumber, secondNumberAsProto);
3170    } catch (NumberParseException e) {
3171      if (e.getErrorType() == NumberParseException.ErrorType.INVALID_COUNTRY_CODE) {
3172        // The second number has no country calling code. EXACT_MATCH is no longer possible.
3173        // We parse it as if the region was the same as that for the first number, and if
3174        // EXACT_MATCH is returned, we replace this with NSN_MATCH.
3175        String firstNumberRegion = getRegionCodeForCountryCode(firstNumber.getCountryCode());
3176        try {
3177          if (!firstNumberRegion.equals(UNKNOWN_REGION)) {
3178            PhoneNumber secondNumberWithFirstNumberRegion = parse(secondNumber, firstNumberRegion);
3179            MatchType match = isNumberMatch(firstNumber, secondNumberWithFirstNumberRegion);
3180            if (match == MatchType.EXACT_MATCH) {
3181              return MatchType.NSN_MATCH;
3182            }
3183            return match;
3184          } else {
3185            // If the first number didn't have a valid country calling code, then we parse the
3186            // second number without one as well.
3187            PhoneNumber secondNumberProto = new PhoneNumber();
3188            parseHelper(secondNumber, null, false, false, secondNumberProto);
3189            return isNumberMatch(firstNumber, secondNumberProto);
3190          }
3191        } catch (NumberParseException e2) {
3192          // Fall-through to return NOT_A_NUMBER.
3193        }
3194      }
3195    }
3196    // One or more of the phone numbers we are trying to match is not a viable phone number.
3197    return MatchType.NOT_A_NUMBER;
3198  }
3199
3200  /**
3201   * Returns true if the number can be dialled from outside the region, or unknown. If the number
3202   * can only be dialled from within the region, returns false. Does not check the number is a valid
3203   * number. Note that, at the moment, this method does not handle short numbers.
3204   * TODO: Make this method public when we have enough metadata to make it worthwhile.
3205   *
3206   * @param number  the phone-number for which we want to know whether it is diallable from
3207   *     outside the region
3208   */
3209  // @VisibleForTesting
3210  boolean canBeInternationallyDialled(PhoneNumber number) {
3211    PhoneMetadata metadata = getMetadataForRegion(getRegionCodeForNumber(number));
3212    if (metadata == null) {
3213      // Note numbers belonging to non-geographical entities (e.g. +800 numbers) are always
3214      // internationally diallable, and will be caught here.
3215      return true;
3216    }
3217    String nationalSignificantNumber = getNationalSignificantNumber(number);
3218    return !isNumberMatchingDesc(nationalSignificantNumber, metadata.getNoInternationalDialling());
3219  }
3220
3221  /**
3222   * Returns true if the supplied region supports mobile number portability. Returns false for
3223   * invalid, unknown or regions that don't support mobile number portability.
3224   *
3225   * @param regionCode  the region for which we want to know whether it supports mobile number
3226   *                    portability or not.
3227   */
3228  public boolean isMobileNumberPortableRegion(String regionCode) {
3229    PhoneMetadata metadata = getMetadataForRegion(regionCode);
3230    if (metadata == null) {
3231      logger.log(Level.WARNING, "Invalid or unknown region code provided: " + regionCode);
3232      return false;
3233    }
3234    return metadata.isMobileNumberPortableRegion();
3235  }
3236}
3237