From 45cb5e44c2dd5d12cf3ea665f8538e0e6dfd9fbf Mon Sep 17 00:00:00 2001 From: karoljk Date: Wed, 27 Nov 2024 12:55:46 +0000 Subject: [PATCH] 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 5e83a4625..1fc1ceb7e 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 6de3061a0..c09370c8d 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 @@ public class PhoneNumberUtil { 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 class PhoneNumberUtil { numberToParse, new ParsingOptions().setDefaultRegion(defaultRegion), phoneNumber); + parseWithOptions( + numberToParse, + new ParsingOptions().setDefaultRegion(defaultRegion), + phoneNumber); } /** @@ -3182,6 +3190,10 @@ public class PhoneNumberUtil { 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 class PhoneNumberUtil { 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)