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