From ed15e472459e4f170fd4fa9ee6a2c88fcb1011e0 Mon Sep 17 00:00:00 2001 From: karoljk Date: Fri, 22 Nov 2024 09:35:52 +0000 Subject: [PATCH 1/7] Add parsingOptions to OpenSource I have added the new parsing methode ParsingOptions to the OpenSource version. --- .../i18n/phonenumbers/ParsingOptions.java | 61 +++++++++++++++++++ .../i18n/phonenumbers/PhoneNumberUtil.java | 23 ++++++- 2 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 java/libphonenumber/src/com/google/i18n/phonenumbers/ParsingOptions.java diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/ParsingOptions.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/ParsingOptions.java new file mode 100644 index 0000000000..5e83a46258 --- /dev/null +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/ParsingOptions.java @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2024 The Libphonenumber Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.i18n.phonenumbers; + +/** Options for the phone number parser. */ +public class ParsingOptions { + /** + * Returns the region we are expecting the number to be from. This is ignored if the number being + * parsed is written in international format. In case of national format, the country_code will be + * set to the one of this default region. If the number is guaranteed to start with a '+' followed + * by the country calling code, then RegionCode.ZZ or null can be supplied. + */ + + private boolean hasDefaultRegion; + private String defaultRegion_ = null; + public boolean hasDefaultRegion() { return hasDefaultRegion; } + public String getDefaultRegion() { return defaultRegion_; } + public ParsingOptions setDefaultRegion(String value) { + if (value == null) { + throw new NullPointerException(); + } + hasDefaultRegion = true; + defaultRegion_ = value; + return this; + } + + /** + * Returns whether the raw input should be kept in the PhoneNumber object. If true, the raw_input + * field and country_code_source fields will be populated. + */ + private boolean hasKeepRawInput; + private boolean keepRawInput_ = false; + public boolean hasKeepRawInput() { return hasKeepRawInput; } + public boolean isKeepRawInput() { return keepRawInput_; } + public ParsingOptions setKeepRawInput(boolean value) { + if(value == false) { + + } + hasKeepRawInput = true; + keepRawInput_ = value; + return this; + } + + public ParsingOptions withDefaultRegion(String regionCode) { + return setDefaultRegion(regionCode); + } +} \ No newline at end of file diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java index e85cb65b52..c0ab592f63 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java @@ -3136,7 +3136,9 @@ private boolean checkRegionForParsing(CharSequence numberToParse, String default * @throws NumberParseException if the string is not considered to be a viable phone number (e.g. * too few or too many digits) or if no default region was supplied and the number is not in * international format (does not start with +) + * @deprecated Use {@link #parseWithOptions(CharSequence, ParsingOptions)} instead. */ + @Deprecated public PhoneNumber parse(CharSequence numberToParse, String defaultRegion) throws NumberParseException { PhoneNumber phoneNumber = new PhoneNumber(); @@ -3147,7 +3149,9 @@ public PhoneNumber parse(CharSequence numberToParse, String defaultRegion) /** * Same as {@link #parse(CharSequence, String)}, but accepts mutable PhoneNumber as a * parameter to decrease object creation when invoked many times. + * @deprecated Use {@link #parseWithOptions(CharSequence, ParsingOptions, PhoneNumber)} instead. */ + @Deprecated public void parse(CharSequence numberToParse, String defaultRegion, PhoneNumber phoneNumber) throws NumberParseException { parseHelper(numberToParse, defaultRegion, false, true, phoneNumber); @@ -3166,7 +3170,9 @@ public void parse(CharSequence numberToParse, String defaultRegion, PhoneNumber * @return a phone number proto buffer filled with the parsed number * @throws NumberParseException if the string is not considered to be a viable phone number or if * no default region was supplied + * @deprecated Use {@link #parseWithOptions(CharSequence, ParsingOptions)} instead. */ + @Deprecated public PhoneNumber parseAndKeepRawInput(CharSequence numberToParse, String defaultRegion) throws NumberParseException { PhoneNumber phoneNumber = new PhoneNumber(); @@ -3177,13 +3183,26 @@ public PhoneNumber parseAndKeepRawInput(CharSequence numberToParse, String defau /** * Same as{@link #parseAndKeepRawInput(CharSequence, String)}, but accepts a mutable * PhoneNumber as a parameter to decrease object creation when invoked many times. + * @deprecated Use {@link #parseWithOptions(CharSequence, ParsingOptions, PhoneNumber)} instead. */ - public void parseAndKeepRawInput(CharSequence numberToParse, String defaultRegion, - PhoneNumber phoneNumber) + @Deprecated + public void parseAndKeepRawInput(CharSequence numberToParse, String defaultRegion, PhoneNumber phoneNumber) throws NumberParseException { parseHelper(numberToParse, defaultRegion, true, true, phoneNumber); } + public PhoneNumber parseWithOptions(CharSequence numberToParse, ParsingOptions options) + throws NumberParseException { + PhoneNumber phoneNumber = new PhoneNumber(); + parseHelper(numberToParse, options.getDefaultRegion(), options.hasKeepRawInput(), options.hasDefaultRegion(), phoneNumber); + return phoneNumber; + } + + public void parseWithOptions(CharSequence numberToParse, ParsingOptions options, PhoneNumber phoneNumber) + throws NumberParseException { + parseHelper(numberToParse, options.getDefaultRegion(), options.hasKeepRawInput(), options.hasDefaultRegion(), phoneNumber); + } + /** * Returns an iterable over all {@link PhoneNumberMatch PhoneNumberMatches} in {@code text}. This * is a shortcut for {@link #findNumbers(CharSequence, String, Leniency, long) From f5c2748872e8d318930d5967485eccd4524fa281 Mon Sep 17 00:00:00 2001 From: karoljk Date: Wed, 27 Nov 2024 12:55:46 +0000 Subject: [PATCH 2/7] Create new class ParsingOptions There is a new class called PrasingOptions. With it we can replace old parsing methodes such as keepRawInput or defaultRegion. --- .../i18n/phonenumbers/ParsingOptions.java | 5 +- .../i18n/phonenumbers/PhoneNumberUtil.java | 60 ++- .../phonenumbers/PhoneNumberUtilTest.java | 412 +++++++++--------- 3 files changed, 260 insertions(+), 217 deletions(-) diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/ParsingOptions.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/ParsingOptions.java index 5e83a46258..1fc1ceb7ec 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/ParsingOptions.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/ParsingOptions.java @@ -30,10 +30,7 @@ public class ParsingOptions { public boolean hasDefaultRegion() { return hasDefaultRegion; } public String getDefaultRegion() { return defaultRegion_; } public ParsingOptions setDefaultRegion(String value) { - if (value == null) { - throw new NullPointerException(); - } - hasDefaultRegion = true; + hasDefaultRegion = (value != null); defaultRegion_ = value; return this; } diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java index c0ab592f63..b8100b08ae 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java @@ -3142,7 +3142,10 @@ private boolean checkRegionForParsing(CharSequence numberToParse, String default public PhoneNumber parse(CharSequence numberToParse, String defaultRegion) throws NumberParseException { PhoneNumber phoneNumber = new PhoneNumber(); - parse(numberToParse, defaultRegion, phoneNumber); + parseWithOptions( + numberToParse, + new ParsingOptions().setDefaultRegion(defaultRegion), + phoneNumber); return phoneNumber; } @@ -3154,7 +3157,10 @@ public PhoneNumber parse(CharSequence numberToParse, String defaultRegion) @Deprecated public void parse(CharSequence numberToParse, String defaultRegion, PhoneNumber phoneNumber) throws NumberParseException { - parseHelper(numberToParse, defaultRegion, false, true, phoneNumber); + parseWithOptions( + numberToParse, + new ParsingOptions().setDefaultRegion(defaultRegion), + phoneNumber); } /** @@ -3176,7 +3182,10 @@ public void parse(CharSequence numberToParse, String defaultRegion, PhoneNumber public PhoneNumber parseAndKeepRawInput(CharSequence numberToParse, String defaultRegion) throws NumberParseException { PhoneNumber phoneNumber = new PhoneNumber(); - parseAndKeepRawInput(numberToParse, defaultRegion, phoneNumber); + parseWithOptions( + numberToParse, + new ParsingOptions().setKeepRawInput(true).setDefaultRegion(defaultRegion), + phoneNumber); return phoneNumber; } @@ -3188,20 +3197,57 @@ public PhoneNumber parseAndKeepRawInput(CharSequence numberToParse, String defau @Deprecated public void parseAndKeepRawInput(CharSequence numberToParse, String defaultRegion, PhoneNumber phoneNumber) throws NumberParseException { - parseHelper(numberToParse, defaultRegion, true, true, phoneNumber); + parseWithOptions( + numberToParse, + new ParsingOptions().setKeepRawInput(true).setDefaultRegion(defaultRegion), + phoneNumber); } + /** + * Parses a string and returns it as a PhoneNumber object. The method is quite lenient and looks + * for a number in the input text (raw input) and does not check whether the string is definitely + * only a phone number. To do this, it ignores punctuation and white-space, as well as any text + * before the number (e.g. a leading "Tel: ") and trims the non-number bits. It will accept a + * number in any format (E164, national, international etc), assuming it can be interpreted with + * the options supplied. It also attempts to convert any alpha characters into digits if it thinks + * this is a vanity number of the type "1800 MICROSOFT". + * + *

This method will throw a {@link com.google.i18n.phonenumbers.NumberParseException} if the + * number is not considered to be a possible number. Note that validation of whether the number is + * actually a valid number for a particular region is not performed. This can be done separately + * with {@link #isValidNumber}. + * + *

Note this method canonicalizes the phone number such that different representations can be + * easily compared, no matter what form it was originally entered in (e.g. national, + * international). If you want to record context about the number being parsed, such as the raw + * input that was entered, how the country code was derived etc. then set the `keepRawInput` + * option in the options accordingly. + * + * @param numberToParse number that we are attempting to parse. This can contain formatting such + * as +, ( and -, as well as a phone number extension. It can also be provided in RFC3966 + * format. + * @param options options to configure the behavior of the parser. See {@link ParsingOptions} for + * more details. + * @return a phone number proto buffer filled with the parsed number. + * @throws NumberParseException if the string is not considered to be a viable phone number (e.g. + * too few or too many digits) or if no default region was supplied and the number is not in + * international format (does not start with +). + */ public PhoneNumber parseWithOptions(CharSequence numberToParse, ParsingOptions options) throws NumberParseException { PhoneNumber phoneNumber = new PhoneNumber(); - parseHelper(numberToParse, options.getDefaultRegion(), options.hasKeepRawInput(), options.hasDefaultRegion(), phoneNumber); + parseWithOptions(numberToParse, options, phoneNumber); return phoneNumber; } + /** + * Same as{@link #parseWithOptions(CharSequence, ParsingOptions)}, but accepts a mutable + * PhoneNumber as a parameter to decrease object creation when invoked many times. + */ public void parseWithOptions(CharSequence numberToParse, ParsingOptions options, PhoneNumber phoneNumber) throws NumberParseException { - parseHelper(numberToParse, options.getDefaultRegion(), options.hasKeepRawInput(), options.hasDefaultRegion(), phoneNumber); - } + parseHelper(numberToParse, options.getDefaultRegion(), options.isKeepRawInput(), true, phoneNumber); + } /** * Returns an iterable over all {@link PhoneNumberMatch PhoneNumberMatches} in {@code text}. This diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java index 6bdef41a7c..87e717928a 100644 --- a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java +++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java @@ -1018,7 +1018,7 @@ public void testFormatInOriginalFormat() throws Exception { PhoneNumber number4 = phoneUtil.parseAndKeepRawInput("442087654321", RegionCode.GB); assertEquals("44 20 8765 4321", phoneUtil.formatInOriginalFormat(number4, RegionCode.GB)); - PhoneNumber number5 = phoneUtil.parse("+442087654321", RegionCode.GB); + PhoneNumber number5 = phoneUtil.parseWithOptions("+442087654321", new ParsingOptions().setDefaultRegion(RegionCode.GB)); assertEquals("(020) 8765 4321", phoneUtil.formatInOriginalFormat(number5, RegionCode.GB)); // Invalid numbers that we have a formatting pattern for should be formatted properly. Note area @@ -2084,92 +2084,92 @@ public void testMaybeExtractCountryCode() { public void testParseNationalNumber() throws Exception { // National prefix attached. - assertEquals(NZ_NUMBER, phoneUtil.parse("033316005", RegionCode.NZ)); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("033316005", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); // Some fields are not filled in by parse, but only by parseAndKeepRawInput. assertFalse(NZ_NUMBER.hasCountryCodeSource()); assertEquals(CountryCodeSource.UNSPECIFIED, NZ_NUMBER.getCountryCodeSource()); - assertEquals(NZ_NUMBER, phoneUtil.parse("33316005", RegionCode.NZ)); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("33316005", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); // National prefix attached and some formatting present. - assertEquals(NZ_NUMBER, phoneUtil.parse("03-331 6005", RegionCode.NZ)); - assertEquals(NZ_NUMBER, phoneUtil.parse("03 331 6005", RegionCode.NZ)); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("03-331 6005", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("03 331 6005", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); // Test parsing RFC3966 format with a phone context. - assertEquals(NZ_NUMBER, phoneUtil.parse("tel:03-331-6005;phone-context=+64", RegionCode.NZ)); - assertEquals(NZ_NUMBER, phoneUtil.parse("tel:331-6005;phone-context=+64-3", RegionCode.NZ)); - assertEquals(NZ_NUMBER, phoneUtil.parse("tel:331-6005;phone-context=+64-3", RegionCode.US)); - assertEquals(NZ_NUMBER, phoneUtil.parse( - "My number is tel:03-331-6005;phone-context=+64", RegionCode.NZ)); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("tel:03-331-6005;phone-context=+64", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("tel:331-6005;phone-context=+64-3", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("tel:331-6005;phone-context=+64-3", new ParsingOptions().setDefaultRegion(RegionCode.US))); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions( + "My number is tel:03-331-6005;phone-context=+64", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); // Test parsing RFC3966 format with optional user-defined parameters. The parameters will appear // after the context if present. - assertEquals(NZ_NUMBER, phoneUtil.parse("tel:03-331-6005;phone-context=+64;a=%A1", - RegionCode.NZ)); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("tel:03-331-6005;phone-context=+64;a=%A1", + new ParsingOptions().setDefaultRegion(RegionCode.NZ))); // Test parsing RFC3966 with an ISDN subaddress. - assertEquals(NZ_NUMBER, phoneUtil.parse("tel:03-331-6005;isub=12345;phone-context=+64", - RegionCode.NZ)); - assertEquals(NZ_NUMBER, phoneUtil.parse("tel:+64-3-331-6005;isub=12345", RegionCode.NZ)); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("tel:03-331-6005;isub=12345;phone-context=+64", + new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("tel:+64-3-331-6005;isub=12345", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); // Test parsing RFC3966 with "tel:" missing. - assertEquals(NZ_NUMBER, phoneUtil.parse("03-331-6005;phone-context=+64", RegionCode.NZ)); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("03-331-6005;phone-context=+64", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); // Testing international prefixes. // Should strip country calling code. - assertEquals(NZ_NUMBER, phoneUtil.parse("0064 3 331 6005", RegionCode.NZ)); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("0064 3 331 6005", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); // Try again, but this time we have an international number with Region Code US. It should // recognise the country calling code and parse accordingly. - assertEquals(NZ_NUMBER, phoneUtil.parse("01164 3 331 6005", RegionCode.US)); - assertEquals(NZ_NUMBER, phoneUtil.parse("+64 3 331 6005", RegionCode.US)); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("01164 3 331 6005", new ParsingOptions().setDefaultRegion(RegionCode.US))); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("+64 3 331 6005", new ParsingOptions().setDefaultRegion(RegionCode.US))); // We should ignore the leading plus here, since it is not followed by a valid country code but // instead is followed by the IDD for the US. - assertEquals(NZ_NUMBER, phoneUtil.parse("+01164 3 331 6005", RegionCode.US)); - assertEquals(NZ_NUMBER, phoneUtil.parse("+0064 3 331 6005", RegionCode.NZ)); - assertEquals(NZ_NUMBER, phoneUtil.parse("+ 00 64 3 331 6005", RegionCode.NZ)); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("+01164 3 331 6005", new ParsingOptions().setDefaultRegion(RegionCode.US))); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("+0064 3 331 6005", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("+ 00 64 3 331 6005", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); assertEquals(US_LOCAL_NUMBER, - phoneUtil.parse("tel:253-0000;phone-context=www.google.com", RegionCode.US)); + phoneUtil.parseWithOptions("tel:253-0000;phone-context=www.google.com", new ParsingOptions().setDefaultRegion(RegionCode.US))); assertEquals(US_LOCAL_NUMBER, - phoneUtil.parse("tel:253-0000;isub=12345;phone-context=www.google.com", RegionCode.US)); + phoneUtil.parseWithOptions("tel:253-0000;isub=12345;phone-context=www.google.com", new ParsingOptions().setDefaultRegion(RegionCode.US))); assertEquals(US_LOCAL_NUMBER, - phoneUtil.parse("tel:2530000;isub=12345;phone-context=1234.com", RegionCode.US)); + phoneUtil.parseWithOptions("tel:2530000;isub=12345;phone-context=1234.com", new ParsingOptions().setDefaultRegion(RegionCode.US))); PhoneNumber nzNumber = new PhoneNumber(); nzNumber.setCountryCode(64).setNationalNumber(64123456L); - assertEquals(nzNumber, phoneUtil.parse("64(0)64123456", RegionCode.NZ)); + assertEquals(nzNumber, phoneUtil.parseWithOptions("64(0)64123456", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); // Check that using a "/" is fine in a phone number. - assertEquals(DE_NUMBER, phoneUtil.parse("301/23456", RegionCode.DE)); + assertEquals(DE_NUMBER, phoneUtil.parseWithOptions("301/23456", new ParsingOptions().setDefaultRegion(RegionCode.DE))); PhoneNumber usNumber = new PhoneNumber(); // Check it doesn't use the '1' as a country calling code when parsing if the phone number was // already possible. usNumber.setCountryCode(1).setNationalNumber(1234567890L); - assertEquals(usNumber, phoneUtil.parse("123-456-7890", RegionCode.US)); + assertEquals(usNumber, phoneUtil.parseWithOptions("123-456-7890", new ParsingOptions().setDefaultRegion(RegionCode.US))); // Test star numbers. Although this is not strictly valid, we would like to make sure we can // parse the output we produce when formatting the number. - assertEquals(JP_STAR_NUMBER, phoneUtil.parse("+81 *2345", RegionCode.JP)); + assertEquals(JP_STAR_NUMBER, phoneUtil.parseWithOptions("+81 *2345", new ParsingOptions().setDefaultRegion(RegionCode.JP))); PhoneNumber shortNumber = new PhoneNumber(); shortNumber.setCountryCode(64).setNationalNumber(12L); - assertEquals(shortNumber, phoneUtil.parse("12", RegionCode.NZ)); + assertEquals(shortNumber, phoneUtil.parseWithOptions("12", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); // Test for short-code with leading zero for a country which has 0 as national prefix. Ensure // it's not interpreted as national prefix if the remaining number length is local-only in // terms of length. Example: In GB, length 6-7 are only possible local-only. shortNumber.setCountryCode(44).setNationalNumber(123456) .setItalianLeadingZero(true); - assertEquals(shortNumber, phoneUtil.parse("0123456", RegionCode.GB)); + assertEquals(shortNumber, phoneUtil.parseWithOptions("0123456", new ParsingOptions().setDefaultRegion(RegionCode.GB))); } public void testParseNumberWithAlphaCharacters() throws Exception { // Test case with alpha characters. PhoneNumber tollfreeNumber = new PhoneNumber(); tollfreeNumber.setCountryCode(64).setNationalNumber(800332005L); - assertEquals(tollfreeNumber, phoneUtil.parse("0800 DDA 005", RegionCode.NZ)); + assertEquals(tollfreeNumber, phoneUtil.parseWithOptions("0800 DDA 005", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); PhoneNumber premiumNumber = new PhoneNumber(); premiumNumber.setCountryCode(64).setNationalNumber(9003326005L); - assertEquals(premiumNumber, phoneUtil.parse("0900 DDA 6005", RegionCode.NZ)); + assertEquals(premiumNumber, phoneUtil.parseWithOptions("0900 DDA 6005", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); // Not enough alpha characters for them to be considered intentional, so they are stripped. - assertEquals(premiumNumber, phoneUtil.parse("0900 332 6005a", RegionCode.NZ)); - assertEquals(premiumNumber, phoneUtil.parse("0900 332 600a5", RegionCode.NZ)); - assertEquals(premiumNumber, phoneUtil.parse("0900 332 600A5", RegionCode.NZ)); - assertEquals(premiumNumber, phoneUtil.parse("0900 a332 600A5", RegionCode.NZ)); + assertEquals(premiumNumber, phoneUtil.parseWithOptions("0900 332 6005a", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(premiumNumber, phoneUtil.parseWithOptions("0900 332 600a5", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(premiumNumber, phoneUtil.parseWithOptions("0900 332 600A5", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(premiumNumber, phoneUtil.parseWithOptions("0900 a332 600A5", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); } public void testParseMaliciousInput() throws Exception { @@ -2180,7 +2180,7 @@ public void testParseMaliciousInput() throws Exception { } maliciousNumber.append("12222-33-244 extensioB 343+"); try { - phoneUtil.parse(maliciousNumber.toString(), RegionCode.US); + phoneUtil.parseWithOptions(maliciousNumber.toString(), new ParsingOptions().setDefaultRegion(RegionCode.US)); fail("This should not parse without throwing an exception " + maliciousNumber); } catch (NumberParseException e) { // Expected this exception. @@ -2194,7 +2194,7 @@ public void testParseMaliciousInput() throws Exception { } maliciousNumberWithAlmostExt.append(" extensiOB 345"); try { - phoneUtil.parse(maliciousNumberWithAlmostExt.toString(), RegionCode.US); + phoneUtil.parseWithOptions(maliciousNumberWithAlmostExt.toString(), new ParsingOptions().setDefaultRegion(RegionCode.US)); fail("This should not parse without throwing an exception " + maliciousNumberWithAlmostExt); } catch (NumberParseException e) { // Expected this exception. @@ -2205,112 +2205,112 @@ public void testParseMaliciousInput() throws Exception { } public void testParseWithInternationalPrefixes() throws Exception { - assertEquals(US_NUMBER, phoneUtil.parse("+1 (650) 253-0000", RegionCode.NZ)); - assertEquals(INTERNATIONAL_TOLL_FREE, phoneUtil.parse("011 800 1234 5678", RegionCode.US)); - assertEquals(US_NUMBER, phoneUtil.parse("1-650-253-0000", RegionCode.US)); + assertEquals(US_NUMBER, phoneUtil.parseWithOptions("+1 (650) 253-0000", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(INTERNATIONAL_TOLL_FREE, phoneUtil.parseWithOptions("011 800 1234 5678", new ParsingOptions().setDefaultRegion(RegionCode.US))); + assertEquals(US_NUMBER, phoneUtil.parseWithOptions("1-650-253-0000", new ParsingOptions().setDefaultRegion(RegionCode.US))); // Calling the US number from Singapore by using different service providers // 1st test: calling using SingTel IDD service (IDD is 001) - assertEquals(US_NUMBER, phoneUtil.parse("0011-650-253-0000", RegionCode.SG)); + assertEquals(US_NUMBER, phoneUtil.parseWithOptions("0011-650-253-0000", new ParsingOptions().setDefaultRegion(RegionCode.SG))); // 2nd test: calling using StarHub IDD service (IDD is 008) - assertEquals(US_NUMBER, phoneUtil.parse("0081-650-253-0000", RegionCode.SG)); + assertEquals(US_NUMBER, phoneUtil.parseWithOptions("0081-650-253-0000", new ParsingOptions().setDefaultRegion(RegionCode.SG))); // 3rd test: calling using SingTel V019 service (IDD is 019) - assertEquals(US_NUMBER, phoneUtil.parse("0191-650-253-0000", RegionCode.SG)); + assertEquals(US_NUMBER, phoneUtil.parseWithOptions("0191-650-253-0000", new ParsingOptions().setDefaultRegion(RegionCode.SG))); // Calling the US number from Poland - assertEquals(US_NUMBER, phoneUtil.parse("0~01-650-253-0000", RegionCode.PL)); + assertEquals(US_NUMBER, phoneUtil.parseWithOptions("0~01-650-253-0000", new ParsingOptions().setDefaultRegion(RegionCode.PL))); // Using "++" at the start. - assertEquals(US_NUMBER, phoneUtil.parse("++1 (650) 253-0000", RegionCode.PL)); + assertEquals(US_NUMBER, phoneUtil.parseWithOptions("++1 (650) 253-0000", new ParsingOptions().setDefaultRegion(RegionCode.PL))); } public void testParseNonAscii() throws Exception { // Using a full-width plus sign. - assertEquals(US_NUMBER, phoneUtil.parse("\uFF0B1 (650) 253-0000", RegionCode.SG)); + assertEquals(US_NUMBER, phoneUtil.parseWithOptions("\uFF0B1 (650) 253-0000", new ParsingOptions().setDefaultRegion(RegionCode.SG))); // Using a soft hyphen U+00AD. - assertEquals(US_NUMBER, phoneUtil.parse("1 (650) 253\u00AD-0000", RegionCode.US)); + assertEquals(US_NUMBER, phoneUtil.parseWithOptions("1 (650) 253\u00AD-0000", new ParsingOptions().setDefaultRegion(RegionCode.US))); // The whole number, including punctuation, is here represented in full-width form. - assertEquals(US_NUMBER, phoneUtil.parse("\uFF0B\uFF11\u3000\uFF08\uFF16\uFF15\uFF10\uFF09" + assertEquals(US_NUMBER, phoneUtil.parseWithOptions("\uFF0B\uFF11\u3000\uFF08\uFF16\uFF15\uFF10\uFF09" + "\u3000\uFF12\uFF15\uFF13\uFF0D\uFF10\uFF10\uFF10\uFF10", - RegionCode.SG)); + new ParsingOptions().setDefaultRegion(RegionCode.SG))); // Using U+30FC dash instead. - assertEquals(US_NUMBER, phoneUtil.parse("\uFF0B\uFF11\u3000\uFF08\uFF16\uFF15\uFF10\uFF09" + assertEquals(US_NUMBER, phoneUtil.parseWithOptions("\uFF0B\uFF11\u3000\uFF08\uFF16\uFF15\uFF10\uFF09" + "\u3000\uFF12\uFF15\uFF13\u30FC\uFF10\uFF10\uFF10\uFF10", - RegionCode.SG)); + new ParsingOptions().setDefaultRegion(RegionCode.SG))); // Using a very strange decimal digit range (Mongolian digits). - assertEquals(US_NUMBER, phoneUtil.parse("\u1811 \u1816\u1815\u1810 " + assertEquals(US_NUMBER, phoneUtil.parseWithOptions("\u1811 \u1816\u1815\u1810 " + "\u1812\u1815\u1813 \u1810\u1810\u1810\u1810", - RegionCode.US)); + new ParsingOptions().setDefaultRegion(RegionCode.US))); } public void testParseWithLeadingZero() throws Exception { - assertEquals(IT_NUMBER, phoneUtil.parse("+39 02-36618 300", RegionCode.NZ)); - assertEquals(IT_NUMBER, phoneUtil.parse("02-36618 300", RegionCode.IT)); + assertEquals(IT_NUMBER, phoneUtil.parseWithOptions("+39 02-36618 300", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(IT_NUMBER, phoneUtil.parseWithOptions("02-36618 300", new ParsingOptions().setDefaultRegion(RegionCode.IT))); - assertEquals(IT_MOBILE, phoneUtil.parse("345 678 901", RegionCode.IT)); + assertEquals(IT_MOBILE, phoneUtil.parseWithOptions("345 678 901", new ParsingOptions().setDefaultRegion(RegionCode.IT))); } public void testParseNationalNumberArgentina() throws Exception { // Test parsing mobile numbers of Argentina. PhoneNumber arNumber = new PhoneNumber(); arNumber.setCountryCode(54).setNationalNumber(93435551212L); - assertEquals(arNumber, phoneUtil.parse("+54 9 343 555 1212", RegionCode.AR)); - assertEquals(arNumber, phoneUtil.parse("0343 15 555 1212", RegionCode.AR)); + assertEquals(arNumber, phoneUtil.parseWithOptions("+54 9 343 555 1212", new ParsingOptions().setDefaultRegion(RegionCode.AR))); + assertEquals(arNumber, phoneUtil.parseWithOptions("0343 15 555 1212", new ParsingOptions().setDefaultRegion(RegionCode.AR))); arNumber.clear(); arNumber.setCountryCode(54).setNationalNumber(93715654320L); - assertEquals(arNumber, phoneUtil.parse("+54 9 3715 65 4320", RegionCode.AR)); - assertEquals(arNumber, phoneUtil.parse("03715 15 65 4320", RegionCode.AR)); - assertEquals(AR_MOBILE, phoneUtil.parse("911 876 54321", RegionCode.AR)); + assertEquals(arNumber, phoneUtil.parseWithOptions("+54 9 3715 65 4320", new ParsingOptions().setDefaultRegion(RegionCode.AR))); + assertEquals(arNumber, phoneUtil.parseWithOptions("03715 15 65 4320", new ParsingOptions().setDefaultRegion(RegionCode.AR))); + assertEquals(AR_MOBILE, phoneUtil.parseWithOptions("911 876 54321", new ParsingOptions().setDefaultRegion(RegionCode.AR))); // Test parsing fixed-line numbers of Argentina. - assertEquals(AR_NUMBER, phoneUtil.parse("+54 11 8765 4321", RegionCode.AR)); - assertEquals(AR_NUMBER, phoneUtil.parse("011 8765 4321", RegionCode.AR)); + assertEquals(AR_NUMBER, phoneUtil.parseWithOptions("+54 11 8765 4321", new ParsingOptions().setDefaultRegion(RegionCode.AR))); + assertEquals(AR_NUMBER, phoneUtil.parseWithOptions("011 8765 4321", new ParsingOptions().setDefaultRegion(RegionCode.AR))); arNumber.clear(); arNumber.setCountryCode(54).setNationalNumber(3715654321L); - assertEquals(arNumber, phoneUtil.parse("+54 3715 65 4321", RegionCode.AR)); - assertEquals(arNumber, phoneUtil.parse("03715 65 4321", RegionCode.AR)); + assertEquals(arNumber, phoneUtil.parseWithOptions("+54 3715 65 4321", new ParsingOptions().setDefaultRegion(RegionCode.AR))); + assertEquals(arNumber, phoneUtil.parseWithOptions("03715 65 4321", new ParsingOptions().setDefaultRegion(RegionCode.AR))); arNumber.clear(); arNumber.setCountryCode(54).setNationalNumber(2312340000L); - assertEquals(arNumber, phoneUtil.parse("+54 23 1234 0000", RegionCode.AR)); - assertEquals(arNumber, phoneUtil.parse("023 1234 0000", RegionCode.AR)); + assertEquals(arNumber, phoneUtil.parseWithOptions("+54 23 1234 0000", new ParsingOptions().setDefaultRegion(RegionCode.AR))); + assertEquals(arNumber, phoneUtil.parseWithOptions("023 1234 0000", new ParsingOptions().setDefaultRegion(RegionCode.AR))); } public void testParseWithXInNumber() throws Exception { // Test that having an 'x' in the phone number at the start is ok and that it just gets removed. - assertEquals(AR_NUMBER, phoneUtil.parse("01187654321", RegionCode.AR)); - assertEquals(AR_NUMBER, phoneUtil.parse("(0) 1187654321", RegionCode.AR)); - assertEquals(AR_NUMBER, phoneUtil.parse("0 1187654321", RegionCode.AR)); - assertEquals(AR_NUMBER, phoneUtil.parse("(0xx) 1187654321", RegionCode.AR)); + assertEquals(AR_NUMBER, phoneUtil.parseWithOptions("01187654321", new ParsingOptions().setDefaultRegion(RegionCode.AR))); + assertEquals(AR_NUMBER, phoneUtil.parseWithOptions("(0) 1187654321", new ParsingOptions().setDefaultRegion(RegionCode.AR))); + assertEquals(AR_NUMBER, phoneUtil.parseWithOptions("0 1187654321", new ParsingOptions().setDefaultRegion(RegionCode.AR))); + assertEquals(AR_NUMBER, phoneUtil.parseWithOptions("(0xx) 1187654321", new ParsingOptions().setDefaultRegion(RegionCode.AR))); PhoneNumber arFromUs = new PhoneNumber(); arFromUs.setCountryCode(54).setNationalNumber(81429712L); // This test is intentionally constructed such that the number of digit after xx is larger than // 7, so that the number won't be mistakenly treated as an extension, as we allow extensions up // to 7 digits. This assumption is okay for now as all the countries where a carrier selection // code is written in the form of xx have a national significant number of length larger than 7. - assertEquals(arFromUs, phoneUtil.parse("011xx5481429712", RegionCode.US)); + assertEquals(arFromUs, phoneUtil.parseWithOptions("011xx5481429712", new ParsingOptions().setDefaultRegion(RegionCode.US))); } public void testParseNumbersMexico() throws Exception { // Test parsing fixed-line numbers of Mexico. PhoneNumber mxNumber = new PhoneNumber(); mxNumber.setCountryCode(52).setNationalNumber(4499780001L); - assertEquals(mxNumber, phoneUtil.parse("+52 (449)978-0001", RegionCode.MX)); - assertEquals(mxNumber, phoneUtil.parse("01 (449)978-0001", RegionCode.MX)); - assertEquals(mxNumber, phoneUtil.parse("(449)978-0001", RegionCode.MX)); + assertEquals(mxNumber, phoneUtil.parseWithOptions("+52 (449)978-0001", new ParsingOptions().setDefaultRegion(RegionCode.MX))); + assertEquals(mxNumber, phoneUtil.parseWithOptions("01 (449)978-0001", new ParsingOptions().setDefaultRegion(RegionCode.MX))); + assertEquals(mxNumber, phoneUtil.parseWithOptions("(449)978-0001", new ParsingOptions().setDefaultRegion(RegionCode.MX))); // Test parsing mobile numbers of Mexico. mxNumber.clear(); mxNumber.setCountryCode(52).setNationalNumber(13312345678L); - assertEquals(mxNumber, phoneUtil.parse("+52 1 33 1234-5678", RegionCode.MX)); - assertEquals(mxNumber, phoneUtil.parse("044 (33) 1234-5678", RegionCode.MX)); - assertEquals(mxNumber, phoneUtil.parse("045 33 1234-5678", RegionCode.MX)); + assertEquals(mxNumber, phoneUtil.parseWithOptions("+52 1 33 1234-5678", new ParsingOptions().setDefaultRegion(RegionCode.MX))); + assertEquals(mxNumber, phoneUtil.parseWithOptions("044 (33) 1234-5678", new ParsingOptions().setDefaultRegion(RegionCode.MX))); + assertEquals(mxNumber, phoneUtil.parseWithOptions("045 33 1234-5678", new ParsingOptions().setDefaultRegion(RegionCode.MX))); } public void testFailedParseOnInvalidNumbers() { try { String sentencePhoneNumber = "This is not a phone number"; - phoneUtil.parse(sentencePhoneNumber, RegionCode.NZ); + phoneUtil.parseWithOptions(sentencePhoneNumber, new ParsingOptions().setDefaultRegion(RegionCode.NZ)); fail("This should not parse without throwing an exception " + sentencePhoneNumber); } catch (NumberParseException e) { // Expected this exception. @@ -2320,7 +2320,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String sentencePhoneNumber = "1 Still not a number"; - phoneUtil.parse(sentencePhoneNumber, RegionCode.NZ); + phoneUtil.parseWithOptions(sentencePhoneNumber, new ParsingOptions().setDefaultRegion(RegionCode.NZ)); fail("This should not parse without throwing an exception " + sentencePhoneNumber); } catch (NumberParseException e) { // Expected this exception. @@ -2330,7 +2330,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String sentencePhoneNumber = "1 MICROSOFT"; - phoneUtil.parse(sentencePhoneNumber, RegionCode.NZ); + phoneUtil.parseWithOptions(sentencePhoneNumber, new ParsingOptions().setDefaultRegion(RegionCode.NZ)); fail("This should not parse without throwing an exception " + sentencePhoneNumber); } catch (NumberParseException e) { // Expected this exception. @@ -2340,7 +2340,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String sentencePhoneNumber = "12 MICROSOFT"; - phoneUtil.parse(sentencePhoneNumber, RegionCode.NZ); + phoneUtil.parseWithOptions(sentencePhoneNumber, new ParsingOptions().setDefaultRegion(RegionCode.NZ)); fail("This should not parse without throwing an exception " + sentencePhoneNumber); } catch (NumberParseException e) { // Expected this exception. @@ -2350,7 +2350,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String tooLongPhoneNumber = "01495 72553301873 810104"; - phoneUtil.parse(tooLongPhoneNumber, RegionCode.GB); + phoneUtil.parseWithOptions(tooLongPhoneNumber, new ParsingOptions().setDefaultRegion(RegionCode.GB)); fail("This should not parse without throwing an exception " + tooLongPhoneNumber); } catch (NumberParseException e) { // Expected this exception. @@ -2360,7 +2360,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String plusMinusPhoneNumber = "+---"; - phoneUtil.parse(plusMinusPhoneNumber, RegionCode.DE); + phoneUtil.parseWithOptions(plusMinusPhoneNumber, new ParsingOptions().setDefaultRegion(RegionCode.DE)); fail("This should not parse without throwing an exception " + plusMinusPhoneNumber); } catch (NumberParseException e) { // Expected this exception. @@ -2370,7 +2370,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String plusStar = "+***"; - phoneUtil.parse(plusStar, RegionCode.DE); + phoneUtil.parseWithOptions(plusStar, new ParsingOptions().setDefaultRegion(RegionCode.DE)); fail("This should not parse without throwing an exception " + plusStar); } catch (NumberParseException e) { // Expected this exception. @@ -2380,7 +2380,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String plusStarPhoneNumber = "+*******91"; - phoneUtil.parse(plusStarPhoneNumber, RegionCode.DE); + phoneUtil.parseWithOptions(plusStarPhoneNumber, new ParsingOptions().setDefaultRegion(RegionCode.DE)); fail("This should not parse without throwing an exception " + plusStarPhoneNumber); } catch (NumberParseException e) { // Expected this exception. @@ -2390,7 +2390,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String tooShortPhoneNumber = "+49 0"; - phoneUtil.parse(tooShortPhoneNumber, RegionCode.DE); + phoneUtil.parseWithOptions(tooShortPhoneNumber, new ParsingOptions().setDefaultRegion(RegionCode.DE)); fail("This should not parse without throwing an exception " + tooShortPhoneNumber); } catch (NumberParseException e) { // Expected this exception. @@ -2400,7 +2400,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String invalidCountryCode = "+210 3456 56789"; - phoneUtil.parse(invalidCountryCode, RegionCode.NZ); + phoneUtil.parseWithOptions(invalidCountryCode, new ParsingOptions().setDefaultRegion(RegionCode.NZ)); fail("This is not a recognised region code: should fail: " + invalidCountryCode); } catch (NumberParseException e) { // Expected this exception. @@ -2410,7 +2410,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String plusAndIddAndInvalidCountryCode = "+ 00 210 3 331 6005"; - phoneUtil.parse(plusAndIddAndInvalidCountryCode, RegionCode.NZ); + phoneUtil.parseWithOptions(plusAndIddAndInvalidCountryCode, new ParsingOptions().setDefaultRegion(RegionCode.NZ)); fail("This should not parse without throwing an exception."); } catch (NumberParseException e) { // Expected this exception. 00 is a correct IDD, but 210 is not a valid country code. @@ -2420,7 +2420,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String someNumber = "123 456 7890"; - phoneUtil.parse(someNumber, RegionCode.ZZ); + phoneUtil.parseWithOptions(someNumber, new ParsingOptions().setDefaultRegion(RegionCode.ZZ)); fail("'Unknown' region code not allowed: should fail."); } catch (NumberParseException e) { // Expected this exception. @@ -2430,7 +2430,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String someNumber = "123 456 7890"; - phoneUtil.parse(someNumber, "CS"); + phoneUtil.parseWithOptions(someNumber, new ParsingOptions().setDefaultRegion("CS")); fail("Deprecated region code not allowed: should fail."); } catch (NumberParseException e) { // Expected this exception. @@ -2440,7 +2440,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String someNumber = "123 456 7890"; - phoneUtil.parse(someNumber, null); + phoneUtil.parseWithOptions(someNumber, new ParsingOptions().setDefaultRegion(null)); fail("Null region code not allowed: should fail."); } catch (NumberParseException e) { // Expected this exception. @@ -2450,7 +2450,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String someNumber = "0044------"; - phoneUtil.parse(someNumber, RegionCode.GB); + phoneUtil.parseWithOptions(someNumber, new ParsingOptions().setDefaultRegion(RegionCode.GB)); fail("No number provided, only region code: should fail"); } catch (NumberParseException e) { // Expected this exception. @@ -2460,7 +2460,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String someNumber = "0044"; - phoneUtil.parse(someNumber, RegionCode.GB); + phoneUtil.parseWithOptions(someNumber, new ParsingOptions().setDefaultRegion(RegionCode.GB)); fail("No number provided, only region code: should fail"); } catch (NumberParseException e) { // Expected this exception. @@ -2470,7 +2470,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String someNumber = "011"; - phoneUtil.parse(someNumber, RegionCode.US); + phoneUtil.parseWithOptions(someNumber, new ParsingOptions().setDefaultRegion(RegionCode.US)); fail("Only IDD provided - should fail."); } catch (NumberParseException e) { // Expected this exception. @@ -2480,7 +2480,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String someNumber = "0119"; - phoneUtil.parse(someNumber, RegionCode.US); + phoneUtil.parseWithOptions(someNumber, new ParsingOptions().setDefaultRegion(RegionCode.US)); fail("Only IDD provided and then 9 - should fail."); } catch (NumberParseException e) { // Expected this exception. @@ -2491,7 +2491,7 @@ public void testFailedParseOnInvalidNumbers() { try { String emptyNumber = ""; // Invalid region. - phoneUtil.parse(emptyNumber, RegionCode.ZZ); + phoneUtil.parseWithOptions(emptyNumber, new ParsingOptions().setDefaultRegion(RegionCode.ZZ)); fail("Empty string - should fail."); } catch (NumberParseException e) { // Expected this exception. @@ -2502,7 +2502,7 @@ public void testFailedParseOnInvalidNumbers() { try { String nullNumber = null; // Invalid region. - phoneUtil.parse(nullNumber, RegionCode.ZZ); + phoneUtil.parseWithOptions(nullNumber, new ParsingOptions().setDefaultRegion(RegionCode.ZZ)); fail("Null string - should fail."); } catch (NumberParseException e) { // Expected this exception. @@ -2514,7 +2514,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String nullNumber = null; - phoneUtil.parse(nullNumber, RegionCode.US); + phoneUtil.parseWithOptions(nullNumber, new ParsingOptions().setDefaultRegion(RegionCode.US)); fail("Null string - should fail."); } catch (NumberParseException e) { // Expected this exception. @@ -2526,7 +2526,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String domainRfcPhoneContext = "tel:555-1234;phone-context=www.google.com"; - phoneUtil.parse(domainRfcPhoneContext, RegionCode.ZZ); + phoneUtil.parseWithOptions(domainRfcPhoneContext, new ParsingOptions().setDefaultRegion(RegionCode.ZZ)); fail("'Unknown' region code not allowed: should fail."); } catch (NumberParseException e) { // Expected this exception. @@ -2538,7 +2538,7 @@ public void testFailedParseOnInvalidNumbers() { // This is invalid because no "+" sign is present as part of phone-context. This should not // succeed in being parsed. String invalidRfcPhoneContext = "tel:555-1234;phone-context=1-331"; - phoneUtil.parse(invalidRfcPhoneContext, RegionCode.ZZ); + phoneUtil.parseWithOptions(invalidRfcPhoneContext, new ParsingOptions().setDefaultRegion(RegionCode.ZZ)); fail("phone-context is missing '+' sign: should fail."); } catch (NumberParseException e) { // Expected this exception. @@ -2549,7 +2549,7 @@ public void testFailedParseOnInvalidNumbers() { try { // Only the phone-context symbol is present, but no data. String invalidRfcPhoneContext = ";phone-context="; - phoneUtil.parse(invalidRfcPhoneContext, RegionCode.ZZ); + phoneUtil.parseWithOptions(invalidRfcPhoneContext, new ParsingOptions().setDefaultRegion(RegionCode.ZZ)); fail("phone-context can't be empty: should fail."); } catch (NumberParseException e) { // Expected this exception. @@ -2562,20 +2562,20 @@ public void testFailedParseOnInvalidNumbers() { public void testParseNumbersWithPlusWithNoRegion() throws Exception { // RegionCode.ZZ is allowed only if the number starts with a '+' - then the country calling code // can be calculated. - assertEquals(NZ_NUMBER, phoneUtil.parse("+64 3 331 6005", RegionCode.ZZ)); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("+64 3 331 6005", new ParsingOptions().setDefaultRegion(RegionCode.ZZ))); // Test with full-width plus. - assertEquals(NZ_NUMBER, phoneUtil.parse("\uFF0B64 3 331 6005", RegionCode.ZZ)); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("\uFF0B64 3 331 6005", new ParsingOptions().setDefaultRegion(RegionCode.ZZ))); // Test with normal plus but leading characters that need to be stripped. - assertEquals(NZ_NUMBER, phoneUtil.parse("Tel: +64 3 331 6005", RegionCode.ZZ)); - assertEquals(NZ_NUMBER, phoneUtil.parse("+64 3 331 6005", null)); - assertEquals(INTERNATIONAL_TOLL_FREE, phoneUtil.parse("+800 1234 5678", null)); - assertEquals(UNIVERSAL_PREMIUM_RATE, phoneUtil.parse("+979 123 456 789", null)); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("Tel: +64 3 331 6005", new ParsingOptions().setDefaultRegion(RegionCode.ZZ))); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("+64 3 331 6005", new ParsingOptions().setDefaultRegion(null))); + assertEquals(INTERNATIONAL_TOLL_FREE, phoneUtil.parseWithOptions("+800 1234 5678", new ParsingOptions().setDefaultRegion(null))); + assertEquals(UNIVERSAL_PREMIUM_RATE, phoneUtil.parseWithOptions("+979 123 456 789", new ParsingOptions().setDefaultRegion(null))); // Test parsing RFC3966 format with a phone context. - assertEquals(NZ_NUMBER, phoneUtil.parse("tel:03-331-6005;phone-context=+64", RegionCode.ZZ)); - assertEquals(NZ_NUMBER, phoneUtil.parse(" tel:03-331-6005;phone-context=+64", RegionCode.ZZ)); - assertEquals(NZ_NUMBER, phoneUtil.parse("tel:03-331-6005;isub=12345;phone-context=+64", - RegionCode.ZZ)); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("tel:03-331-6005;phone-context=+64", new ParsingOptions().setDefaultRegion(RegionCode.ZZ))); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions(" tel:03-331-6005;phone-context=+64", new ParsingOptions().setDefaultRegion(RegionCode.ZZ))); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("tel:03-331-6005;isub=12345;phone-context=+64", + new ParsingOptions().setDefaultRegion(RegionCode.ZZ))); PhoneNumber nzNumberWithRawInput = new PhoneNumber().mergeFrom(NZ_NUMBER). setRawInput("+64 3 331 6005"). @@ -2591,110 +2591,110 @@ public void testParseNumberTooShortIfNationalPrefixStripped() throws Exception { // get them stripped if doing so would result in a number too short to be a possible (regular // length) phone number for that region. PhoneNumber byNumber = new PhoneNumber().setCountryCode(375).setNationalNumber(8123L); - assertEquals(byNumber, phoneUtil.parse("8123", RegionCode.BY)); + assertEquals(byNumber, phoneUtil.parseWithOptions("8123", new ParsingOptions().setDefaultRegion(RegionCode.BY))); byNumber.setNationalNumber(81234L); - assertEquals(byNumber, phoneUtil.parse("81234", RegionCode.BY)); + assertEquals(byNumber, phoneUtil.parseWithOptions("81234", new ParsingOptions().setDefaultRegion(RegionCode.BY))); // The prefix doesn't get stripped, since the input is a viable 6-digit number, whereas the // result of stripping is only 5 digits. byNumber.setNationalNumber(812345L); - assertEquals(byNumber, phoneUtil.parse("812345", RegionCode.BY)); + assertEquals(byNumber, phoneUtil.parseWithOptions("812345", new ParsingOptions().setDefaultRegion(RegionCode.BY))); // The prefix gets stripped, since only 6-digit numbers are possible. byNumber.setNationalNumber(123456L); - assertEquals(byNumber, phoneUtil.parse("8123456", RegionCode.BY)); + assertEquals(byNumber, phoneUtil.parseWithOptions("8123456", new ParsingOptions().setDefaultRegion(RegionCode.BY))); } public void testParseExtensions() throws Exception { PhoneNumber nzNumber = new PhoneNumber(); nzNumber.setCountryCode(64).setNationalNumber(33316005L).setExtension("3456"); - assertEquals(nzNumber, phoneUtil.parse("03 331 6005 ext 3456", RegionCode.NZ)); - assertEquals(nzNumber, phoneUtil.parse("03-3316005x3456", RegionCode.NZ)); - assertEquals(nzNumber, phoneUtil.parse("03-3316005 int.3456", RegionCode.NZ)); - assertEquals(nzNumber, phoneUtil.parse("03 3316005 #3456", RegionCode.NZ)); + assertEquals(nzNumber, phoneUtil.parseWithOptions("03 331 6005 ext 3456", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(nzNumber, phoneUtil.parseWithOptions("03-3316005x3456", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(nzNumber, phoneUtil.parseWithOptions("03-3316005 int.3456", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(nzNumber, phoneUtil.parseWithOptions("03 3316005 #3456", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); // Test the following do not extract extensions: - assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parse("1800 six-flags", RegionCode.US)); - assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parse("1800 SIX FLAGS", RegionCode.US)); - assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parse("0~0 1800 7493 5247", RegionCode.PL)); - assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parse("(1800) 7493.5247", RegionCode.US)); + assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parseWithOptions("1800 six-flags", new ParsingOptions().setDefaultRegion(RegionCode.US))); + assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parseWithOptions("1800 SIX FLAGS", new ParsingOptions().setDefaultRegion(RegionCode.US))); + assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parseWithOptions("0~0 1800 7493 5247", new ParsingOptions().setDefaultRegion(RegionCode.PL))); + assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parseWithOptions("(1800) 7493.5247", new ParsingOptions().setDefaultRegion(RegionCode.US))); // Check that the last instance of an extension token is matched. PhoneNumber extnNumber = new PhoneNumber().mergeFrom(ALPHA_NUMERIC_NUMBER).setExtension("1234"); - assertEquals(extnNumber, phoneUtil.parse("0~0 1800 7493 5247 ~1234", RegionCode.PL)); + assertEquals(extnNumber, phoneUtil.parseWithOptions("0~0 1800 7493 5247 ~1234", new ParsingOptions().setDefaultRegion(RegionCode.PL))); // Verifying bug-fix where the last digit of a number was previously omitted if it was a 0 when // extracting the extension. Also verifying a few different cases of extensions. PhoneNumber ukNumber = new PhoneNumber(); ukNumber.setCountryCode(44).setNationalNumber(2034567890L).setExtension("456"); - assertEquals(ukNumber, phoneUtil.parse("+44 2034567890x456", RegionCode.NZ)); - assertEquals(ukNumber, phoneUtil.parse("+44 2034567890x456", RegionCode.GB)); - assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 x456", RegionCode.GB)); - assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 X456", RegionCode.GB)); - assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 X 456", RegionCode.GB)); - assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 X 456", RegionCode.GB)); - assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 x 456 ", RegionCode.GB)); - assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 X 456", RegionCode.GB)); - assertEquals(ukNumber, phoneUtil.parse("+44-2034567890;ext=456", RegionCode.GB)); - assertEquals(ukNumber, phoneUtil.parse("tel:2034567890;ext=456;phone-context=+44", - RegionCode.ZZ)); + assertEquals(ukNumber, phoneUtil.parseWithOptions("+44 2034567890x456", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(ukNumber, phoneUtil.parseWithOptions("+44 2034567890x456", new ParsingOptions().setDefaultRegion(RegionCode.GB))); + assertEquals(ukNumber, phoneUtil.parseWithOptions("+44 2034567890 x456", new ParsingOptions().setDefaultRegion(RegionCode.GB))); + assertEquals(ukNumber, phoneUtil.parseWithOptions("+44 2034567890 X456", new ParsingOptions().setDefaultRegion(RegionCode.GB))); + assertEquals(ukNumber, phoneUtil.parseWithOptions("+44 2034567890 X 456", new ParsingOptions().setDefaultRegion(RegionCode.GB))); + assertEquals(ukNumber, phoneUtil.parseWithOptions("+44 2034567890 X 456", new ParsingOptions().setDefaultRegion(RegionCode.GB))); + assertEquals(ukNumber, phoneUtil.parseWithOptions("+44 2034567890 x 456 ", new ParsingOptions().setDefaultRegion(RegionCode.GB))); + assertEquals(ukNumber, phoneUtil.parseWithOptions("+44 2034567890 X 456", new ParsingOptions().setDefaultRegion(RegionCode.GB))); + assertEquals(ukNumber, phoneUtil.parseWithOptions("+44-2034567890;ext=456", new ParsingOptions().setDefaultRegion(RegionCode.GB))); + assertEquals(ukNumber, phoneUtil.parseWithOptions("tel:2034567890;ext=456;phone-context=+44", + new ParsingOptions().setDefaultRegion(RegionCode.ZZ))); // Full-width extension, "extn" only. - assertEquals(ukNumber, phoneUtil.parse("+442034567890\uFF45\uFF58\uFF54\uFF4E456", - RegionCode.GB)); + assertEquals(ukNumber, phoneUtil.parseWithOptions("+442034567890\uFF45\uFF58\uFF54\uFF4E456", + new ParsingOptions().setDefaultRegion(RegionCode.GB))); // "xtn" only. - assertEquals(ukNumber, phoneUtil.parse("+442034567890\uFF58\uFF54\uFF4E456", - RegionCode.GB)); + assertEquals(ukNumber, phoneUtil.parseWithOptions("+442034567890\uFF58\uFF54\uFF4E456", + new ParsingOptions().setDefaultRegion(RegionCode.GB))); // "xt" only. - assertEquals(ukNumber, phoneUtil.parse("+442034567890\uFF58\uFF54456", - RegionCode.GB)); + assertEquals(ukNumber, phoneUtil.parseWithOptions("+442034567890\uFF58\uFF54456", + new ParsingOptions().setDefaultRegion(RegionCode.GB))); PhoneNumber usWithExtension = new PhoneNumber(); usWithExtension.setCountryCode(1).setNationalNumber(8009013355L).setExtension("7246433"); - assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 x 7246433", RegionCode.US)); - assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 , ext 7246433", RegionCode.US)); - assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 ; 7246433", RegionCode.US)); + assertEquals(usWithExtension, phoneUtil.parseWithOptions("(800) 901-3355 x 7246433", new ParsingOptions().setDefaultRegion(RegionCode.US))); + assertEquals(usWithExtension, phoneUtil.parseWithOptions("(800) 901-3355 , ext 7246433", new ParsingOptions().setDefaultRegion(RegionCode.US))); + assertEquals(usWithExtension, phoneUtil.parseWithOptions("(800) 901-3355 ; 7246433", new ParsingOptions().setDefaultRegion(RegionCode.US))); // To test an extension character without surrounding spaces. - assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355;7246433", RegionCode.US)); + assertEquals(usWithExtension, phoneUtil.parseWithOptions("(800) 901-3355;7246433", new ParsingOptions().setDefaultRegion(RegionCode.US))); assertEquals(usWithExtension, - phoneUtil.parse("(800) 901-3355 ,extension 7246433", RegionCode.US)); + phoneUtil.parseWithOptions("(800) 901-3355 ,extension 7246433", new ParsingOptions().setDefaultRegion(RegionCode.US))); assertEquals(usWithExtension, - phoneUtil.parse("(800) 901-3355 ,extensi\u00F3n 7246433", RegionCode.US)); + phoneUtil.parseWithOptions("(800) 901-3355 ,extensi\u00F3n 7246433", new ParsingOptions().setDefaultRegion(RegionCode.US))); // Repeat with the small letter o with acute accent created by combining characters. assertEquals(usWithExtension, - phoneUtil.parse("(800) 901-3355 ,extensio\u0301n 7246433", RegionCode.US)); - assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 , 7246433", RegionCode.US)); - assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 ext: 7246433", RegionCode.US)); + phoneUtil.parseWithOptions("(800) 901-3355 ,extensio\u0301n 7246433", new ParsingOptions().setDefaultRegion(RegionCode.US))); + assertEquals(usWithExtension, phoneUtil.parseWithOptions("(800) 901-3355 , 7246433", new ParsingOptions().setDefaultRegion(RegionCode.US))); + assertEquals(usWithExtension, phoneUtil.parseWithOptions("(800) 901-3355 ext: 7246433", new ParsingOptions().setDefaultRegion(RegionCode.US))); // Testing Russian extension \u0434\u043E\u0431 with variants found online. PhoneNumber ruWithExtension = new PhoneNumber(); ruWithExtension.setCountryCode(7).setNationalNumber(4232022511L).setExtension("100"); assertEquals(ruWithExtension, - phoneUtil.parse("8 (423) 202-25-11, \u0434\u043E\u0431. 100", RegionCode.RU)); + phoneUtil.parseWithOptions("8 (423) 202-25-11, \u0434\u043E\u0431. 100", new ParsingOptions().setDefaultRegion(RegionCode.RU))); assertEquals(ruWithExtension, - phoneUtil.parse("8 (423) 202-25-11 \u0434\u043E\u0431. 100", RegionCode.RU)); + phoneUtil.parseWithOptions("8 (423) 202-25-11 \u0434\u043E\u0431. 100", new ParsingOptions().setDefaultRegion(RegionCode.RU))); assertEquals(ruWithExtension, - phoneUtil.parse("8 (423) 202-25-11, \u0434\u043E\u0431 100", RegionCode.RU)); + phoneUtil.parseWithOptions("8 (423) 202-25-11, \u0434\u043E\u0431 100", new ParsingOptions().setDefaultRegion(RegionCode.RU))); assertEquals(ruWithExtension, - phoneUtil.parse("8 (423) 202-25-11 \u0434\u043E\u0431 100", RegionCode.RU)); + phoneUtil.parseWithOptions("8 (423) 202-25-11 \u0434\u043E\u0431 100", new ParsingOptions().setDefaultRegion(RegionCode.RU))); assertEquals(ruWithExtension, - phoneUtil.parse("8 (423) 202-25-11\u0434\u043E\u0431100", RegionCode.RU)); + phoneUtil.parseWithOptions("8 (423) 202-25-11\u0434\u043E\u0431100", new ParsingOptions().setDefaultRegion(RegionCode.RU))); // In upper case assertEquals(ruWithExtension, - phoneUtil.parse("8 (423) 202-25-11, \u0414\u041E\u0411. 100", RegionCode.RU)); + phoneUtil.parseWithOptions("8 (423) 202-25-11, \u0414\u041E\u0411. 100", new ParsingOptions().setDefaultRegion(RegionCode.RU))); // Test that if a number has two extensions specified, we ignore the second. PhoneNumber usWithTwoExtensionsNumber = new PhoneNumber(); usWithTwoExtensionsNumber.setCountryCode(1).setNationalNumber(2121231234L).setExtension("508"); - assertEquals(usWithTwoExtensionsNumber, phoneUtil.parse("(212)123-1234 x508/x1234", - RegionCode.US)); - assertEquals(usWithTwoExtensionsNumber, phoneUtil.parse("(212)123-1234 x508/ x1234", - RegionCode.US)); - assertEquals(usWithTwoExtensionsNumber, phoneUtil.parse("(212)123-1234 x508\\x1234", - RegionCode.US)); + assertEquals(usWithTwoExtensionsNumber, phoneUtil.parseWithOptions("(212)123-1234 x508/x1234", + new ParsingOptions().setDefaultRegion(RegionCode.US))); + assertEquals(usWithTwoExtensionsNumber, phoneUtil.parseWithOptions("(212)123-1234 x508/ x1234", + new ParsingOptions().setDefaultRegion(RegionCode.US))); + assertEquals(usWithTwoExtensionsNumber, phoneUtil.parseWithOptions("(212)123-1234 x508\\x1234", + new ParsingOptions().setDefaultRegion(RegionCode.US))); // Test parsing numbers in the form (645) 123-1234-910# works, where the last 3 digits before // the # are an extension. usWithExtension.clear(); usWithExtension.setCountryCode(1).setNationalNumber(6451231234L).setExtension("910"); - assertEquals(usWithExtension, phoneUtil.parse("+1 (645) 123 1234-910#", RegionCode.US)); + assertEquals(usWithExtension, phoneUtil.parseWithOptions("+1 (645) 123 1234-910#", new ParsingOptions().setDefaultRegion(RegionCode.US))); // Retry with the same number in a slightly different format. - assertEquals(usWithExtension, phoneUtil.parse("+1 (645) 123 1234 ext. 910#", RegionCode.US)); + assertEquals(usWithExtension, phoneUtil.parseWithOptions("+1 (645) 123 1234 ext. 910#", new ParsingOptions().setDefaultRegion(RegionCode.US))); } public void testParseHandlesLongExtensionsWithExplicitLabels() throws Exception { @@ -2704,13 +2704,13 @@ public void testParseHandlesLongExtensionsWithExplicitLabels() throws Exception // Firstly, when in RFC format: PhoneNumberUtil.extLimitAfterExplicitLabel nzNumber.setExtension("0"); - assertEquals(nzNumber, phoneUtil.parse("tel:+6433316005;ext=0", RegionCode.NZ)); + assertEquals(nzNumber, phoneUtil.parseWithOptions("tel:+6433316005;ext=0", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); nzNumber.setExtension("01234567890123456789"); assertEquals( - nzNumber, phoneUtil.parse("tel:+6433316005;ext=01234567890123456789", RegionCode.NZ)); + nzNumber, phoneUtil.parseWithOptions("tel:+6433316005;ext=01234567890123456789", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); // Extension too long. try { - phoneUtil.parse("tel:+6433316005;ext=012345678901234567890", RegionCode.NZ); + phoneUtil.parseWithOptions("tel:+6433316005;ext=012345678901234567890", new ParsingOptions().setDefaultRegion(RegionCode.NZ)); fail( "This should not parse as length of extension is higher than allowed: " + "tel:+6433316005;ext=012345678901234567890"); @@ -2724,21 +2724,21 @@ public void testParseHandlesLongExtensionsWithExplicitLabels() throws Exception // Explicit extension label: PhoneNumberUtil.extLimitAfterExplicitLabel nzNumber.setExtension("1"); - assertEquals(nzNumber, phoneUtil.parse("03 3316005ext:1", RegionCode.NZ)); + assertEquals(nzNumber, phoneUtil.parseWithOptions("03 3316005ext:1", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); nzNumber.setExtension("12345678901234567890"); - assertEquals(nzNumber, phoneUtil.parse("03 3316005 xtn:12345678901234567890", RegionCode.NZ)); + assertEquals(nzNumber, phoneUtil.parseWithOptions("03 3316005 xtn:12345678901234567890", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); assertEquals( - nzNumber, phoneUtil.parse("03 3316005 extension\t12345678901234567890", RegionCode.NZ)); + nzNumber, phoneUtil.parseWithOptions("03 3316005 extension\t12345678901234567890", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); assertEquals( - nzNumber, phoneUtil.parse("03 3316005 xtensio:12345678901234567890", RegionCode.NZ)); + nzNumber, phoneUtil.parseWithOptions("03 3316005 xtensio:12345678901234567890", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); assertEquals( - nzNumber, phoneUtil.parse("03 3316005 xtensi\u00F3n, 12345678901234567890#", RegionCode.NZ)); + nzNumber, phoneUtil.parseWithOptions("03 3316005 xtensi\u00F3n, 12345678901234567890#", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); assertEquals( - nzNumber, phoneUtil.parse("03 3316005extension.12345678901234567890", RegionCode.NZ)); - assertEquals(nzNumber, phoneUtil.parse("03 3316005 \u0434\u043E\u0431:12345678901234567890", RegionCode.NZ)); + nzNumber, phoneUtil.parseWithOptions("03 3316005extension.12345678901234567890", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(nzNumber, phoneUtil.parseWithOptions("03 3316005 \u0434\u043E\u0431:12345678901234567890", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); // Extension too long. try { - phoneUtil.parse("03 3316005 extension 123456789012345678901", RegionCode.NZ); + phoneUtil.parseWithOptions("03 3316005 extension 123456789012345678901", new ParsingOptions().setDefaultRegion(RegionCode.NZ)); fail( "This should not parse as length of extension is higher than allowed: " + "03 3316005 extension 123456789012345678901"); @@ -2758,15 +2758,15 @@ public void testParseHandlesLongExtensionsWithAutoDiallingLabels() throws Except usNumberUserInput.setCountryCode(1).setNationalNumber(2679000000L); usNumberUserInput.setExtension("123456789012345"); assertEquals( - usNumberUserInput, phoneUtil.parse("+12679000000,,123456789012345#", RegionCode.US)); + usNumberUserInput, phoneUtil.parseWithOptions("+12679000000,,123456789012345#", new ParsingOptions().setDefaultRegion(RegionCode.US))); assertEquals( - usNumberUserInput, phoneUtil.parse("+12679000000;123456789012345#", RegionCode.US)); + usNumberUserInput, phoneUtil.parseWithOptions("+12679000000;123456789012345#", new ParsingOptions().setDefaultRegion(RegionCode.US))); PhoneNumber ukNumberUserInput = new PhoneNumber(); ukNumberUserInput.setCountryCode(44).setNationalNumber(2034000000L).setExtension("123456789"); - assertEquals(ukNumberUserInput, phoneUtil.parse("+442034000000,,123456789#", RegionCode.GB)); + assertEquals(ukNumberUserInput, phoneUtil.parseWithOptions("+442034000000,,123456789#", new ParsingOptions().setDefaultRegion(RegionCode.GB))); // Extension too long. try { - phoneUtil.parse("+12679000000,,1234567890123456#", RegionCode.US); + phoneUtil.parseWithOptions("+12679000000,,1234567890123456#", new ParsingOptions().setDefaultRegion(RegionCode.US)); fail( "This should not parse as length of extension is higher than allowed: " + "+12679000000,,1234567890123456#"); @@ -2786,13 +2786,13 @@ public void testParseHandlesShortExtensionsWithAmbiguousChar() throws Exception // Thirdly, for single and non-standard cases: // PhoneNumberUtil.extLimitAfterAmbiguousChar nzNumber.setExtension("123456789"); - assertEquals(nzNumber, phoneUtil.parse("03 3316005 x 123456789", RegionCode.NZ)); - assertEquals(nzNumber, phoneUtil.parse("03 3316005 x. 123456789", RegionCode.NZ)); - assertEquals(nzNumber, phoneUtil.parse("03 3316005 #123456789#", RegionCode.NZ)); - assertEquals(nzNumber, phoneUtil.parse("03 3316005 ~ 123456789", RegionCode.NZ)); + assertEquals(nzNumber, phoneUtil.parseWithOptions("03 3316005 x 123456789", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(nzNumber, phoneUtil.parseWithOptions("03 3316005 x. 123456789", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(nzNumber, phoneUtil.parseWithOptions("03 3316005 #123456789#", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(nzNumber, phoneUtil.parseWithOptions("03 3316005 ~ 123456789", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); // Extension too long. try { - phoneUtil.parse("03 3316005 ~ 1234567890", RegionCode.NZ); + phoneUtil.parseWithOptions("03 3316005 ~ 1234567890", new ParsingOptions().setDefaultRegion(RegionCode.NZ)); fail( "This should not parse as length of extension is higher than allowed: " + "03 3316005 ~ 1234567890"); @@ -2810,12 +2810,12 @@ public void testParseHandlesShortExtensionsWhenNotSureOfLabel() throws Exception // PhoneNumberUtil.extLimitWhenNotSure PhoneNumber usNumber = new PhoneNumber(); usNumber.setCountryCode(1).setNationalNumber(1234567890L).setExtension("666666"); - assertEquals(usNumber, phoneUtil.parse("+1123-456-7890 666666#", RegionCode.US)); + assertEquals(usNumber, phoneUtil.parseWithOptions("+1123-456-7890 666666#", new ParsingOptions().setDefaultRegion(RegionCode.US))); usNumber.setExtension("6"); - assertEquals(usNumber, phoneUtil.parse("+11234567890-6#", RegionCode.US)); + assertEquals(usNumber, phoneUtil.parseWithOptions("+11234567890-6#", new ParsingOptions().setDefaultRegion(RegionCode.US))); // Extension too long. try { - phoneUtil.parse("+1123-456-7890 7777777#", RegionCode.US); + phoneUtil.parseWithOptions("+1123-456-7890 7777777#", new ParsingOptions().setDefaultRegion(RegionCode.US)); fail( "This should not parse as length of extension is higher than allowed: " + "+1123-456-7890 7777777#"); @@ -2874,25 +2874,25 @@ public void testParseItalianLeadingZeros() throws Exception { // Test the number "011". PhoneNumber oneZero = new PhoneNumber(); oneZero.setCountryCode(61).setNationalNumber(11L).setItalianLeadingZero(true); - assertEquals(oneZero, phoneUtil.parse("011", RegionCode.AU)); + assertEquals(oneZero, phoneUtil.parseWithOptions("011", new ParsingOptions().setDefaultRegion(RegionCode.AU))); // Test the number "001". PhoneNumber twoZeros = new PhoneNumber(); twoZeros.setCountryCode(61).setNationalNumber(1).setItalianLeadingZero(true) .setNumberOfLeadingZeros(2); - assertEquals(twoZeros, phoneUtil.parse("001", RegionCode.AU)); + assertEquals(twoZeros, phoneUtil.parseWithOptions("001", new ParsingOptions().setDefaultRegion(RegionCode.AU))); // Test the number "000". This number has 2 leading zeros. PhoneNumber stillTwoZeros = new PhoneNumber(); stillTwoZeros.setCountryCode(61).setNationalNumber(0L).setItalianLeadingZero(true) .setNumberOfLeadingZeros(2); - assertEquals(stillTwoZeros, phoneUtil.parse("000", RegionCode.AU)); + assertEquals(stillTwoZeros, phoneUtil.parseWithOptions("000", new ParsingOptions().setDefaultRegion(RegionCode.AU))); // Test the number "0000". This number has 3 leading zeros. PhoneNumber threeZeros = new PhoneNumber(); threeZeros.setCountryCode(61).setNationalNumber(0L).setItalianLeadingZero(true) .setNumberOfLeadingZeros(3); - assertEquals(threeZeros, phoneUtil.parse("0000", RegionCode.AU)); + assertEquals(threeZeros, phoneUtil.parseWithOptions("0000", new ParsingOptions().setDefaultRegion(RegionCode.AU))); } public void testParseWithPhoneContext() throws Exception { @@ -2900,37 +2900,37 @@ public void testParseWithPhoneContext() throws Exception { // descriptor = domainname / global-number-digits // Valid global-phone-digits - assertEquals(NZ_NUMBER, phoneUtil.parse("tel:033316005;phone-context=+64", RegionCode.ZZ)); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("tel:033316005;phone-context=+64", new ParsingOptions().setDefaultRegion(RegionCode.ZZ))); assertEquals( NZ_NUMBER, - phoneUtil.parse( + phoneUtil.parseWithOptions( "tel:033316005;phone-context=+64;{this isn't part of phone-context anymore!}", - RegionCode.ZZ)); + new ParsingOptions().setDefaultRegion(RegionCode.ZZ))); PhoneNumber nzFromPhoneContext = new PhoneNumber(); nzFromPhoneContext.setCountryCode(64).setNationalNumber(3033316005L); assertEquals( nzFromPhoneContext, - phoneUtil.parse("tel:033316005;phone-context=+64-3", RegionCode.ZZ)); + phoneUtil.parseWithOptions("tel:033316005;phone-context=+64-3", new ParsingOptions().setDefaultRegion(RegionCode.ZZ))); PhoneNumber brFromPhoneContext = new PhoneNumber(); brFromPhoneContext.setCountryCode(55).setNationalNumber(5033316005L); assertEquals( brFromPhoneContext, - phoneUtil.parse("tel:033316005;phone-context=+(555)", RegionCode.ZZ)); + phoneUtil.parseWithOptions("tel:033316005;phone-context=+(555)", new ParsingOptions().setDefaultRegion(RegionCode.ZZ))); PhoneNumber usFromPhoneContext = new PhoneNumber(); usFromPhoneContext.setCountryCode(1).setNationalNumber(23033316005L); assertEquals( usFromPhoneContext, - phoneUtil.parse("tel:033316005;phone-context=+-1-2.3()", RegionCode.ZZ)); + phoneUtil.parseWithOptions("tel:033316005;phone-context=+-1-2.3()", new ParsingOptions().setDefaultRegion(RegionCode.ZZ))); // Valid domainname - assertEquals(NZ_NUMBER, phoneUtil.parse("tel:033316005;phone-context=abc.nz", RegionCode.NZ)); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("tel:033316005;phone-context=abc.nz", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); assertEquals( NZ_NUMBER, - phoneUtil.parse("tel:033316005;phone-context=www.PHONE-numb3r.com", RegionCode.NZ)); - assertEquals(NZ_NUMBER, phoneUtil.parse("tel:033316005;phone-context=a", RegionCode.NZ)); + phoneUtil.parseWithOptions("tel:033316005;phone-context=www.PHONE-numb3r.com", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("tel:033316005;phone-context=a", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); assertEquals( - NZ_NUMBER, phoneUtil.parse("tel:033316005;phone-context=3phone.J.", RegionCode.NZ)); - assertEquals(NZ_NUMBER, phoneUtil.parse("tel:033316005;phone-context=a--z", RegionCode.NZ)); + NZ_NUMBER, phoneUtil.parseWithOptions("tel:033316005;phone-context=3phone.J.", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("tel:033316005;phone-context=a--z", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); // Invalid descriptor assertThrowsForInvalidPhoneContext("tel:033316005;phone-context="); @@ -2952,7 +2952,7 @@ private void assertThrowsForInvalidPhoneContext(String numberToParse) { NumberParseException.class, new ThrowingRunnable() { @Override public void run() throws Throwable { - phoneUtil.parse(numberToParseFinal, RegionCode.ZZ); + phoneUtil.parseWithOptions(numberToParseFinal, new ParsingOptions().setDefaultRegion(RegionCode.ZZ)); } }) .getErrorType()); From 809f889145c1907d9ea2c66289f382b4736931f2 Mon Sep 17 00:00:00 2001 From: karoljk Date: Fri, 22 Nov 2024 09:35:52 +0000 Subject: [PATCH 3/7] Add parsingOptions to OpenSource I have added the new parsing methode ParsingOptions to the OpenSource version. --- .../com/google/i18n/phonenumbers/ParsingOptions.java | 5 ++++- .../google/i18n/phonenumbers/PhoneNumberUtil.java | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/ParsingOptions.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/ParsingOptions.java index 1fc1ceb7ec..5e83a46258 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/ParsingOptions.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/ParsingOptions.java @@ -30,7 +30,10 @@ public class ParsingOptions { public boolean hasDefaultRegion() { return hasDefaultRegion; } public String getDefaultRegion() { return defaultRegion_; } public ParsingOptions setDefaultRegion(String value) { - hasDefaultRegion = (value != null); + if (value == null) { + throw new NullPointerException(); + } + hasDefaultRegion = true; defaultRegion_ = value; return this; } diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java index b8100b08ae..6de3061a0f 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java @@ -3249,6 +3249,18 @@ public void parseWithOptions(CharSequence numberToParse, ParsingOptions options, parseHelper(numberToParse, options.getDefaultRegion(), options.isKeepRawInput(), true, phoneNumber); } + public PhoneNumber parseWithOptions(CharSequence numberToParse, ParsingOptions options) + throws NumberParseException { + PhoneNumber phoneNumber = new PhoneNumber(); + parseHelper(numberToParse, options.getDefaultRegion(), options.hasKeepRawInput(), options.hasDefaultRegion(), phoneNumber); + return phoneNumber; + } + + public void parseWithOptions(CharSequence numberToParse, ParsingOptions options, PhoneNumber phoneNumber) + throws NumberParseException { + parseHelper(numberToParse, options.getDefaultRegion(), options.hasKeepRawInput(), options.hasDefaultRegion(), phoneNumber); + } + /** * Returns an iterable over all {@link PhoneNumberMatch PhoneNumberMatches} in {@code text}. This * is a shortcut for {@link #findNumbers(CharSequence, String, Leniency, long) From 45cb5e44c2dd5d12cf3ea665f8538e0e6dfd9fbf Mon Sep 17 00:00:00 2001 From: karoljk Date: Wed, 27 Nov 2024 12:55:46 +0000 Subject: [PATCH 4/7] Create new class ParsingOptions There is a new class called PrasingOptions. With it we can replace old parsing methodes such as keepRawInput or defaultRegion. --- .../i18n/phonenumbers/ParsingOptions.java | 5 +--- .../i18n/phonenumbers/PhoneNumberUtil.java | 24 +++++++++---------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/ParsingOptions.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/ParsingOptions.java index 5e83a46258..1fc1ceb7ec 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/ParsingOptions.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/ParsingOptions.java @@ -30,10 +30,7 @@ public class ParsingOptions { public boolean hasDefaultRegion() { return hasDefaultRegion; } public String getDefaultRegion() { return defaultRegion_; } public ParsingOptions setDefaultRegion(String value) { - if (value == null) { - throw new NullPointerException(); - } - hasDefaultRegion = true; + hasDefaultRegion = (value != null); defaultRegion_ = value; return this; } diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java index 6de3061a0f..c09370c8d5 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java @@ -3142,6 +3142,10 @@ private boolean checkRegionForParsing(CharSequence numberToParse, String default public PhoneNumber parse(CharSequence numberToParse, String defaultRegion) throws NumberParseException { PhoneNumber phoneNumber = new PhoneNumber(); + parseWithOptions( + numberToParse, + new ParsingOptions().setDefaultRegion(defaultRegion), + phoneNumber); parseWithOptions( numberToParse, new ParsingOptions().setDefaultRegion(defaultRegion), @@ -3161,6 +3165,10 @@ public void parse(CharSequence numberToParse, String defaultRegion, PhoneNumber numberToParse, new ParsingOptions().setDefaultRegion(defaultRegion), phoneNumber); + parseWithOptions( + numberToParse, + new ParsingOptions().setDefaultRegion(defaultRegion), + phoneNumber); } /** @@ -3182,6 +3190,10 @@ public void parse(CharSequence numberToParse, String defaultRegion, PhoneNumber public PhoneNumber parseAndKeepRawInput(CharSequence numberToParse, String defaultRegion) throws NumberParseException { PhoneNumber phoneNumber = new PhoneNumber(); + parseWithOptions( + numberToParse, + new ParsingOptions().setKeepRawInput(true).setDefaultRegion(defaultRegion), + phoneNumber); parseWithOptions( numberToParse, new ParsingOptions().setKeepRawInput(true).setDefaultRegion(defaultRegion), @@ -3249,18 +3261,6 @@ public void parseWithOptions(CharSequence numberToParse, ParsingOptions options, parseHelper(numberToParse, options.getDefaultRegion(), options.isKeepRawInput(), true, phoneNumber); } - public PhoneNumber parseWithOptions(CharSequence numberToParse, ParsingOptions options) - throws NumberParseException { - PhoneNumber phoneNumber = new PhoneNumber(); - parseHelper(numberToParse, options.getDefaultRegion(), options.hasKeepRawInput(), options.hasDefaultRegion(), phoneNumber); - return phoneNumber; - } - - public void parseWithOptions(CharSequence numberToParse, ParsingOptions options, PhoneNumber phoneNumber) - throws NumberParseException { - parseHelper(numberToParse, options.getDefaultRegion(), options.hasKeepRawInput(), options.hasDefaultRegion(), phoneNumber); - } - /** * Returns an iterable over all {@link PhoneNumberMatch PhoneNumberMatches} in {@code text}. This * is a shortcut for {@link #findNumbers(CharSequence, String, Leniency, long) From 90270c70dc04961c124ffb8bd989f65b1bef7eb6 Mon Sep 17 00:00:00 2001 From: karoljk Date: Fri, 29 Nov 2024 10:33:55 +0000 Subject: [PATCH 5/7] Write testcases for parseWithOptions We had to switch up the deprecated test cases of parseAndKeepRawInput and replaced them with parseWithOptions. It includes the phonenumber that has to be parsed and the options such as if the raw input should be kept or what the default regioncode is. --- .../i18n/phonenumbers/PhoneNumberUtil.java | 16 - .../phonenumbers/PhoneNumberUtilTest.java | 479 +++++++++--------- pending_code_changes.txt | 5 +- 3 files changed, 243 insertions(+), 257 deletions(-) diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java index c09370c8d5..fd7e7dbf9d 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java @@ -3136,16 +3136,10 @@ private boolean checkRegionForParsing(CharSequence numberToParse, String default * @throws NumberParseException if the string is not considered to be a viable phone number (e.g. * too few or too many digits) or if no default region was supplied and the number is not in * international format (does not start with +) - * @deprecated Use {@link #parseWithOptions(CharSequence, ParsingOptions)} instead. */ - @Deprecated public PhoneNumber parse(CharSequence numberToParse, String defaultRegion) throws NumberParseException { PhoneNumber phoneNumber = new PhoneNumber(); - parseWithOptions( - numberToParse, - new ParsingOptions().setDefaultRegion(defaultRegion), - phoneNumber); parseWithOptions( numberToParse, new ParsingOptions().setDefaultRegion(defaultRegion), @@ -3156,19 +3150,13 @@ public PhoneNumber parse(CharSequence numberToParse, String defaultRegion) /** * Same as {@link #parse(CharSequence, String)}, but accepts mutable PhoneNumber as a * parameter to decrease object creation when invoked many times. - * @deprecated Use {@link #parseWithOptions(CharSequence, ParsingOptions, PhoneNumber)} instead. */ - @Deprecated public void parse(CharSequence numberToParse, String defaultRegion, PhoneNumber phoneNumber) throws NumberParseException { parseWithOptions( numberToParse, new ParsingOptions().setDefaultRegion(defaultRegion), phoneNumber); - parseWithOptions( - numberToParse, - new ParsingOptions().setDefaultRegion(defaultRegion), - phoneNumber); } /** @@ -3190,10 +3178,6 @@ public void parse(CharSequence numberToParse, String defaultRegion, PhoneNumber public PhoneNumber parseAndKeepRawInput(CharSequence numberToParse, String defaultRegion) throws NumberParseException { PhoneNumber phoneNumber = new PhoneNumber(); - parseWithOptions( - numberToParse, - new ParsingOptions().setKeepRawInput(true).setDefaultRegion(defaultRegion), - phoneNumber); parseWithOptions( numberToParse, new ParsingOptions().setKeepRawInput(true).setDefaultRegion(defaultRegion), diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java index 87e717928a..0f6f8a40fc 100644 --- a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java +++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java @@ -1006,97 +1006,97 @@ public void testFormatNumberWithExtension() { } public void testFormatInOriginalFormat() throws Exception { - PhoneNumber number1 = phoneUtil.parseAndKeepRawInput("+442087654321", RegionCode.GB); + PhoneNumber number1 = phoneUtil.parseWithOptions("+442087654321", new ParsingOptions().setDefaultRegion(RegionCode.GB).setKeepRawInput(true)); assertEquals("+44 20 8765 4321", phoneUtil.formatInOriginalFormat(number1, RegionCode.GB)); - PhoneNumber number2 = phoneUtil.parseAndKeepRawInput("02087654321", RegionCode.GB); + PhoneNumber number2 = phoneUtil.parseWithOptions("02087654321", new ParsingOptions().setDefaultRegion(RegionCode.GB).setKeepRawInput(true)); assertEquals("(020) 8765 4321", phoneUtil.formatInOriginalFormat(number2, RegionCode.GB)); - PhoneNumber number3 = phoneUtil.parseAndKeepRawInput("011442087654321", RegionCode.US); + PhoneNumber number3 = phoneUtil.parseWithOptions("011442087654321", new ParsingOptions().setDefaultRegion(RegionCode.US).setKeepRawInput(true)); assertEquals("011 44 20 8765 4321", phoneUtil.formatInOriginalFormat(number3, RegionCode.US)); - PhoneNumber number4 = phoneUtil.parseAndKeepRawInput("442087654321", RegionCode.GB); + PhoneNumber number4 = phoneUtil.parseWithOptions("442087654321", new ParsingOptions().setDefaultRegion(RegionCode.GB).setKeepRawInput(true)); assertEquals("44 20 8765 4321", phoneUtil.formatInOriginalFormat(number4, RegionCode.GB)); - PhoneNumber number5 = phoneUtil.parseWithOptions("+442087654321", new ParsingOptions().setDefaultRegion(RegionCode.GB)); + PhoneNumber number5 = phoneUtil.parse("+442087654321", RegionCode.GB); assertEquals("(020) 8765 4321", phoneUtil.formatInOriginalFormat(number5, RegionCode.GB)); // Invalid numbers that we have a formatting pattern for should be formatted properly. Note area // codes starting with 7 are intentionally excluded in the test metadata for testing purposes. - PhoneNumber number6 = phoneUtil.parseAndKeepRawInput("7345678901", RegionCode.US); + PhoneNumber number6 = phoneUtil.parseWithOptions("7345678901", new ParsingOptions().setDefaultRegion(RegionCode.US).setKeepRawInput(true)); assertEquals("734 567 8901", phoneUtil.formatInOriginalFormat(number6, RegionCode.US)); // US is not a leading zero country, and the presence of the leading zero leads us to format the // number using raw_input. - PhoneNumber number7 = phoneUtil.parseAndKeepRawInput("0734567 8901", RegionCode.US); + PhoneNumber number7 = phoneUtil.parseWithOptions("0734567 8901", new ParsingOptions().setDefaultRegion(RegionCode.US).setKeepRawInput(true)); assertEquals("0734567 8901", phoneUtil.formatInOriginalFormat(number7, RegionCode.US)); // This number is valid, but we don't have a formatting pattern for it. Fall back to the raw // input. - PhoneNumber number8 = phoneUtil.parseAndKeepRawInput("02-4567-8900", RegionCode.KR); + PhoneNumber number8 = phoneUtil.parseWithOptions("02-4567-8900", new ParsingOptions().setDefaultRegion(RegionCode.KR).setKeepRawInput(true)); assertEquals("02-4567-8900", phoneUtil.formatInOriginalFormat(number8, RegionCode.KR)); - PhoneNumber number9 = phoneUtil.parseAndKeepRawInput("01180012345678", RegionCode.US); + PhoneNumber number9 = phoneUtil.parseWithOptions("01180012345678", new ParsingOptions().setDefaultRegion(RegionCode.US).setKeepRawInput(true)); assertEquals("011 800 1234 5678", phoneUtil.formatInOriginalFormat(number9, RegionCode.US)); - PhoneNumber number10 = phoneUtil.parseAndKeepRawInput("+80012345678", RegionCode.KR); + PhoneNumber number10 = phoneUtil.parseWithOptions("+80012345678", new ParsingOptions().setDefaultRegion(RegionCode.KR).setKeepRawInput(true)); assertEquals("+800 1234 5678", phoneUtil.formatInOriginalFormat(number10, RegionCode.KR)); // US local numbers are formatted correctly, as we have formatting patterns for them. - PhoneNumber localNumberUS = phoneUtil.parseAndKeepRawInput("2530000", RegionCode.US); + PhoneNumber localNumberUS = phoneUtil.parseWithOptions("2530000", new ParsingOptions().setDefaultRegion(RegionCode.US).setKeepRawInput(true)); assertEquals("253 0000", phoneUtil.formatInOriginalFormat(localNumberUS, RegionCode.US)); PhoneNumber numberWithNationalPrefixUS = - phoneUtil.parseAndKeepRawInput("18003456789", RegionCode.US); + phoneUtil.parseWithOptions("18003456789", new ParsingOptions().setDefaultRegion(RegionCode.US).setKeepRawInput(true)); assertEquals("1 800 345 6789", phoneUtil.formatInOriginalFormat(numberWithNationalPrefixUS, RegionCode.US)); PhoneNumber numberWithoutNationalPrefixGB = - phoneUtil.parseAndKeepRawInput("2087654321", RegionCode.GB); + phoneUtil.parseWithOptions("2087654321", new ParsingOptions().setDefaultRegion(RegionCode.GB).setKeepRawInput(true)); assertEquals("20 8765 4321", phoneUtil.formatInOriginalFormat(numberWithoutNationalPrefixGB, RegionCode.GB)); // Make sure no metadata is modified as a result of the previous function call. assertEquals("(020) 8765 4321", phoneUtil.formatInOriginalFormat(number5, RegionCode.GB)); PhoneNumber numberWithNationalPrefixMX = - phoneUtil.parseAndKeepRawInput("013312345678", RegionCode.MX); + phoneUtil.parseWithOptions("013312345678", new ParsingOptions().setDefaultRegion(RegionCode.MX).setKeepRawInput(true)); assertEquals("01 33 1234 5678", phoneUtil.formatInOriginalFormat(numberWithNationalPrefixMX, RegionCode.MX)); PhoneNumber numberWithoutNationalPrefixMX = - phoneUtil.parseAndKeepRawInput("3312345678", RegionCode.MX); + phoneUtil.parseWithOptions("3312345678", new ParsingOptions().setDefaultRegion(RegionCode.MX).setKeepRawInput(true)); assertEquals("33 1234 5678", phoneUtil.formatInOriginalFormat(numberWithoutNationalPrefixMX, RegionCode.MX)); PhoneNumber italianFixedLineNumber = - phoneUtil.parseAndKeepRawInput("0212345678", RegionCode.IT); + phoneUtil.parseWithOptions("0212345678", new ParsingOptions().setDefaultRegion(RegionCode.IT).setKeepRawInput(true)); assertEquals("02 1234 5678", phoneUtil.formatInOriginalFormat(italianFixedLineNumber, RegionCode.IT)); PhoneNumber numberWithNationalPrefixJP = - phoneUtil.parseAndKeepRawInput("00777012", RegionCode.JP); + phoneUtil.parseWithOptions("00777012", new ParsingOptions().setDefaultRegion(RegionCode.JP).setKeepRawInput(true)); assertEquals("0077-7012", phoneUtil.formatInOriginalFormat(numberWithNationalPrefixJP, RegionCode.JP)); PhoneNumber numberWithoutNationalPrefixJP = - phoneUtil.parseAndKeepRawInput("0777012", RegionCode.JP); + phoneUtil.parseWithOptions("0777012", new ParsingOptions().setDefaultRegion(RegionCode.JP).setKeepRawInput(true)); assertEquals("0777012", phoneUtil.formatInOriginalFormat(numberWithoutNationalPrefixJP, RegionCode.JP)); PhoneNumber numberWithCarrierCodeBR = - phoneUtil.parseAndKeepRawInput("012 3121286979", RegionCode.BR); + phoneUtil.parseWithOptions("012 3121286979", new ParsingOptions().setDefaultRegion(RegionCode.BR).setKeepRawInput(true)); assertEquals("012 3121286979", phoneUtil.formatInOriginalFormat(numberWithCarrierCodeBR, RegionCode.BR)); // The default national prefix used in this case is 045. When a number with national prefix 044 // is entered, we return the raw input as we don't want to change the number entered. PhoneNumber numberWithNationalPrefixMX1 = - phoneUtil.parseAndKeepRawInput("044(33)1234-5678", RegionCode.MX); + phoneUtil.parseWithOptions("044(33)1234-5678", new ParsingOptions().setDefaultRegion(RegionCode.MX).setKeepRawInput(true)); assertEquals("044(33)1234-5678", phoneUtil.formatInOriginalFormat(numberWithNationalPrefixMX1, RegionCode.MX)); PhoneNumber numberWithNationalPrefixMX2 = - phoneUtil.parseAndKeepRawInput("045(33)1234-5678", RegionCode.MX); + phoneUtil.parseWithOptions("045(33)1234-5678", new ParsingOptions().setDefaultRegion(RegionCode.MX).setKeepRawInput(true)); assertEquals("045 33 1234 5678", phoneUtil.formatInOriginalFormat(numberWithNationalPrefixMX2, RegionCode.MX)); @@ -1104,19 +1104,19 @@ public void testFormatInOriginalFormat() throws Exception { // prefix 0012 is entered, we return the raw input as we don't want to change the number // entered. PhoneNumber outOfCountryNumberFromAU1 = - phoneUtil.parseAndKeepRawInput("0012 16502530000", RegionCode.AU); + phoneUtil.parseWithOptions("0012 16502530000", new ParsingOptions().setDefaultRegion(RegionCode.AU).setKeepRawInput(true)); assertEquals("0012 16502530000", phoneUtil.formatInOriginalFormat(outOfCountryNumberFromAU1, RegionCode.AU)); PhoneNumber outOfCountryNumberFromAU2 = - phoneUtil.parseAndKeepRawInput("0011 16502530000", RegionCode.AU); + phoneUtil.parseWithOptions("0011 16502530000", new ParsingOptions().setDefaultRegion(RegionCode.AU).setKeepRawInput(true)); assertEquals("0011 1 650 253 0000", phoneUtil.formatInOriginalFormat(outOfCountryNumberFromAU2, RegionCode.AU)); // Test the star sign is not removed from or added to the original input by this method. - PhoneNumber starNumber = phoneUtil.parseAndKeepRawInput("*1234", RegionCode.JP); + PhoneNumber starNumber = phoneUtil.parseWithOptions("*1234", new ParsingOptions().setDefaultRegion(RegionCode.JP).setKeepRawInput(true)); assertEquals("*1234", phoneUtil.formatInOriginalFormat(starNumber, RegionCode.JP)); - PhoneNumber numberWithoutStar = phoneUtil.parseAndKeepRawInput("1234", RegionCode.JP); + PhoneNumber numberWithoutStar = phoneUtil.parseWithOptions("1234", new ParsingOptions().setDefaultRegion(RegionCode.JP).setKeepRawInput(true)); assertEquals("1234", phoneUtil.formatInOriginalFormat(numberWithoutStar, RegionCode.JP)); // Test an invalid national number without raw input is just formatted as the national number. @@ -2084,92 +2084,92 @@ public void testMaybeExtractCountryCode() { public void testParseNationalNumber() throws Exception { // National prefix attached. - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("033316005", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); - // Some fields are not filled in by parse, but only by parseAndKeepRawInput. + assertEquals(NZ_NUMBER, phoneUtil.parse("033316005", RegionCode.NZ)); + // Some fields are not filled in by parse, but only by parseWithOptions. assertFalse(NZ_NUMBER.hasCountryCodeSource()); assertEquals(CountryCodeSource.UNSPECIFIED, NZ_NUMBER.getCountryCodeSource()); - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("33316005", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(NZ_NUMBER, phoneUtil.parse("33316005", RegionCode.NZ)); // National prefix attached and some formatting present. - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("03-331 6005", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("03 331 6005", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(NZ_NUMBER, phoneUtil.parse("03-331 6005", RegionCode.NZ)); + assertEquals(NZ_NUMBER, phoneUtil.parse("03 331 6005", RegionCode.NZ)); // Test parsing RFC3966 format with a phone context. - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("tel:03-331-6005;phone-context=+64", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("tel:331-6005;phone-context=+64-3", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("tel:331-6005;phone-context=+64-3", new ParsingOptions().setDefaultRegion(RegionCode.US))); - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions( - "My number is tel:03-331-6005;phone-context=+64", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(NZ_NUMBER, phoneUtil.parse("tel:03-331-6005;phone-context=+64", RegionCode.NZ)); + assertEquals(NZ_NUMBER, phoneUtil.parse("tel:331-6005;phone-context=+64-3", RegionCode.NZ)); + assertEquals(NZ_NUMBER, phoneUtil.parse("tel:331-6005;phone-context=+64-3", RegionCode.US)); + assertEquals(NZ_NUMBER, phoneUtil.parse( + "My number is tel:03-331-6005;phone-context=+64", RegionCode.NZ)); // Test parsing RFC3966 format with optional user-defined parameters. The parameters will appear // after the context if present. - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("tel:03-331-6005;phone-context=+64;a=%A1", - new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(NZ_NUMBER, phoneUtil.parse("tel:03-331-6005;phone-context=+64;a=%A1", + RegionCode.NZ)); // Test parsing RFC3966 with an ISDN subaddress. - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("tel:03-331-6005;isub=12345;phone-context=+64", - new ParsingOptions().setDefaultRegion(RegionCode.NZ))); - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("tel:+64-3-331-6005;isub=12345", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(NZ_NUMBER, phoneUtil.parse("tel:03-331-6005;isub=12345;phone-context=+64", + RegionCode.NZ)); + assertEquals(NZ_NUMBER, phoneUtil.parse("tel:+64-3-331-6005;isub=12345", RegionCode.NZ)); // Test parsing RFC3966 with "tel:" missing. - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("03-331-6005;phone-context=+64", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(NZ_NUMBER, phoneUtil.parse("03-331-6005;phone-context=+64", RegionCode.NZ)); // Testing international prefixes. // Should strip country calling code. - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("0064 3 331 6005", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(NZ_NUMBER, phoneUtil.parse("0064 3 331 6005", RegionCode.NZ)); // Try again, but this time we have an international number with Region Code US. It should // recognise the country calling code and parse accordingly. - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("01164 3 331 6005", new ParsingOptions().setDefaultRegion(RegionCode.US))); - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("+64 3 331 6005", new ParsingOptions().setDefaultRegion(RegionCode.US))); + assertEquals(NZ_NUMBER, phoneUtil.parse("01164 3 331 6005", RegionCode.US)); + assertEquals(NZ_NUMBER, phoneUtil.parse("+64 3 331 6005", RegionCode.US)); // We should ignore the leading plus here, since it is not followed by a valid country code but // instead is followed by the IDD for the US. - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("+01164 3 331 6005", new ParsingOptions().setDefaultRegion(RegionCode.US))); - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("+0064 3 331 6005", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("+ 00 64 3 331 6005", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(NZ_NUMBER, phoneUtil.parse("+01164 3 331 6005", RegionCode.US)); + assertEquals(NZ_NUMBER, phoneUtil.parse("+0064 3 331 6005", RegionCode.NZ)); + assertEquals(NZ_NUMBER, phoneUtil.parse("+ 00 64 3 331 6005", RegionCode.NZ)); assertEquals(US_LOCAL_NUMBER, - phoneUtil.parseWithOptions("tel:253-0000;phone-context=www.google.com", new ParsingOptions().setDefaultRegion(RegionCode.US))); + phoneUtil.parse("tel:253-0000;phone-context=www.google.com", RegionCode.US)); assertEquals(US_LOCAL_NUMBER, - phoneUtil.parseWithOptions("tel:253-0000;isub=12345;phone-context=www.google.com", new ParsingOptions().setDefaultRegion(RegionCode.US))); + phoneUtil.parse("tel:253-0000;isub=12345;phone-context=www.google.com", RegionCode.US)); assertEquals(US_LOCAL_NUMBER, - phoneUtil.parseWithOptions("tel:2530000;isub=12345;phone-context=1234.com", new ParsingOptions().setDefaultRegion(RegionCode.US))); + phoneUtil.parse("tel:2530000;isub=12345;phone-context=1234.com", RegionCode.US)); PhoneNumber nzNumber = new PhoneNumber(); nzNumber.setCountryCode(64).setNationalNumber(64123456L); - assertEquals(nzNumber, phoneUtil.parseWithOptions("64(0)64123456", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(nzNumber, phoneUtil.parse("64(0)64123456", RegionCode.NZ)); // Check that using a "/" is fine in a phone number. - assertEquals(DE_NUMBER, phoneUtil.parseWithOptions("301/23456", new ParsingOptions().setDefaultRegion(RegionCode.DE))); + assertEquals(DE_NUMBER, phoneUtil.parse("301/23456", RegionCode.DE)); PhoneNumber usNumber = new PhoneNumber(); // Check it doesn't use the '1' as a country calling code when parsing if the phone number was // already possible. usNumber.setCountryCode(1).setNationalNumber(1234567890L); - assertEquals(usNumber, phoneUtil.parseWithOptions("123-456-7890", new ParsingOptions().setDefaultRegion(RegionCode.US))); + assertEquals(usNumber, phoneUtil.parse("123-456-7890", RegionCode.US)); // Test star numbers. Although this is not strictly valid, we would like to make sure we can // parse the output we produce when formatting the number. - assertEquals(JP_STAR_NUMBER, phoneUtil.parseWithOptions("+81 *2345", new ParsingOptions().setDefaultRegion(RegionCode.JP))); + assertEquals(JP_STAR_NUMBER, phoneUtil.parse("+81 *2345", RegionCode.JP)); PhoneNumber shortNumber = new PhoneNumber(); shortNumber.setCountryCode(64).setNationalNumber(12L); - assertEquals(shortNumber, phoneUtil.parseWithOptions("12", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(shortNumber, phoneUtil.parse("12", RegionCode.NZ)); // Test for short-code with leading zero for a country which has 0 as national prefix. Ensure // it's not interpreted as national prefix if the remaining number length is local-only in // terms of length. Example: In GB, length 6-7 are only possible local-only. shortNumber.setCountryCode(44).setNationalNumber(123456) .setItalianLeadingZero(true); - assertEquals(shortNumber, phoneUtil.parseWithOptions("0123456", new ParsingOptions().setDefaultRegion(RegionCode.GB))); + assertEquals(shortNumber, phoneUtil.parse("0123456", RegionCode.GB)); } public void testParseNumberWithAlphaCharacters() throws Exception { // Test case with alpha characters. PhoneNumber tollfreeNumber = new PhoneNumber(); tollfreeNumber.setCountryCode(64).setNationalNumber(800332005L); - assertEquals(tollfreeNumber, phoneUtil.parseWithOptions("0800 DDA 005", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(tollfreeNumber, phoneUtil.parse("0800 DDA 005", RegionCode.NZ)); PhoneNumber premiumNumber = new PhoneNumber(); premiumNumber.setCountryCode(64).setNationalNumber(9003326005L); - assertEquals(premiumNumber, phoneUtil.parseWithOptions("0900 DDA 6005", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(premiumNumber, phoneUtil.parse("0900 DDA 6005", RegionCode.NZ)); // Not enough alpha characters for them to be considered intentional, so they are stripped. - assertEquals(premiumNumber, phoneUtil.parseWithOptions("0900 332 6005a", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); - assertEquals(premiumNumber, phoneUtil.parseWithOptions("0900 332 600a5", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); - assertEquals(premiumNumber, phoneUtil.parseWithOptions("0900 332 600A5", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); - assertEquals(premiumNumber, phoneUtil.parseWithOptions("0900 a332 600A5", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(premiumNumber, phoneUtil.parse("0900 332 6005a", RegionCode.NZ)); + assertEquals(premiumNumber, phoneUtil.parse("0900 332 600a5", RegionCode.NZ)); + assertEquals(premiumNumber, phoneUtil.parse("0900 332 600A5", RegionCode.NZ)); + assertEquals(premiumNumber, phoneUtil.parse("0900 a332 600A5", RegionCode.NZ)); } public void testParseMaliciousInput() throws Exception { @@ -2180,7 +2180,7 @@ public void testParseMaliciousInput() throws Exception { } maliciousNumber.append("12222-33-244 extensioB 343+"); try { - phoneUtil.parseWithOptions(maliciousNumber.toString(), new ParsingOptions().setDefaultRegion(RegionCode.US)); + phoneUtil.parse(maliciousNumber.toString(), RegionCode.US); fail("This should not parse without throwing an exception " + maliciousNumber); } catch (NumberParseException e) { // Expected this exception. @@ -2194,7 +2194,7 @@ public void testParseMaliciousInput() throws Exception { } maliciousNumberWithAlmostExt.append(" extensiOB 345"); try { - phoneUtil.parseWithOptions(maliciousNumberWithAlmostExt.toString(), new ParsingOptions().setDefaultRegion(RegionCode.US)); + phoneUtil.parse(maliciousNumberWithAlmostExt.toString(), RegionCode.US); fail("This should not parse without throwing an exception " + maliciousNumberWithAlmostExt); } catch (NumberParseException e) { // Expected this exception. @@ -2205,112 +2205,112 @@ public void testParseMaliciousInput() throws Exception { } public void testParseWithInternationalPrefixes() throws Exception { - assertEquals(US_NUMBER, phoneUtil.parseWithOptions("+1 (650) 253-0000", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); - assertEquals(INTERNATIONAL_TOLL_FREE, phoneUtil.parseWithOptions("011 800 1234 5678", new ParsingOptions().setDefaultRegion(RegionCode.US))); - assertEquals(US_NUMBER, phoneUtil.parseWithOptions("1-650-253-0000", new ParsingOptions().setDefaultRegion(RegionCode.US))); + assertEquals(US_NUMBER, phoneUtil.parse("+1 (650) 253-0000", RegionCode.NZ)); + assertEquals(INTERNATIONAL_TOLL_FREE, phoneUtil.parse("011 800 1234 5678", RegionCode.US)); + assertEquals(US_NUMBER, phoneUtil.parse("1-650-253-0000", RegionCode.US)); // Calling the US number from Singapore by using different service providers // 1st test: calling using SingTel IDD service (IDD is 001) - assertEquals(US_NUMBER, phoneUtil.parseWithOptions("0011-650-253-0000", new ParsingOptions().setDefaultRegion(RegionCode.SG))); + assertEquals(US_NUMBER, phoneUtil.parse("0011-650-253-0000", RegionCode.SG)); // 2nd test: calling using StarHub IDD service (IDD is 008) - assertEquals(US_NUMBER, phoneUtil.parseWithOptions("0081-650-253-0000", new ParsingOptions().setDefaultRegion(RegionCode.SG))); + assertEquals(US_NUMBER, phoneUtil.parse("0081-650-253-0000", RegionCode.SG)); // 3rd test: calling using SingTel V019 service (IDD is 019) - assertEquals(US_NUMBER, phoneUtil.parseWithOptions("0191-650-253-0000", new ParsingOptions().setDefaultRegion(RegionCode.SG))); + assertEquals(US_NUMBER, phoneUtil.parse("0191-650-253-0000", RegionCode.SG)); // Calling the US number from Poland - assertEquals(US_NUMBER, phoneUtil.parseWithOptions("0~01-650-253-0000", new ParsingOptions().setDefaultRegion(RegionCode.PL))); + assertEquals(US_NUMBER, phoneUtil.parse("0~01-650-253-0000", RegionCode.PL)); // Using "++" at the start. - assertEquals(US_NUMBER, phoneUtil.parseWithOptions("++1 (650) 253-0000", new ParsingOptions().setDefaultRegion(RegionCode.PL))); + assertEquals(US_NUMBER, phoneUtil.parse("++1 (650) 253-0000", RegionCode.PL)); } public void testParseNonAscii() throws Exception { // Using a full-width plus sign. - assertEquals(US_NUMBER, phoneUtil.parseWithOptions("\uFF0B1 (650) 253-0000", new ParsingOptions().setDefaultRegion(RegionCode.SG))); + assertEquals(US_NUMBER, phoneUtil.parse("\uFF0B1 (650) 253-0000", RegionCode.SG)); // Using a soft hyphen U+00AD. - assertEquals(US_NUMBER, phoneUtil.parseWithOptions("1 (650) 253\u00AD-0000", new ParsingOptions().setDefaultRegion(RegionCode.US))); + assertEquals(US_NUMBER, phoneUtil.parse("1 (650) 253\u00AD-0000", RegionCode.US)); // The whole number, including punctuation, is here represented in full-width form. - assertEquals(US_NUMBER, phoneUtil.parseWithOptions("\uFF0B\uFF11\u3000\uFF08\uFF16\uFF15\uFF10\uFF09" + assertEquals(US_NUMBER, phoneUtil.parse("\uFF0B\uFF11\u3000\uFF08\uFF16\uFF15\uFF10\uFF09" + "\u3000\uFF12\uFF15\uFF13\uFF0D\uFF10\uFF10\uFF10\uFF10", - new ParsingOptions().setDefaultRegion(RegionCode.SG))); + RegionCode.SG)); // Using U+30FC dash instead. - assertEquals(US_NUMBER, phoneUtil.parseWithOptions("\uFF0B\uFF11\u3000\uFF08\uFF16\uFF15\uFF10\uFF09" + assertEquals(US_NUMBER, phoneUtil.parse("\uFF0B\uFF11\u3000\uFF08\uFF16\uFF15\uFF10\uFF09" + "\u3000\uFF12\uFF15\uFF13\u30FC\uFF10\uFF10\uFF10\uFF10", - new ParsingOptions().setDefaultRegion(RegionCode.SG))); + RegionCode.SG)); // Using a very strange decimal digit range (Mongolian digits). - assertEquals(US_NUMBER, phoneUtil.parseWithOptions("\u1811 \u1816\u1815\u1810 " + assertEquals(US_NUMBER, phoneUtil.parse("\u1811 \u1816\u1815\u1810 " + "\u1812\u1815\u1813 \u1810\u1810\u1810\u1810", - new ParsingOptions().setDefaultRegion(RegionCode.US))); + RegionCode.US)); } public void testParseWithLeadingZero() throws Exception { - assertEquals(IT_NUMBER, phoneUtil.parseWithOptions("+39 02-36618 300", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); - assertEquals(IT_NUMBER, phoneUtil.parseWithOptions("02-36618 300", new ParsingOptions().setDefaultRegion(RegionCode.IT))); + assertEquals(IT_NUMBER, phoneUtil.parse("+39 02-36618 300", RegionCode.NZ)); + assertEquals(IT_NUMBER, phoneUtil.parse("02-36618 300", RegionCode.IT)); - assertEquals(IT_MOBILE, phoneUtil.parseWithOptions("345 678 901", new ParsingOptions().setDefaultRegion(RegionCode.IT))); + assertEquals(IT_MOBILE, phoneUtil.parse("345 678 901", RegionCode.IT)); } public void testParseNationalNumberArgentina() throws Exception { // Test parsing mobile numbers of Argentina. PhoneNumber arNumber = new PhoneNumber(); arNumber.setCountryCode(54).setNationalNumber(93435551212L); - assertEquals(arNumber, phoneUtil.parseWithOptions("+54 9 343 555 1212", new ParsingOptions().setDefaultRegion(RegionCode.AR))); - assertEquals(arNumber, phoneUtil.parseWithOptions("0343 15 555 1212", new ParsingOptions().setDefaultRegion(RegionCode.AR))); + assertEquals(arNumber, phoneUtil.parse("+54 9 343 555 1212", RegionCode.AR)); + assertEquals(arNumber, phoneUtil.parse("0343 15 555 1212", RegionCode.AR)); arNumber.clear(); arNumber.setCountryCode(54).setNationalNumber(93715654320L); - assertEquals(arNumber, phoneUtil.parseWithOptions("+54 9 3715 65 4320", new ParsingOptions().setDefaultRegion(RegionCode.AR))); - assertEquals(arNumber, phoneUtil.parseWithOptions("03715 15 65 4320", new ParsingOptions().setDefaultRegion(RegionCode.AR))); - assertEquals(AR_MOBILE, phoneUtil.parseWithOptions("911 876 54321", new ParsingOptions().setDefaultRegion(RegionCode.AR))); + assertEquals(arNumber, phoneUtil.parse("+54 9 3715 65 4320", RegionCode.AR)); + assertEquals(arNumber, phoneUtil.parse("03715 15 65 4320", RegionCode.AR)); + assertEquals(AR_MOBILE, phoneUtil.parse("911 876 54321", RegionCode.AR)); // Test parsing fixed-line numbers of Argentina. - assertEquals(AR_NUMBER, phoneUtil.parseWithOptions("+54 11 8765 4321", new ParsingOptions().setDefaultRegion(RegionCode.AR))); - assertEquals(AR_NUMBER, phoneUtil.parseWithOptions("011 8765 4321", new ParsingOptions().setDefaultRegion(RegionCode.AR))); + assertEquals(AR_NUMBER, phoneUtil.parse("+54 11 8765 4321", RegionCode.AR)); + assertEquals(AR_NUMBER, phoneUtil.parse("011 8765 4321", RegionCode.AR)); arNumber.clear(); arNumber.setCountryCode(54).setNationalNumber(3715654321L); - assertEquals(arNumber, phoneUtil.parseWithOptions("+54 3715 65 4321", new ParsingOptions().setDefaultRegion(RegionCode.AR))); - assertEquals(arNumber, phoneUtil.parseWithOptions("03715 65 4321", new ParsingOptions().setDefaultRegion(RegionCode.AR))); + assertEquals(arNumber, phoneUtil.parse("+54 3715 65 4321", RegionCode.AR)); + assertEquals(arNumber, phoneUtil.parse("03715 65 4321", RegionCode.AR)); arNumber.clear(); arNumber.setCountryCode(54).setNationalNumber(2312340000L); - assertEquals(arNumber, phoneUtil.parseWithOptions("+54 23 1234 0000", new ParsingOptions().setDefaultRegion(RegionCode.AR))); - assertEquals(arNumber, phoneUtil.parseWithOptions("023 1234 0000", new ParsingOptions().setDefaultRegion(RegionCode.AR))); + assertEquals(arNumber, phoneUtil.parse("+54 23 1234 0000", RegionCode.AR)); + assertEquals(arNumber, phoneUtil.parse("023 1234 0000", RegionCode.AR)); } public void testParseWithXInNumber() throws Exception { // Test that having an 'x' in the phone number at the start is ok and that it just gets removed. - assertEquals(AR_NUMBER, phoneUtil.parseWithOptions("01187654321", new ParsingOptions().setDefaultRegion(RegionCode.AR))); - assertEquals(AR_NUMBER, phoneUtil.parseWithOptions("(0) 1187654321", new ParsingOptions().setDefaultRegion(RegionCode.AR))); - assertEquals(AR_NUMBER, phoneUtil.parseWithOptions("0 1187654321", new ParsingOptions().setDefaultRegion(RegionCode.AR))); - assertEquals(AR_NUMBER, phoneUtil.parseWithOptions("(0xx) 1187654321", new ParsingOptions().setDefaultRegion(RegionCode.AR))); + assertEquals(AR_NUMBER, phoneUtil.parse("01187654321", RegionCode.AR)); + assertEquals(AR_NUMBER, phoneUtil.parse("(0) 1187654321", RegionCode.AR)); + assertEquals(AR_NUMBER, phoneUtil.parse("0 1187654321", RegionCode.AR)); + assertEquals(AR_NUMBER, phoneUtil.parse("(0xx) 1187654321", RegionCode.AR)); PhoneNumber arFromUs = new PhoneNumber(); arFromUs.setCountryCode(54).setNationalNumber(81429712L); // This test is intentionally constructed such that the number of digit after xx is larger than // 7, so that the number won't be mistakenly treated as an extension, as we allow extensions up // to 7 digits. This assumption is okay for now as all the countries where a carrier selection // code is written in the form of xx have a national significant number of length larger than 7. - assertEquals(arFromUs, phoneUtil.parseWithOptions("011xx5481429712", new ParsingOptions().setDefaultRegion(RegionCode.US))); + assertEquals(arFromUs, phoneUtil.parse("011xx5481429712", RegionCode.US)); } public void testParseNumbersMexico() throws Exception { // Test parsing fixed-line numbers of Mexico. PhoneNumber mxNumber = new PhoneNumber(); mxNumber.setCountryCode(52).setNationalNumber(4499780001L); - assertEquals(mxNumber, phoneUtil.parseWithOptions("+52 (449)978-0001", new ParsingOptions().setDefaultRegion(RegionCode.MX))); - assertEquals(mxNumber, phoneUtil.parseWithOptions("01 (449)978-0001", new ParsingOptions().setDefaultRegion(RegionCode.MX))); - assertEquals(mxNumber, phoneUtil.parseWithOptions("(449)978-0001", new ParsingOptions().setDefaultRegion(RegionCode.MX))); + assertEquals(mxNumber, phoneUtil.parse("+52 (449)978-0001", RegionCode.MX)); + assertEquals(mxNumber, phoneUtil.parse("01 (449)978-0001", RegionCode.MX)); + assertEquals(mxNumber, phoneUtil.parse("(449)978-0001", RegionCode.MX)); // Test parsing mobile numbers of Mexico. mxNumber.clear(); mxNumber.setCountryCode(52).setNationalNumber(13312345678L); - assertEquals(mxNumber, phoneUtil.parseWithOptions("+52 1 33 1234-5678", new ParsingOptions().setDefaultRegion(RegionCode.MX))); - assertEquals(mxNumber, phoneUtil.parseWithOptions("044 (33) 1234-5678", new ParsingOptions().setDefaultRegion(RegionCode.MX))); - assertEquals(mxNumber, phoneUtil.parseWithOptions("045 33 1234-5678", new ParsingOptions().setDefaultRegion(RegionCode.MX))); + assertEquals(mxNumber, phoneUtil.parse("+52 1 33 1234-5678", RegionCode.MX)); + assertEquals(mxNumber, phoneUtil.parse("044 (33) 1234-5678", RegionCode.MX)); + assertEquals(mxNumber, phoneUtil.parse("045 33 1234-5678", RegionCode.MX)); } public void testFailedParseOnInvalidNumbers() { try { String sentencePhoneNumber = "This is not a phone number"; - phoneUtil.parseWithOptions(sentencePhoneNumber, new ParsingOptions().setDefaultRegion(RegionCode.NZ)); + phoneUtil.parse(sentencePhoneNumber, RegionCode.NZ); fail("This should not parse without throwing an exception " + sentencePhoneNumber); } catch (NumberParseException e) { // Expected this exception. @@ -2320,7 +2320,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String sentencePhoneNumber = "1 Still not a number"; - phoneUtil.parseWithOptions(sentencePhoneNumber, new ParsingOptions().setDefaultRegion(RegionCode.NZ)); + phoneUtil.parse(sentencePhoneNumber, RegionCode.NZ); fail("This should not parse without throwing an exception " + sentencePhoneNumber); } catch (NumberParseException e) { // Expected this exception. @@ -2330,7 +2330,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String sentencePhoneNumber = "1 MICROSOFT"; - phoneUtil.parseWithOptions(sentencePhoneNumber, new ParsingOptions().setDefaultRegion(RegionCode.NZ)); + phoneUtil.parse(sentencePhoneNumber, RegionCode.NZ); fail("This should not parse without throwing an exception " + sentencePhoneNumber); } catch (NumberParseException e) { // Expected this exception. @@ -2340,7 +2340,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String sentencePhoneNumber = "12 MICROSOFT"; - phoneUtil.parseWithOptions(sentencePhoneNumber, new ParsingOptions().setDefaultRegion(RegionCode.NZ)); + phoneUtil.parse(sentencePhoneNumber, RegionCode.NZ); fail("This should not parse without throwing an exception " + sentencePhoneNumber); } catch (NumberParseException e) { // Expected this exception. @@ -2350,7 +2350,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String tooLongPhoneNumber = "01495 72553301873 810104"; - phoneUtil.parseWithOptions(tooLongPhoneNumber, new ParsingOptions().setDefaultRegion(RegionCode.GB)); + phoneUtil.parse(tooLongPhoneNumber, RegionCode.GB); fail("This should not parse without throwing an exception " + tooLongPhoneNumber); } catch (NumberParseException e) { // Expected this exception. @@ -2360,7 +2360,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String plusMinusPhoneNumber = "+---"; - phoneUtil.parseWithOptions(plusMinusPhoneNumber, new ParsingOptions().setDefaultRegion(RegionCode.DE)); + phoneUtil.parse(plusMinusPhoneNumber, RegionCode.DE); fail("This should not parse without throwing an exception " + plusMinusPhoneNumber); } catch (NumberParseException e) { // Expected this exception. @@ -2370,7 +2370,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String plusStar = "+***"; - phoneUtil.parseWithOptions(plusStar, new ParsingOptions().setDefaultRegion(RegionCode.DE)); + phoneUtil.parse(plusStar, RegionCode.DE); fail("This should not parse without throwing an exception " + plusStar); } catch (NumberParseException e) { // Expected this exception. @@ -2380,7 +2380,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String plusStarPhoneNumber = "+*******91"; - phoneUtil.parseWithOptions(plusStarPhoneNumber, new ParsingOptions().setDefaultRegion(RegionCode.DE)); + phoneUtil.parse(plusStarPhoneNumber, RegionCode.DE); fail("This should not parse without throwing an exception " + plusStarPhoneNumber); } catch (NumberParseException e) { // Expected this exception. @@ -2390,7 +2390,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String tooShortPhoneNumber = "+49 0"; - phoneUtil.parseWithOptions(tooShortPhoneNumber, new ParsingOptions().setDefaultRegion(RegionCode.DE)); + phoneUtil.parse(tooShortPhoneNumber, RegionCode.DE); fail("This should not parse without throwing an exception " + tooShortPhoneNumber); } catch (NumberParseException e) { // Expected this exception. @@ -2400,7 +2400,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String invalidCountryCode = "+210 3456 56789"; - phoneUtil.parseWithOptions(invalidCountryCode, new ParsingOptions().setDefaultRegion(RegionCode.NZ)); + phoneUtil.parse(invalidCountryCode, RegionCode.NZ); fail("This is not a recognised region code: should fail: " + invalidCountryCode); } catch (NumberParseException e) { // Expected this exception. @@ -2410,7 +2410,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String plusAndIddAndInvalidCountryCode = "+ 00 210 3 331 6005"; - phoneUtil.parseWithOptions(plusAndIddAndInvalidCountryCode, new ParsingOptions().setDefaultRegion(RegionCode.NZ)); + phoneUtil.parse(plusAndIddAndInvalidCountryCode, RegionCode.NZ); fail("This should not parse without throwing an exception."); } catch (NumberParseException e) { // Expected this exception. 00 is a correct IDD, but 210 is not a valid country code. @@ -2420,7 +2420,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String someNumber = "123 456 7890"; - phoneUtil.parseWithOptions(someNumber, new ParsingOptions().setDefaultRegion(RegionCode.ZZ)); + phoneUtil.parse(someNumber, RegionCode.ZZ); fail("'Unknown' region code not allowed: should fail."); } catch (NumberParseException e) { // Expected this exception. @@ -2430,7 +2430,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String someNumber = "123 456 7890"; - phoneUtil.parseWithOptions(someNumber, new ParsingOptions().setDefaultRegion("CS")); + phoneUtil.parse(someNumber, "CS"); fail("Deprecated region code not allowed: should fail."); } catch (NumberParseException e) { // Expected this exception. @@ -2440,7 +2440,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String someNumber = "123 456 7890"; - phoneUtil.parseWithOptions(someNumber, new ParsingOptions().setDefaultRegion(null)); + phoneUtil.parse(someNumber, null); fail("Null region code not allowed: should fail."); } catch (NumberParseException e) { // Expected this exception. @@ -2450,7 +2450,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String someNumber = "0044------"; - phoneUtil.parseWithOptions(someNumber, new ParsingOptions().setDefaultRegion(RegionCode.GB)); + phoneUtil.parse(someNumber, RegionCode.GB); fail("No number provided, only region code: should fail"); } catch (NumberParseException e) { // Expected this exception. @@ -2460,7 +2460,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String someNumber = "0044"; - phoneUtil.parseWithOptions(someNumber, new ParsingOptions().setDefaultRegion(RegionCode.GB)); + phoneUtil.parse(someNumber, RegionCode.GB); fail("No number provided, only region code: should fail"); } catch (NumberParseException e) { // Expected this exception. @@ -2470,7 +2470,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String someNumber = "011"; - phoneUtil.parseWithOptions(someNumber, new ParsingOptions().setDefaultRegion(RegionCode.US)); + phoneUtil.parse(someNumber, RegionCode.US); fail("Only IDD provided - should fail."); } catch (NumberParseException e) { // Expected this exception. @@ -2480,7 +2480,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String someNumber = "0119"; - phoneUtil.parseWithOptions(someNumber, new ParsingOptions().setDefaultRegion(RegionCode.US)); + phoneUtil.parse(someNumber, RegionCode.US); fail("Only IDD provided and then 9 - should fail."); } catch (NumberParseException e) { // Expected this exception. @@ -2491,7 +2491,7 @@ public void testFailedParseOnInvalidNumbers() { try { String emptyNumber = ""; // Invalid region. - phoneUtil.parseWithOptions(emptyNumber, new ParsingOptions().setDefaultRegion(RegionCode.ZZ)); + phoneUtil.parse(emptyNumber, RegionCode.ZZ); fail("Empty string - should fail."); } catch (NumberParseException e) { // Expected this exception. @@ -2502,7 +2502,7 @@ public void testFailedParseOnInvalidNumbers() { try { String nullNumber = null; // Invalid region. - phoneUtil.parseWithOptions(nullNumber, new ParsingOptions().setDefaultRegion(RegionCode.ZZ)); + phoneUtil.parse(nullNumber, RegionCode.ZZ); fail("Null string - should fail."); } catch (NumberParseException e) { // Expected this exception. @@ -2514,7 +2514,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String nullNumber = null; - phoneUtil.parseWithOptions(nullNumber, new ParsingOptions().setDefaultRegion(RegionCode.US)); + phoneUtil.parse(nullNumber, RegionCode.US); fail("Null string - should fail."); } catch (NumberParseException e) { // Expected this exception. @@ -2526,7 +2526,7 @@ public void testFailedParseOnInvalidNumbers() { } try { String domainRfcPhoneContext = "tel:555-1234;phone-context=www.google.com"; - phoneUtil.parseWithOptions(domainRfcPhoneContext, new ParsingOptions().setDefaultRegion(RegionCode.ZZ)); + phoneUtil.parse(domainRfcPhoneContext, RegionCode.ZZ); fail("'Unknown' region code not allowed: should fail."); } catch (NumberParseException e) { // Expected this exception. @@ -2538,7 +2538,7 @@ public void testFailedParseOnInvalidNumbers() { // This is invalid because no "+" sign is present as part of phone-context. This should not // succeed in being parsed. String invalidRfcPhoneContext = "tel:555-1234;phone-context=1-331"; - phoneUtil.parseWithOptions(invalidRfcPhoneContext, new ParsingOptions().setDefaultRegion(RegionCode.ZZ)); + phoneUtil.parse(invalidRfcPhoneContext, RegionCode.ZZ); fail("phone-context is missing '+' sign: should fail."); } catch (NumberParseException e) { // Expected this exception. @@ -2549,7 +2549,7 @@ public void testFailedParseOnInvalidNumbers() { try { // Only the phone-context symbol is present, but no data. String invalidRfcPhoneContext = ";phone-context="; - phoneUtil.parseWithOptions(invalidRfcPhoneContext, new ParsingOptions().setDefaultRegion(RegionCode.ZZ)); + phoneUtil.parse(invalidRfcPhoneContext, RegionCode.ZZ); fail("phone-context can't be empty: should fail."); } catch (NumberParseException e) { // Expected this exception. @@ -2562,28 +2562,27 @@ public void testFailedParseOnInvalidNumbers() { public void testParseNumbersWithPlusWithNoRegion() throws Exception { // RegionCode.ZZ is allowed only if the number starts with a '+' - then the country calling code // can be calculated. - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("+64 3 331 6005", new ParsingOptions().setDefaultRegion(RegionCode.ZZ))); + assertEquals(NZ_NUMBER, phoneUtil.parse("+64 3 331 6005", RegionCode.ZZ)); // Test with full-width plus. - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("\uFF0B64 3 331 6005", new ParsingOptions().setDefaultRegion(RegionCode.ZZ))); + assertEquals(NZ_NUMBER, phoneUtil.parse("\uFF0B64 3 331 6005", RegionCode.ZZ)); // Test with normal plus but leading characters that need to be stripped. - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("Tel: +64 3 331 6005", new ParsingOptions().setDefaultRegion(RegionCode.ZZ))); - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("+64 3 331 6005", new ParsingOptions().setDefaultRegion(null))); - assertEquals(INTERNATIONAL_TOLL_FREE, phoneUtil.parseWithOptions("+800 1234 5678", new ParsingOptions().setDefaultRegion(null))); - assertEquals(UNIVERSAL_PREMIUM_RATE, phoneUtil.parseWithOptions("+979 123 456 789", new ParsingOptions().setDefaultRegion(null))); + assertEquals(NZ_NUMBER, phoneUtil.parse("Tel: +64 3 331 6005", RegionCode.ZZ)); + assertEquals(NZ_NUMBER, phoneUtil.parse("+64 3 331 6005", null)); + assertEquals(INTERNATIONAL_TOLL_FREE, phoneUtil.parse("+800 1234 5678", null)); + assertEquals(UNIVERSAL_PREMIUM_RATE, phoneUtil.parse("+979 123 456 789", null)); // Test parsing RFC3966 format with a phone context. - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("tel:03-331-6005;phone-context=+64", new ParsingOptions().setDefaultRegion(RegionCode.ZZ))); - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions(" tel:03-331-6005;phone-context=+64", new ParsingOptions().setDefaultRegion(RegionCode.ZZ))); - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("tel:03-331-6005;isub=12345;phone-context=+64", - new ParsingOptions().setDefaultRegion(RegionCode.ZZ))); + assertEquals(NZ_NUMBER, phoneUtil.parse("tel:03-331-6005;phone-context=+64", RegionCode.ZZ)); + assertEquals(NZ_NUMBER, phoneUtil.parse(" tel:03-331-6005;phone-context=+64", RegionCode.ZZ)); + assertEquals(NZ_NUMBER, phoneUtil.parse("tel:03-331-6005;isub=12345;phone-context=+64", + RegionCode.ZZ)); PhoneNumber nzNumberWithRawInput = new PhoneNumber().mergeFrom(NZ_NUMBER). setRawInput("+64 3 331 6005"). setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN); - assertEquals(nzNumberWithRawInput, phoneUtil.parseAndKeepRawInput("+64 3 331 6005", - RegionCode.ZZ)); + assertEquals(nzNumberWithRawInput, phoneUtil.parseWithOptions("+64 3 331 6005", new ParsingOptions().setDefaultRegion(RegionCode.ZZ).setKeepRawInput(true))); // Null is also allowed for the region code in these cases. - assertEquals(nzNumberWithRawInput, phoneUtil.parseAndKeepRawInput("+64 3 331 6005", null)); + assertEquals(nzNumberWithRawInput, phoneUtil.parseWithOptions("+64 3 331 6005", new ParsingOptions().setDefaultRegion(null).setKeepRawInput(true))); } public void testParseNumberTooShortIfNationalPrefixStripped() throws Exception { @@ -2591,110 +2590,110 @@ public void testParseNumberTooShortIfNationalPrefixStripped() throws Exception { // get them stripped if doing so would result in a number too short to be a possible (regular // length) phone number for that region. PhoneNumber byNumber = new PhoneNumber().setCountryCode(375).setNationalNumber(8123L); - assertEquals(byNumber, phoneUtil.parseWithOptions("8123", new ParsingOptions().setDefaultRegion(RegionCode.BY))); + assertEquals(byNumber, phoneUtil.parse("8123", RegionCode.BY)); byNumber.setNationalNumber(81234L); - assertEquals(byNumber, phoneUtil.parseWithOptions("81234", new ParsingOptions().setDefaultRegion(RegionCode.BY))); + assertEquals(byNumber, phoneUtil.parse("81234", RegionCode.BY)); // The prefix doesn't get stripped, since the input is a viable 6-digit number, whereas the // result of stripping is only 5 digits. byNumber.setNationalNumber(812345L); - assertEquals(byNumber, phoneUtil.parseWithOptions("812345", new ParsingOptions().setDefaultRegion(RegionCode.BY))); + assertEquals(byNumber, phoneUtil.parse("812345", RegionCode.BY)); // The prefix gets stripped, since only 6-digit numbers are possible. byNumber.setNationalNumber(123456L); - assertEquals(byNumber, phoneUtil.parseWithOptions("8123456", new ParsingOptions().setDefaultRegion(RegionCode.BY))); + assertEquals(byNumber, phoneUtil.parse("8123456", RegionCode.BY)); } public void testParseExtensions() throws Exception { PhoneNumber nzNumber = new PhoneNumber(); nzNumber.setCountryCode(64).setNationalNumber(33316005L).setExtension("3456"); - assertEquals(nzNumber, phoneUtil.parseWithOptions("03 331 6005 ext 3456", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); - assertEquals(nzNumber, phoneUtil.parseWithOptions("03-3316005x3456", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); - assertEquals(nzNumber, phoneUtil.parseWithOptions("03-3316005 int.3456", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); - assertEquals(nzNumber, phoneUtil.parseWithOptions("03 3316005 #3456", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(nzNumber, phoneUtil.parse("03 331 6005 ext 3456", RegionCode.NZ)); + assertEquals(nzNumber, phoneUtil.parse("03-3316005x3456", RegionCode.NZ)); + assertEquals(nzNumber, phoneUtil.parse("03-3316005 int.3456", RegionCode.NZ)); + assertEquals(nzNumber, phoneUtil.parse("03 3316005 #3456", RegionCode.NZ)); // Test the following do not extract extensions: - assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parseWithOptions("1800 six-flags", new ParsingOptions().setDefaultRegion(RegionCode.US))); - assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parseWithOptions("1800 SIX FLAGS", new ParsingOptions().setDefaultRegion(RegionCode.US))); - assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parseWithOptions("0~0 1800 7493 5247", new ParsingOptions().setDefaultRegion(RegionCode.PL))); - assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parseWithOptions("(1800) 7493.5247", new ParsingOptions().setDefaultRegion(RegionCode.US))); + assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parse("1800 six-flags", RegionCode.US)); + assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parse("1800 SIX FLAGS", RegionCode.US)); + assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parse("0~0 1800 7493 5247", RegionCode.PL)); + assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parse("(1800) 7493.5247", RegionCode.US)); // Check that the last instance of an extension token is matched. PhoneNumber extnNumber = new PhoneNumber().mergeFrom(ALPHA_NUMERIC_NUMBER).setExtension("1234"); - assertEquals(extnNumber, phoneUtil.parseWithOptions("0~0 1800 7493 5247 ~1234", new ParsingOptions().setDefaultRegion(RegionCode.PL))); + assertEquals(extnNumber, phoneUtil.parse("0~0 1800 7493 5247 ~1234", RegionCode.PL)); // Verifying bug-fix where the last digit of a number was previously omitted if it was a 0 when // extracting the extension. Also verifying a few different cases of extensions. PhoneNumber ukNumber = new PhoneNumber(); ukNumber.setCountryCode(44).setNationalNumber(2034567890L).setExtension("456"); - assertEquals(ukNumber, phoneUtil.parseWithOptions("+44 2034567890x456", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); - assertEquals(ukNumber, phoneUtil.parseWithOptions("+44 2034567890x456", new ParsingOptions().setDefaultRegion(RegionCode.GB))); - assertEquals(ukNumber, phoneUtil.parseWithOptions("+44 2034567890 x456", new ParsingOptions().setDefaultRegion(RegionCode.GB))); - assertEquals(ukNumber, phoneUtil.parseWithOptions("+44 2034567890 X456", new ParsingOptions().setDefaultRegion(RegionCode.GB))); - assertEquals(ukNumber, phoneUtil.parseWithOptions("+44 2034567890 X 456", new ParsingOptions().setDefaultRegion(RegionCode.GB))); - assertEquals(ukNumber, phoneUtil.parseWithOptions("+44 2034567890 X 456", new ParsingOptions().setDefaultRegion(RegionCode.GB))); - assertEquals(ukNumber, phoneUtil.parseWithOptions("+44 2034567890 x 456 ", new ParsingOptions().setDefaultRegion(RegionCode.GB))); - assertEquals(ukNumber, phoneUtil.parseWithOptions("+44 2034567890 X 456", new ParsingOptions().setDefaultRegion(RegionCode.GB))); - assertEquals(ukNumber, phoneUtil.parseWithOptions("+44-2034567890;ext=456", new ParsingOptions().setDefaultRegion(RegionCode.GB))); - assertEquals(ukNumber, phoneUtil.parseWithOptions("tel:2034567890;ext=456;phone-context=+44", - new ParsingOptions().setDefaultRegion(RegionCode.ZZ))); + assertEquals(ukNumber, phoneUtil.parse("+44 2034567890x456", RegionCode.NZ)); + assertEquals(ukNumber, phoneUtil.parse("+44 2034567890x456", RegionCode.GB)); + assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 x456", RegionCode.GB)); + assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 X456", RegionCode.GB)); + assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 X 456", RegionCode.GB)); + assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 X 456", RegionCode.GB)); + assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 x 456 ", RegionCode.GB)); + assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 X 456", RegionCode.GB)); + assertEquals(ukNumber, phoneUtil.parse("+44-2034567890;ext=456", RegionCode.GB)); + assertEquals(ukNumber, phoneUtil.parse("tel:2034567890;ext=456;phone-context=+44", + RegionCode.ZZ)); // Full-width extension, "extn" only. - assertEquals(ukNumber, phoneUtil.parseWithOptions("+442034567890\uFF45\uFF58\uFF54\uFF4E456", - new ParsingOptions().setDefaultRegion(RegionCode.GB))); + assertEquals(ukNumber, phoneUtil.parse("+442034567890\uFF45\uFF58\uFF54\uFF4E456", + RegionCode.GB)); // "xtn" only. - assertEquals(ukNumber, phoneUtil.parseWithOptions("+442034567890\uFF58\uFF54\uFF4E456", - new ParsingOptions().setDefaultRegion(RegionCode.GB))); + assertEquals(ukNumber, phoneUtil.parse("+442034567890\uFF58\uFF54\uFF4E456", + RegionCode.GB)); // "xt" only. - assertEquals(ukNumber, phoneUtil.parseWithOptions("+442034567890\uFF58\uFF54456", - new ParsingOptions().setDefaultRegion(RegionCode.GB))); + assertEquals(ukNumber, phoneUtil.parse("+442034567890\uFF58\uFF54456", + RegionCode.GB)); PhoneNumber usWithExtension = new PhoneNumber(); usWithExtension.setCountryCode(1).setNationalNumber(8009013355L).setExtension("7246433"); - assertEquals(usWithExtension, phoneUtil.parseWithOptions("(800) 901-3355 x 7246433", new ParsingOptions().setDefaultRegion(RegionCode.US))); - assertEquals(usWithExtension, phoneUtil.parseWithOptions("(800) 901-3355 , ext 7246433", new ParsingOptions().setDefaultRegion(RegionCode.US))); - assertEquals(usWithExtension, phoneUtil.parseWithOptions("(800) 901-3355 ; 7246433", new ParsingOptions().setDefaultRegion(RegionCode.US))); + assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 x 7246433", RegionCode.US)); + assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 , ext 7246433", RegionCode.US)); + assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 ; 7246433", RegionCode.US)); // To test an extension character without surrounding spaces. - assertEquals(usWithExtension, phoneUtil.parseWithOptions("(800) 901-3355;7246433", new ParsingOptions().setDefaultRegion(RegionCode.US))); + assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355;7246433", RegionCode.US)); assertEquals(usWithExtension, - phoneUtil.parseWithOptions("(800) 901-3355 ,extension 7246433", new ParsingOptions().setDefaultRegion(RegionCode.US))); + phoneUtil.parse("(800) 901-3355 ,extension 7246433", RegionCode.US)); assertEquals(usWithExtension, - phoneUtil.parseWithOptions("(800) 901-3355 ,extensi\u00F3n 7246433", new ParsingOptions().setDefaultRegion(RegionCode.US))); + phoneUtil.parse("(800) 901-3355 ,extensi\u00F3n 7246433", RegionCode.US)); // Repeat with the small letter o with acute accent created by combining characters. assertEquals(usWithExtension, - phoneUtil.parseWithOptions("(800) 901-3355 ,extensio\u0301n 7246433", new ParsingOptions().setDefaultRegion(RegionCode.US))); - assertEquals(usWithExtension, phoneUtil.parseWithOptions("(800) 901-3355 , 7246433", new ParsingOptions().setDefaultRegion(RegionCode.US))); - assertEquals(usWithExtension, phoneUtil.parseWithOptions("(800) 901-3355 ext: 7246433", new ParsingOptions().setDefaultRegion(RegionCode.US))); + phoneUtil.parse("(800) 901-3355 ,extensio\u0301n 7246433", RegionCode.US)); + assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 , 7246433", RegionCode.US)); + assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 ext: 7246433", RegionCode.US)); // Testing Russian extension \u0434\u043E\u0431 with variants found online. PhoneNumber ruWithExtension = new PhoneNumber(); ruWithExtension.setCountryCode(7).setNationalNumber(4232022511L).setExtension("100"); assertEquals(ruWithExtension, - phoneUtil.parseWithOptions("8 (423) 202-25-11, \u0434\u043E\u0431. 100", new ParsingOptions().setDefaultRegion(RegionCode.RU))); + phoneUtil.parse("8 (423) 202-25-11, \u0434\u043E\u0431. 100", RegionCode.RU)); assertEquals(ruWithExtension, - phoneUtil.parseWithOptions("8 (423) 202-25-11 \u0434\u043E\u0431. 100", new ParsingOptions().setDefaultRegion(RegionCode.RU))); + phoneUtil.parse("8 (423) 202-25-11 \u0434\u043E\u0431. 100", RegionCode.RU)); assertEquals(ruWithExtension, - phoneUtil.parseWithOptions("8 (423) 202-25-11, \u0434\u043E\u0431 100", new ParsingOptions().setDefaultRegion(RegionCode.RU))); + phoneUtil.parse("8 (423) 202-25-11, \u0434\u043E\u0431 100", RegionCode.RU)); assertEquals(ruWithExtension, - phoneUtil.parseWithOptions("8 (423) 202-25-11 \u0434\u043E\u0431 100", new ParsingOptions().setDefaultRegion(RegionCode.RU))); + phoneUtil.parse("8 (423) 202-25-11 \u0434\u043E\u0431 100", RegionCode.RU)); assertEquals(ruWithExtension, - phoneUtil.parseWithOptions("8 (423) 202-25-11\u0434\u043E\u0431100", new ParsingOptions().setDefaultRegion(RegionCode.RU))); + phoneUtil.parse("8 (423) 202-25-11\u0434\u043E\u0431100", RegionCode.RU)); // In upper case assertEquals(ruWithExtension, - phoneUtil.parseWithOptions("8 (423) 202-25-11, \u0414\u041E\u0411. 100", new ParsingOptions().setDefaultRegion(RegionCode.RU))); + phoneUtil.parse("8 (423) 202-25-11, \u0414\u041E\u0411. 100", RegionCode.RU)); // Test that if a number has two extensions specified, we ignore the second. PhoneNumber usWithTwoExtensionsNumber = new PhoneNumber(); usWithTwoExtensionsNumber.setCountryCode(1).setNationalNumber(2121231234L).setExtension("508"); - assertEquals(usWithTwoExtensionsNumber, phoneUtil.parseWithOptions("(212)123-1234 x508/x1234", - new ParsingOptions().setDefaultRegion(RegionCode.US))); - assertEquals(usWithTwoExtensionsNumber, phoneUtil.parseWithOptions("(212)123-1234 x508/ x1234", - new ParsingOptions().setDefaultRegion(RegionCode.US))); - assertEquals(usWithTwoExtensionsNumber, phoneUtil.parseWithOptions("(212)123-1234 x508\\x1234", - new ParsingOptions().setDefaultRegion(RegionCode.US))); + assertEquals(usWithTwoExtensionsNumber, phoneUtil.parse("(212)123-1234 x508/x1234", + RegionCode.US)); + assertEquals(usWithTwoExtensionsNumber, phoneUtil.parse("(212)123-1234 x508/ x1234", + RegionCode.US)); + assertEquals(usWithTwoExtensionsNumber, phoneUtil.parse("(212)123-1234 x508\\x1234", + RegionCode.US)); // Test parsing numbers in the form (645) 123-1234-910# works, where the last 3 digits before // the # are an extension. usWithExtension.clear(); usWithExtension.setCountryCode(1).setNationalNumber(6451231234L).setExtension("910"); - assertEquals(usWithExtension, phoneUtil.parseWithOptions("+1 (645) 123 1234-910#", new ParsingOptions().setDefaultRegion(RegionCode.US))); + assertEquals(usWithExtension, phoneUtil.parse("+1 (645) 123 1234-910#", RegionCode.US)); // Retry with the same number in a slightly different format. - assertEquals(usWithExtension, phoneUtil.parseWithOptions("+1 (645) 123 1234 ext. 910#", new ParsingOptions().setDefaultRegion(RegionCode.US))); + assertEquals(usWithExtension, phoneUtil.parse("+1 (645) 123 1234 ext. 910#", RegionCode.US)); } public void testParseHandlesLongExtensionsWithExplicitLabels() throws Exception { @@ -2704,13 +2703,13 @@ public void testParseHandlesLongExtensionsWithExplicitLabels() throws Exception // Firstly, when in RFC format: PhoneNumberUtil.extLimitAfterExplicitLabel nzNumber.setExtension("0"); - assertEquals(nzNumber, phoneUtil.parseWithOptions("tel:+6433316005;ext=0", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(nzNumber, phoneUtil.parse("tel:+6433316005;ext=0", RegionCode.NZ)); nzNumber.setExtension("01234567890123456789"); assertEquals( - nzNumber, phoneUtil.parseWithOptions("tel:+6433316005;ext=01234567890123456789", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + nzNumber, phoneUtil.parse("tel:+6433316005;ext=01234567890123456789", RegionCode.NZ)); // Extension too long. try { - phoneUtil.parseWithOptions("tel:+6433316005;ext=012345678901234567890", new ParsingOptions().setDefaultRegion(RegionCode.NZ)); + phoneUtil.parse("tel:+6433316005;ext=012345678901234567890", RegionCode.NZ); fail( "This should not parse as length of extension is higher than allowed: " + "tel:+6433316005;ext=012345678901234567890"); @@ -2724,21 +2723,21 @@ public void testParseHandlesLongExtensionsWithExplicitLabels() throws Exception // Explicit extension label: PhoneNumberUtil.extLimitAfterExplicitLabel nzNumber.setExtension("1"); - assertEquals(nzNumber, phoneUtil.parseWithOptions("03 3316005ext:1", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(nzNumber, phoneUtil.parse("03 3316005ext:1", RegionCode.NZ)); nzNumber.setExtension("12345678901234567890"); - assertEquals(nzNumber, phoneUtil.parseWithOptions("03 3316005 xtn:12345678901234567890", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(nzNumber, phoneUtil.parse("03 3316005 xtn:12345678901234567890", RegionCode.NZ)); assertEquals( - nzNumber, phoneUtil.parseWithOptions("03 3316005 extension\t12345678901234567890", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + nzNumber, phoneUtil.parse("03 3316005 extension\t12345678901234567890", RegionCode.NZ)); assertEquals( - nzNumber, phoneUtil.parseWithOptions("03 3316005 xtensio:12345678901234567890", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + nzNumber, phoneUtil.parse("03 3316005 xtensio:12345678901234567890", RegionCode.NZ)); assertEquals( - nzNumber, phoneUtil.parseWithOptions("03 3316005 xtensi\u00F3n, 12345678901234567890#", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + nzNumber, phoneUtil.parse("03 3316005 xtensi\u00F3n, 12345678901234567890#", RegionCode.NZ)); assertEquals( - nzNumber, phoneUtil.parseWithOptions("03 3316005extension.12345678901234567890", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); - assertEquals(nzNumber, phoneUtil.parseWithOptions("03 3316005 \u0434\u043E\u0431:12345678901234567890", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + nzNumber, phoneUtil.parse("03 3316005extension.12345678901234567890", RegionCode.NZ)); + assertEquals(nzNumber, phoneUtil.parse("03 3316005 \u0434\u043E\u0431:12345678901234567890", RegionCode.NZ)); // Extension too long. try { - phoneUtil.parseWithOptions("03 3316005 extension 123456789012345678901", new ParsingOptions().setDefaultRegion(RegionCode.NZ)); + phoneUtil.parse("03 3316005 extension 123456789012345678901", RegionCode.NZ); fail( "This should not parse as length of extension is higher than allowed: " + "03 3316005 extension 123456789012345678901"); @@ -2758,15 +2757,15 @@ public void testParseHandlesLongExtensionsWithAutoDiallingLabels() throws Except usNumberUserInput.setCountryCode(1).setNationalNumber(2679000000L); usNumberUserInput.setExtension("123456789012345"); assertEquals( - usNumberUserInput, phoneUtil.parseWithOptions("+12679000000,,123456789012345#", new ParsingOptions().setDefaultRegion(RegionCode.US))); + usNumberUserInput, phoneUtil.parse("+12679000000,,123456789012345#", RegionCode.US)); assertEquals( - usNumberUserInput, phoneUtil.parseWithOptions("+12679000000;123456789012345#", new ParsingOptions().setDefaultRegion(RegionCode.US))); + usNumberUserInput, phoneUtil.parse("+12679000000;123456789012345#", RegionCode.US)); PhoneNumber ukNumberUserInput = new PhoneNumber(); ukNumberUserInput.setCountryCode(44).setNationalNumber(2034000000L).setExtension("123456789"); - assertEquals(ukNumberUserInput, phoneUtil.parseWithOptions("+442034000000,,123456789#", new ParsingOptions().setDefaultRegion(RegionCode.GB))); + assertEquals(ukNumberUserInput, phoneUtil.parse("+442034000000,,123456789#", RegionCode.GB)); // Extension too long. try { - phoneUtil.parseWithOptions("+12679000000,,1234567890123456#", new ParsingOptions().setDefaultRegion(RegionCode.US)); + phoneUtil.parse("+12679000000,,1234567890123456#", RegionCode.US); fail( "This should not parse as length of extension is higher than allowed: " + "+12679000000,,1234567890123456#"); @@ -2786,13 +2785,13 @@ public void testParseHandlesShortExtensionsWithAmbiguousChar() throws Exception // Thirdly, for single and non-standard cases: // PhoneNumberUtil.extLimitAfterAmbiguousChar nzNumber.setExtension("123456789"); - assertEquals(nzNumber, phoneUtil.parseWithOptions("03 3316005 x 123456789", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); - assertEquals(nzNumber, phoneUtil.parseWithOptions("03 3316005 x. 123456789", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); - assertEquals(nzNumber, phoneUtil.parseWithOptions("03 3316005 #123456789#", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); - assertEquals(nzNumber, phoneUtil.parseWithOptions("03 3316005 ~ 123456789", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(nzNumber, phoneUtil.parse("03 3316005 x 123456789", RegionCode.NZ)); + assertEquals(nzNumber, phoneUtil.parse("03 3316005 x. 123456789", RegionCode.NZ)); + assertEquals(nzNumber, phoneUtil.parse("03 3316005 #123456789#", RegionCode.NZ)); + assertEquals(nzNumber, phoneUtil.parse("03 3316005 ~ 123456789", RegionCode.NZ)); // Extension too long. try { - phoneUtil.parseWithOptions("03 3316005 ~ 1234567890", new ParsingOptions().setDefaultRegion(RegionCode.NZ)); + phoneUtil.parse("03 3316005 ~ 1234567890", RegionCode.NZ); fail( "This should not parse as length of extension is higher than allowed: " + "03 3316005 ~ 1234567890"); @@ -2810,12 +2809,12 @@ public void testParseHandlesShortExtensionsWhenNotSureOfLabel() throws Exception // PhoneNumberUtil.extLimitWhenNotSure PhoneNumber usNumber = new PhoneNumber(); usNumber.setCountryCode(1).setNationalNumber(1234567890L).setExtension("666666"); - assertEquals(usNumber, phoneUtil.parseWithOptions("+1123-456-7890 666666#", new ParsingOptions().setDefaultRegion(RegionCode.US))); + assertEquals(usNumber, phoneUtil.parse("+1123-456-7890 666666#", RegionCode.US)); usNumber.setExtension("6"); - assertEquals(usNumber, phoneUtil.parseWithOptions("+11234567890-6#", new ParsingOptions().setDefaultRegion(RegionCode.US))); + assertEquals(usNumber, phoneUtil.parse("+11234567890-6#", RegionCode.US)); // Extension too long. try { - phoneUtil.parseWithOptions("+1123-456-7890 7777777#", new ParsingOptions().setDefaultRegion(RegionCode.US)); + phoneUtil.parse("+1123-456-7890 7777777#", RegionCode.US); fail( "This should not parse as length of extension is higher than allowed: " + "+1123-456-7890 7777777#"); @@ -2833,28 +2832,28 @@ public void testParseAndKeepRaw() throws Exception { setRawInput("800 six-flags"). setCountryCodeSource(CountryCodeSource.FROM_DEFAULT_COUNTRY); assertEquals(alphaNumericNumber, - phoneUtil.parseAndKeepRawInput("800 six-flags", RegionCode.US)); + phoneUtil.parseWithOptions("800 six-flags", new ParsingOptions().setDefaultRegion(RegionCode.US).setKeepRawInput(true))); PhoneNumber shorterAlphaNumber = new PhoneNumber(). setCountryCode(1).setNationalNumber(8007493524L). setRawInput("1800 six-flag"). setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN); assertEquals(shorterAlphaNumber, - phoneUtil.parseAndKeepRawInput("1800 six-flag", RegionCode.US)); + phoneUtil.parseWithOptions("1800 six-flag", new ParsingOptions().setDefaultRegion(RegionCode.US).setKeepRawInput(true))); shorterAlphaNumber.setRawInput("+1800 six-flag"). setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN); assertEquals(shorterAlphaNumber, - phoneUtil.parseAndKeepRawInput("+1800 six-flag", RegionCode.NZ)); + phoneUtil.parseWithOptions("+1800 six-flag", new ParsingOptions().setDefaultRegion(RegionCode.NZ).setKeepRawInput(true))); shorterAlphaNumber.setRawInput("001800 six-flag"). setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_IDD); assertEquals(shorterAlphaNumber, - phoneUtil.parseAndKeepRawInput("001800 six-flag", RegionCode.NZ)); + phoneUtil.parseWithOptions("001800 six-flag", new ParsingOptions().setDefaultRegion(RegionCode.NZ).setKeepRawInput(true))); // Invalid region code supplied. try { - phoneUtil.parseAndKeepRawInput("123 456 7890", "CS"); + phoneUtil.parseWithOptions("123 456 7890", new ParsingOptions().setDefaultRegion("CS").setKeepRawInput(true)); fail("Deprecated region code not allowed: should fail."); } catch (NumberParseException e) { // Expected this exception. @@ -2867,32 +2866,32 @@ public void testParseAndKeepRaw() throws Exception { koreanNumber.setCountryCode(82).setNationalNumber(22123456).setRawInput("08122123456"). setCountryCodeSource(CountryCodeSource.FROM_DEFAULT_COUNTRY). setPreferredDomesticCarrierCode("81"); - assertEquals(koreanNumber, phoneUtil.parseAndKeepRawInput("08122123456", RegionCode.KR)); + assertEquals(koreanNumber, phoneUtil.parseWithOptions("08122123456", new ParsingOptions().setDefaultRegion(RegionCode.KR).setKeepRawInput(true))); } public void testParseItalianLeadingZeros() throws Exception { // Test the number "011". PhoneNumber oneZero = new PhoneNumber(); oneZero.setCountryCode(61).setNationalNumber(11L).setItalianLeadingZero(true); - assertEquals(oneZero, phoneUtil.parseWithOptions("011", new ParsingOptions().setDefaultRegion(RegionCode.AU))); + assertEquals(oneZero, phoneUtil.parse("011", RegionCode.AU)); // Test the number "001". PhoneNumber twoZeros = new PhoneNumber(); twoZeros.setCountryCode(61).setNationalNumber(1).setItalianLeadingZero(true) .setNumberOfLeadingZeros(2); - assertEquals(twoZeros, phoneUtil.parseWithOptions("001", new ParsingOptions().setDefaultRegion(RegionCode.AU))); + assertEquals(twoZeros, phoneUtil.parse("001", RegionCode.AU)); // Test the number "000". This number has 2 leading zeros. PhoneNumber stillTwoZeros = new PhoneNumber(); stillTwoZeros.setCountryCode(61).setNationalNumber(0L).setItalianLeadingZero(true) .setNumberOfLeadingZeros(2); - assertEquals(stillTwoZeros, phoneUtil.parseWithOptions("000", new ParsingOptions().setDefaultRegion(RegionCode.AU))); + assertEquals(stillTwoZeros, phoneUtil.parse("000", RegionCode.AU)); // Test the number "0000". This number has 3 leading zeros. PhoneNumber threeZeros = new PhoneNumber(); threeZeros.setCountryCode(61).setNationalNumber(0L).setItalianLeadingZero(true) .setNumberOfLeadingZeros(3); - assertEquals(threeZeros, phoneUtil.parseWithOptions("0000", new ParsingOptions().setDefaultRegion(RegionCode.AU))); + assertEquals(threeZeros, phoneUtil.parse("0000", RegionCode.AU)); } public void testParseWithPhoneContext() throws Exception { @@ -2900,37 +2899,37 @@ public void testParseWithPhoneContext() throws Exception { // descriptor = domainname / global-number-digits // Valid global-phone-digits - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("tel:033316005;phone-context=+64", new ParsingOptions().setDefaultRegion(RegionCode.ZZ))); + assertEquals(NZ_NUMBER, phoneUtil.parse("tel:033316005;phone-context=+64", RegionCode.ZZ)); assertEquals( NZ_NUMBER, - phoneUtil.parseWithOptions( + phoneUtil.parse( "tel:033316005;phone-context=+64;{this isn't part of phone-context anymore!}", - new ParsingOptions().setDefaultRegion(RegionCode.ZZ))); + RegionCode.ZZ)); PhoneNumber nzFromPhoneContext = new PhoneNumber(); nzFromPhoneContext.setCountryCode(64).setNationalNumber(3033316005L); assertEquals( nzFromPhoneContext, - phoneUtil.parseWithOptions("tel:033316005;phone-context=+64-3", new ParsingOptions().setDefaultRegion(RegionCode.ZZ))); + phoneUtil.parse("tel:033316005;phone-context=+64-3", RegionCode.ZZ)); PhoneNumber brFromPhoneContext = new PhoneNumber(); brFromPhoneContext.setCountryCode(55).setNationalNumber(5033316005L); assertEquals( brFromPhoneContext, - phoneUtil.parseWithOptions("tel:033316005;phone-context=+(555)", new ParsingOptions().setDefaultRegion(RegionCode.ZZ))); + phoneUtil.parse("tel:033316005;phone-context=+(555)", RegionCode.ZZ)); PhoneNumber usFromPhoneContext = new PhoneNumber(); usFromPhoneContext.setCountryCode(1).setNationalNumber(23033316005L); assertEquals( usFromPhoneContext, - phoneUtil.parseWithOptions("tel:033316005;phone-context=+-1-2.3()", new ParsingOptions().setDefaultRegion(RegionCode.ZZ))); + phoneUtil.parse("tel:033316005;phone-context=+-1-2.3()", RegionCode.ZZ)); // Valid domainname - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("tel:033316005;phone-context=abc.nz", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + assertEquals(NZ_NUMBER, phoneUtil.parse("tel:033316005;phone-context=abc.nz", RegionCode.NZ)); assertEquals( NZ_NUMBER, - phoneUtil.parseWithOptions("tel:033316005;phone-context=www.PHONE-numb3r.com", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("tel:033316005;phone-context=a", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + phoneUtil.parse("tel:033316005;phone-context=www.PHONE-numb3r.com", RegionCode.NZ)); + assertEquals(NZ_NUMBER, phoneUtil.parse("tel:033316005;phone-context=a", RegionCode.NZ)); assertEquals( - NZ_NUMBER, phoneUtil.parseWithOptions("tel:033316005;phone-context=3phone.J.", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); - assertEquals(NZ_NUMBER, phoneUtil.parseWithOptions("tel:033316005;phone-context=a--z", new ParsingOptions().setDefaultRegion(RegionCode.NZ))); + NZ_NUMBER, phoneUtil.parse("tel:033316005;phone-context=3phone.J.", RegionCode.NZ)); + assertEquals(NZ_NUMBER, phoneUtil.parse("tel:033316005;phone-context=a--z", RegionCode.NZ)); // Invalid descriptor assertThrowsForInvalidPhoneContext("tel:033316005;phone-context="); @@ -2952,7 +2951,7 @@ private void assertThrowsForInvalidPhoneContext(String numberToParse) { NumberParseException.class, new ThrowingRunnable() { @Override public void run() throws Throwable { - phoneUtil.parseWithOptions(numberToParseFinal, new ParsingOptions().setDefaultRegion(RegionCode.ZZ)); + phoneUtil.parse(numberToParseFinal, RegionCode.ZZ); } }) .getErrorType()); diff --git a/pending_code_changes.txt b/pending_code_changes.txt index 8b13789179..15a8e10952 100644 --- a/pending_code_changes.txt +++ b/pending_code_changes.txt @@ -1 +1,4 @@ - +Code changes: + - Introduced new function parseWithOptions to customize the parser's behaviour + - Deprecated parseAndKeepRawInput in favor of parseWithOptions + - \ No newline at end of file From 5155f580b8e7ee77596124efe865842a8681bbf4 Mon Sep 17 00:00:00 2001 From: KarolJakubKrawiec Date: Thu, 12 Dec 2024 09:41:43 +0100 Subject: [PATCH 6/7] Rewrite comments I rewrote some comments because they didnt make sense and were misplaced --- .../src/com/google/i18n/phonenumbers/ParsingOptions.java | 2 +- .../test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/ParsingOptions.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/ParsingOptions.java index 1fc1ceb7ec..af3eb1aef0 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/ParsingOptions.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/ParsingOptions.java @@ -18,13 +18,13 @@ /** Options for the phone number parser. */ public class ParsingOptions { + /** * Returns the region we are expecting the number to be from. This is ignored if the number being * parsed is written in international format. In case of national format, the country_code will be * set to the one of this default region. If the number is guaranteed to start with a '+' followed * by the country calling code, then RegionCode.ZZ or null can be supplied. */ - private boolean hasDefaultRegion; private String defaultRegion_ = null; public boolean hasDefaultRegion() { return hasDefaultRegion; } diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java index 0f6f8a40fc..66b33fbeeb 100644 --- a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java +++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java @@ -2085,7 +2085,7 @@ public void testMaybeExtractCountryCode() { public void testParseNationalNumber() throws Exception { // National prefix attached. assertEquals(NZ_NUMBER, phoneUtil.parse("033316005", RegionCode.NZ)); - // Some fields are not filled in by parse, but only by parseWithOptions. + // Some fields are not filled in by parse, but only by parseWithOptions with the keepRawInput option set. assertFalse(NZ_NUMBER.hasCountryCodeSource()); assertEquals(CountryCodeSource.UNSPECIFIED, NZ_NUMBER.getCountryCodeSource()); From 9d7975ac946e456df8ccefd0b0f8fab77d0df6f0 Mon Sep 17 00:00:00 2001 From: KarolJakubKrawiec Date: Thu, 12 Dec 2024 13:44:40 +0100 Subject: [PATCH 7/7] Create ParsingOptions for cpp In this commit we added the ParsingOptions from the java version which will make parsing in different ways easier --- cpp/src/phonenumbers/parsingOptions.cc | 20 ++++ cpp/src/phonenumbers/parsingOptions.h | 43 +++++++ cpp/src/phonenumbers/phonenumberutil.cc | 14 ++- cpp/src/phonenumbers/phonenumberutil.h | 25 +++- cpp/test/phonenumbers/phonenumberutil_test.cc | 111 +++++++++++------- 5 files changed, 159 insertions(+), 54 deletions(-) create mode 100644 cpp/src/phonenumbers/parsingOptions.cc create mode 100644 cpp/src/phonenumbers/parsingOptions.h diff --git a/cpp/src/phonenumbers/parsingOptions.cc b/cpp/src/phonenumbers/parsingOptions.cc new file mode 100644 index 0000000000..1a955bc0ac --- /dev/null +++ b/cpp/src/phonenumbers/parsingOptions.cc @@ -0,0 +1,20 @@ +#include "i18n/phonenumbers/parsingoptions.h" + +#include "i18n/identifiers/regioncode.h" + +namespace i18n { +namespace phonenumbers { + +ParsingOptions& ParsingOptions::SetDefaultRegion( + i18n_identifiers::RegionCode default_region) { + default_region_ = default_region; + return *this; +} + +ParsingOptions& ParsingOptions::SetKeepRawInput(bool keep_raw_input) { + keep_raw_input_ = keep_raw_input; + return *this; +} + +} // namespace phonenumbers +} // namespace i18n \ No newline at end of file diff --git a/cpp/src/phonenumbers/parsingOptions.h b/cpp/src/phonenumbers/parsingOptions.h new file mode 100644 index 0000000000..917a115262 --- /dev/null +++ b/cpp/src/phonenumbers/parsingOptions.h @@ -0,0 +1,43 @@ +#ifndef I18N_PHONENUMBERS_PARSINGOPTIONS_H_ +#define I18N_PHONENUMBERS_PARSINGOPTIONS_H_ + +#include "i18n/identifiers/regioncode.h" + +namespace i18n { +namespace phonenumbers { + +// Options for parsing a phone number. To be used with the ParseWithOptions +// method. +// Example: +// ParsingOptions().SetDefaultRegion(RegionCode::US()).SetKeepRawInput(true); +class ParsingOptions { + public: + ParsingOptions() = default; + + // Set the value for default_region_. + ParsingOptions& SetDefaultRegion( + i18n_identifiers::RegionCode default_region); + + // Set the value for keep_raw_input_. + ParsingOptions& SetKeepRawInput(bool keep_raw_input); + + private: + friend class PhoneNumberUtil; + + // The region we are expecting the number to be from. This is ignored if the + // number being parsed is written in international format. In case of national + // format, the country_code will be set to the one of this default region. If + // the number is guaranteed to start with a '+' followed by the country + // calling code, then RegionCode.ZZ or null can be supplied. + i18n_identifiers::RegionCode default_region_ = + i18n_identifiers::RegionCode::ZZ(); + + // Whether the raw input should be kept in the PhoneNumber object. If true, + // the raw_input field and country_code_source fields will be populated. + bool keep_raw_input_ = false; +}; + +} // namespace phonenumbers +} // namespace i18n + +#endif // I1 \ No newline at end of file diff --git a/cpp/src/phonenumbers/phonenumberutil.cc b/cpp/src/phonenumbers/phonenumberutil.cc index f1442974f3..2bcfe2154f 100644 --- a/cpp/src/phonenumbers/phonenumberutil.cc +++ b/cpp/src/phonenumbers/phonenumberutil.cc @@ -37,6 +37,7 @@ #include "phonenumbers/phonemetadata.pb.h" #include "phonenumbers/phonenumber.h" #include "phonenumbers/phonenumber.pb.h" +#include "phonenumbers/parsingoptions.h" #include "phonenumbers/regex_based_matcher.h" #include "phonenumbers/regexp_adapter.h" #include "phonenumbers/regexp_cache.h" @@ -47,6 +48,7 @@ #include "phonenumbers/utf/unicodetext.h" #include "phonenumbers/utf/utf.h" + namespace i18n { namespace phonenumbers { @@ -2127,15 +2129,17 @@ PhoneNumberUtil::ErrorType PhoneNumberUtil::Parse(const string& number_to_parse, const string& default_region, PhoneNumber* number) const { DCHECK(number); - return ParseHelper(number_to_parse, default_region, false, true, number); + return ParseWithOptions(number_to_parse, + ParsingOptions().SetDefaultRegion(default_region), + number); } -PhoneNumberUtil::ErrorType PhoneNumberUtil::ParseAndKeepRawInput( - const string& number_to_parse, - const string& default_region, +PhoneNumberUtil::ErrorType PhoneNumberUtil::ParseWithOptions( + absl::string_view number_to_parse, const ParsingOptions& options, PhoneNumber* number) const { DCHECK(number); - return ParseHelper(number_to_parse, default_region, true, true, number); + return ParseHelper(number_to_parse, options.default_region_, + options.keep_raw_input_, /*check_region=*/true, number); } // Checks to see that the region code used is valid, or if it is not valid, that diff --git a/cpp/src/phonenumbers/phonenumberutil.h b/cpp/src/phonenumbers/phonenumberutil.h index 14cfc670c7..d5e7e36398 100644 --- a/cpp/src/phonenumbers/phonenumberutil.h +++ b/cpp/src/phonenumbers/phonenumberutil.h @@ -29,9 +29,10 @@ #include "phonenumbers/base/memory/scoped_ptr.h" #include "phonenumbers/base/memory/singleton.h" #include "phonenumbers/phonenumber.pb.h" - +#include "phonenumbers/parsingoptions.h" #include "absl/container/node_hash_set.h" #include "absl/container/node_hash_map.h" +#include "third_party/absl/base/macros.h" class TelephoneNumber; @@ -700,13 +701,29 @@ class PhoneNumberUtil : public Singleton { ErrorType Parse(const string& number_to_parse, const string& default_region, PhoneNumber* number) const; + // Parses a string and returns it in proto buffer format. This method differs // from Parse() in that it always populates the raw_input field of the // protocol buffer with number_to_parse as well as the country_code_source // field. - ErrorType ParseAndKeepRawInput(const string& number_to_parse, - const string& default_region, - PhoneNumber* number) const; + ABSL_DEPRECATE_AND_INLINE() + ErrorType ParseAndKeepRawInput( + absl::string_view number_to_parse, + i18n_identifiers::RegionCode default_region + PhoneNumber* number) const { + return ParseWithOptions(number_to_parse, + ParsingOptions() + .SetDefaultRegion(default_region) + .SetKeepRawInput(true), + number); + } + + // Parses a string and returns it in proto buffer format. This method differs + // from Parse() in that it allows the caller to change the behavior of the + // parser. See ParsingOptions for more details. + ErrorType ParseWithOptions(absl::string_view number_to_parse, + const ParsingOptions& options, + PhoneNumber* number) const; // Takes two phone numbers and compares them for equality. // diff --git a/cpp/test/phonenumbers/phonenumberutil_test.cc b/cpp/test/phonenumbers/phonenumberutil_test.cc index 4f69903492..75824b3475 100644 --- a/cpp/test/phonenumbers/phonenumberutil_test.cc +++ b/cpp/test/phonenumbers/phonenumberutil_test.cc @@ -64,9 +64,9 @@ class PhoneNumberUtilTest : public testing::Test { return phone_util_.GetMetadataForRegion(region_code); } - const PhoneMetadata* GetMetadataForNonGeographicalRegion( + const PhoneMetadata* GetMetadataForNonGeographicalEntity( int country_code) const { - return phone_util_.GetMetadataForNonGeographicalRegion(country_code); + return phone_util_.GetMetadataForNonGeographicalEntity(country_code); } void ExtractPossibleNumber(const string& number, @@ -341,7 +341,7 @@ TEST_F(PhoneNumberUtilTest, GetInstanceLoadARMetadata) { } TEST_F(PhoneNumberUtilTest, GetInstanceLoadInternationalTollFreeMetadata) { - const PhoneMetadata* metadata = GetMetadataForNonGeographicalRegion(800); + const PhoneMetadata* metadata = GetMetadataForNonGeographicalEntity(800); EXPECT_FALSE(metadata == NULL); EXPECT_EQ("001", metadata->id()); EXPECT_EQ(800, metadata->country_code()); @@ -2408,7 +2408,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { string formatted_number; EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("+442087654321", RegionCode::GB(), + phone_util_.ParseWithOptions("+442087654321", ParsingOptions().SetDefaultRegion(RegionCode::GB()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::GB(), &formatted_number); @@ -2417,7 +2418,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("02087654321", RegionCode::GB(), + phone_util_.ParseWithOptions("02087654321", ParsingOptions().SetDefaultRegion(RegionCode::GB()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::GB(), &formatted_number); @@ -2426,8 +2428,9 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("011442087654321", - RegionCode::US(), &phone_number)); + phone_util_.ParseWithOptions("011442087654321", ParsingOptions().SetDefaultRegion(RegionCode::US(),) + SetKeepRawInput(true), + &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::US(), &formatted_number); EXPECT_EQ("011 44 20 8765 4321", formatted_number); @@ -2435,7 +2438,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("442087654321", RegionCode::GB(), + phone_util_.ParseWithOptions("442087654321", ParsingOptions().SetDefaultRegion(RegionCode::GB()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::GB(), &formatted_number); @@ -2456,7 +2460,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("7345678901", RegionCode::US(), + phone_util_.ParseWithOptions("7345678901", ParsingOptions().SetDefaultRegion(RegionCode::US()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::US(), &formatted_number); @@ -2467,7 +2472,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("0734567 8901", RegionCode::US(), + phone_util_.ParseWithOptions("0734567 8901", ParsingOptions().SetDefaultRegion(RegionCode::US()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::US(), &formatted_number); @@ -2478,7 +2484,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("02-4567-8900", RegionCode::KR(), + phone_util_.ParseWithOptions("02-4567-8900", ParsingOptions().SetDefaultRegion(RegionCode::KR()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::KR(), &formatted_number); @@ -2487,8 +2494,9 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("01180012345678", - RegionCode::US(), &phone_number)); + phone_util_.ParseWithOptions("01180012345678", ParsingOptions().SetDefaultRegion(RegionCode::US(),) + SetKeepRawInput(true), + &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::US(), &formatted_number); EXPECT_EQ("011 800 1234 5678", formatted_number); @@ -2496,7 +2504,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("+80012345678", RegionCode::KR(), + phone_util_.ParseWithOptions("+80012345678", ParsingOptions().SetDefaultRegion(RegionCode::KR()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::KR(), &formatted_number); @@ -2507,7 +2516,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("2530000", RegionCode::US(), + phone_util_.ParseWithOptions("2530000", ParsingOptions().SetDefaultRegion(RegionCode::US()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::US(), &formatted_number); @@ -2517,7 +2527,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { formatted_number.clear(); // Number with national prefix in the US. EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("18003456789", RegionCode::US(), + phone_util_.ParseWithOptions("18003456789", ParsingOptions().SetDefaultRegion(RegionCode::US()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::US(), &formatted_number); @@ -2527,7 +2538,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { formatted_number.clear(); // Number without national prefix in the UK. EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("2087654321", RegionCode::GB(), + phone_util_.ParseWithOptions("2087654321", ParsingOptions().SetDefaultRegion(RegionCode::GB()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::GB(), &formatted_number); @@ -2547,7 +2559,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { formatted_number.clear(); // Number with national prefix in Mexico. EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("013312345678", RegionCode::MX(), + phone_util_.ParseWithOptions("013312345678", ParsingOptions().SetDefaultRegion(RegionCode::MX()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::MX(), &formatted_number); @@ -2557,7 +2570,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { formatted_number.clear(); // Number without national prefix in Mexico. EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("3312345678", RegionCode::MX(), + phone_util_.ParseWithOptions("3312345678", ParsingOptions().SetDefaultRegion(RegionCode::MX()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::MX(), &formatted_number); @@ -2567,7 +2581,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { formatted_number.clear(); // Italian fixed-line number. EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("0212345678", RegionCode::IT(), + phone_util_.ParseWithOptions("0212345678", ParsingOptions().SetDefaultRegion(RegionCode::IT()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::IT(), &formatted_number); @@ -2577,7 +2592,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { formatted_number.clear(); // Number with national prefix in Japan. EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("00777012", RegionCode::JP(), + phone_util_.ParseWithOptions("00777012", ParsingOptions().SetDefaultRegion(RegionCode::JP()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::JP(), &formatted_number); @@ -2587,7 +2603,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { formatted_number.clear(); // Number without national prefix in Japan. EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("0777012", RegionCode::JP(), + phone_util_.ParseWithOptions("0777012", ParsingOptions().SetDefaultRegion(RegionCode::JP()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::JP(), &formatted_number); @@ -2597,7 +2614,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { formatted_number.clear(); // Number with carrier code in Brazil. EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("012 3121286979", RegionCode::BR(), + phone_util_.ParseWithOptions("012 3121286979", ParsingOptions().SetDefaultRegion(RegionCode::BR()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::BR(), &formatted_number); @@ -2609,8 +2627,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { // national prefix 044 is entered, we return the raw input as we don't want to // change the number entered. EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("044(33)1234-5678", - RegionCode::MX(), + phone_util_.ParseWithOptions("044(33)1234-5678", ParsingOptions().SetDefaultRegion(RegionCode::MX()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::MX(), &formatted_number); @@ -2619,8 +2637,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("045(33)1234-5678", - RegionCode::MX(), + phone_util_.ParseWithOptions("045(33)1234-5678", ParsingOptions().SetDefaultRegion(RegionCode::MX()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::MX(), &formatted_number); @@ -2632,8 +2650,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("0012 16502530000", - RegionCode::AU(), + phone_util_.ParseWithOptions("0012 16502530000", ParsingOptions().SetDefaultRegion(RegionCode::AU()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::AU(), &formatted_number); @@ -2642,8 +2660,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("0011 16502530000", - RegionCode::AU(), + phone_util_.ParseWithOptions("0011 16502530000", ParsingOptions().SetDefaultRegion(RegionCode::AU()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::AU(), &formatted_number); @@ -2654,8 +2672,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("*1234", - RegionCode::JP(), + phone_util_.ParseWithOptions("*1234", ParsingOptions().SetDefaultRegion(RegionCode::JP()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::JP(), &formatted_number); @@ -2663,8 +2681,8 @@ TEST_F(PhoneNumberUtilTest, FormatInOriginalFormat) { phone_number.Clear(); formatted_number.clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("1234", - RegionCode::JP(), + phone_util_.ParseWithOptions("1234", ParsingOptions().SetDefaultRegion(RegionCode::JP()), + SetKeepRawInput(true), &phone_number)); phone_util_.FormatInOriginalFormat(phone_number, RegionCode::JP(), &formatted_number); @@ -3575,7 +3593,7 @@ TEST_F(PhoneNumberUtilTest, ParseNationalNumber) { EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, phone_util_.Parse("033316005", RegionCode::NZ(), &test_number)); EXPECT_EQ(nz_number, test_number); - // Some fields are not filled in by Parse, but only by ParseAndKeepRawInput. + // Some fields are not filled in by Parse, but only by ParseWithOptions. EXPECT_FALSE(nz_number.has_country_code_source()); EXPECT_EQ(PhoneNumber::UNSPECIFIED, nz_number.country_code_source()); @@ -4170,8 +4188,8 @@ TEST_F(PhoneNumberUtilTest, ParseNumbersWithPlusWithNoRegion) { nz_number.set_country_code_source(PhoneNumber::FROM_NUMBER_WITH_PLUS_SIGN); result_proto.Clear(); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("+64 3 331 6005", - RegionCode::GetUnknown(), + phone_util_.ParseWithOptions("+64 3 331 6005", ParsingOptions().SetDefaultRegion( RegionCode::GetUnknown()), + SetKeepRawInput(true), &result_proto)); EXPECT_EQ(nz_number, result_proto); } @@ -4621,7 +4639,8 @@ TEST_F(PhoneNumberUtilTest, ParseAndKeepRaw) { PhoneNumber test_number; EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("800 six-flags", RegionCode::US(), + phone_util_.ParseWithOptions("800 six-flags", ParsingOptions().SetDefaultRegion(RegionCode::US()), + SetKeepRawInput(true), &test_number)); EXPECT_EQ(alpha_numeric_number, test_number); @@ -4630,7 +4649,8 @@ TEST_F(PhoneNumberUtilTest, ParseAndKeepRaw) { alpha_numeric_number.set_country_code_source( PhoneNumber::FROM_NUMBER_WITHOUT_PLUS_SIGN); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("1800 six-flag", RegionCode::US(), + phone_util_.ParseWithOptions("1800 six-flag", ParsingOptions().SetDefaultRegion(RegionCode::US()), + SetKeepRawInput(true), &test_number)); EXPECT_EQ(alpha_numeric_number, test_number); @@ -4638,7 +4658,8 @@ TEST_F(PhoneNumberUtilTest, ParseAndKeepRaw) { alpha_numeric_number.set_country_code_source( PhoneNumber::FROM_NUMBER_WITH_PLUS_SIGN); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("+1800 six-flag", RegionCode::CN(), + phone_util_.ParseWithOptions("+1800 six-flag", ParsingOptions().SetDefaultRegion(RegionCode::CN()), + SetKeepRawInput(true), &test_number)); EXPECT_EQ(alpha_numeric_number, test_number); @@ -4646,8 +4667,8 @@ TEST_F(PhoneNumberUtilTest, ParseAndKeepRaw) { alpha_numeric_number.set_country_code_source( PhoneNumber::FROM_NUMBER_WITH_IDD); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("001800 six-flag", - RegionCode::NZ(), + phone_util_.ParseWithOptions("001800 six-flag", ParsingOptions().SetDefaultRegion( RegionCode::NZ()), + SetKeepRawInput(true), &test_number)); EXPECT_EQ(alpha_numeric_number, test_number); @@ -4665,8 +4686,8 @@ TEST_F(PhoneNumberUtilTest, ParseAndKeepRaw) { korean_number.set_country_code_source(PhoneNumber::FROM_DEFAULT_COUNTRY); korean_number.set_preferred_domestic_carrier_code("81"); EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, - phone_util_.ParseAndKeepRawInput("08122123456", - RegionCode::KR(), + phone_util_.ParseWithOptions("08122123456", ParsingOptions().SetDefaultRegion( RegionCode::KR()), + SetKeepRawInput(true), &test_number)); EXPECT_EQ(korean_number, test_number); }