@@ -3142,7 +3142,10 @@ private boolean checkRegionForParsing(CharSequence numberToParse, String default
31423142 public PhoneNumber parse (CharSequence numberToParse , String defaultRegion )
31433143 throws NumberParseException {
31443144 PhoneNumber phoneNumber = new PhoneNumber ();
3145- parse (numberToParse , defaultRegion , phoneNumber );
3145+ parseWithOptions (
3146+ numberToParse ,
3147+ new ParsingOptions ().setDefaultRegion (defaultRegion ),
3148+ phoneNumber );
31463149 return phoneNumber ;
31473150 }
31483151
@@ -3154,7 +3157,10 @@ public PhoneNumber parse(CharSequence numberToParse, String defaultRegion)
31543157 @ Deprecated
31553158 public void parse (CharSequence numberToParse , String defaultRegion , PhoneNumber phoneNumber )
31563159 throws NumberParseException {
3157- parseHelper (numberToParse , defaultRegion , false , true , phoneNumber );
3160+ parseWithOptions (
3161+ numberToParse ,
3162+ new ParsingOptions ().setDefaultRegion (defaultRegion ),
3163+ phoneNumber );
31583164 }
31593165
31603166 /**
@@ -3176,7 +3182,10 @@ public void parse(CharSequence numberToParse, String defaultRegion, PhoneNumber
31763182 public PhoneNumber parseAndKeepRawInput (CharSequence numberToParse , String defaultRegion )
31773183 throws NumberParseException {
31783184 PhoneNumber phoneNumber = new PhoneNumber ();
3179- parseAndKeepRawInput (numberToParse , defaultRegion , phoneNumber );
3185+ parseWithOptions (
3186+ numberToParse ,
3187+ new ParsingOptions ().setKeepRawInput (true ).setDefaultRegion (defaultRegion ),
3188+ phoneNumber );
31803189 return phoneNumber ;
31813190 }
31823191
@@ -3188,20 +3197,57 @@ public PhoneNumber parseAndKeepRawInput(CharSequence numberToParse, String defau
31883197 @ Deprecated
31893198 public void parseAndKeepRawInput (CharSequence numberToParse , String defaultRegion , PhoneNumber phoneNumber )
31903199 throws NumberParseException {
3191- parseHelper (numberToParse , defaultRegion , true , true , phoneNumber );
3200+ parseWithOptions (
3201+ numberToParse ,
3202+ new ParsingOptions ().setKeepRawInput (true ).setDefaultRegion (defaultRegion ),
3203+ phoneNumber );
31923204 }
31933205
3206+ /**
3207+ * Parses a string and returns it as a PhoneNumber object. The method is quite lenient and looks
3208+ * for a number in the input text (raw input) and does not check whether the string is definitely
3209+ * only a phone number. To do this, it ignores punctuation and white-space, as well as any text
3210+ * before the number (e.g. a leading "Tel: ") and trims the non-number bits. It will accept a
3211+ * number in any format (E164, national, international etc), assuming it can be interpreted with
3212+ * the options supplied. It also attempts to convert any alpha characters into digits if it thinks
3213+ * this is a vanity number of the type "1800 MICROSOFT".
3214+ *
3215+ * <p>This method will throw a {@link com.google.i18n.phonenumbers.NumberParseException} if the
3216+ * number is not considered to be a possible number. Note that validation of whether the number is
3217+ * actually a valid number for a particular region is not performed. This can be done separately
3218+ * with {@link #isValidNumber}.
3219+ *
3220+ * <p>Note this method canonicalizes the phone number such that different representations can be
3221+ * easily compared, no matter what form it was originally entered in (e.g. national,
3222+ * international). If you want to record context about the number being parsed, such as the raw
3223+ * input that was entered, how the country code was derived etc. then set the `keepRawInput`
3224+ * option in the options accordingly.
3225+ *
3226+ * @param numberToParse number that we are attempting to parse. This can contain formatting such
3227+ * as +, ( and -, as well as a phone number extension. It can also be provided in RFC3966
3228+ * format.
3229+ * @param options options to configure the behavior of the parser. See {@link ParsingOptions} for
3230+ * more details.
3231+ * @return a phone number proto buffer filled with the parsed number.
3232+ * @throws NumberParseException if the string is not considered to be a viable phone number (e.g.
3233+ * too few or too many digits) or if no default region was supplied and the number is not in
3234+ * international format (does not start with +).
3235+ */
31943236 public PhoneNumber parseWithOptions (CharSequence numberToParse , ParsingOptions options )
31953237 throws NumberParseException {
31963238 PhoneNumber phoneNumber = new PhoneNumber ();
3197- parseHelper (numberToParse , options . getDefaultRegion (), options . hasKeepRawInput (), options . hasDefaultRegion () , phoneNumber );
3239+ parseWithOptions (numberToParse , options , phoneNumber );
31983240 return phoneNumber ;
31993241 }
32003242
3243+ /**
3244+ * Same as{@link #parseWithOptions(CharSequence, ParsingOptions)}, but accepts a mutable
3245+ * PhoneNumber as a parameter to decrease object creation when invoked many times.
3246+ */
32013247 public void parseWithOptions (CharSequence numberToParse , ParsingOptions options , PhoneNumber phoneNumber )
32023248 throws NumberParseException {
3203- parseHelper (numberToParse , options .getDefaultRegion (), options .hasKeepRawInput (), options . hasDefaultRegion () , phoneNumber );
3204- }
3249+ parseHelper (numberToParse , options .getDefaultRegion (), options .isKeepRawInput (), true , phoneNumber );
3250+ }
32053251
32063252 /**
32073253 * Returns an iterable over all {@link PhoneNumberMatch PhoneNumberMatches} in {@code text}. This
0 commit comments