Skip to content

Commit ed15e47

Browse files
Add parsingOptions to OpenSource
I have added the new parsing methode ParsingOptions to the OpenSource version.
1 parent dfdc8e4 commit ed15e47

2 files changed

Lines changed: 82 additions & 2 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (C) 2024 The Libphonenumber Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.i18n.phonenumbers;
18+
19+
/** Options for the phone number parser. */
20+
public class ParsingOptions {
21+
/**
22+
* Returns the region we are expecting the number to be from. This is ignored if the number being
23+
* parsed is written in international format. In case of national format, the country_code will be
24+
* set to the one of this default region. If the number is guaranteed to start with a '+' followed
25+
* by the country calling code, then RegionCode.ZZ or null can be supplied.
26+
*/
27+
28+
private boolean hasDefaultRegion;
29+
private String defaultRegion_ = null;
30+
public boolean hasDefaultRegion() { return hasDefaultRegion; }
31+
public String getDefaultRegion() { return defaultRegion_; }
32+
public ParsingOptions setDefaultRegion(String value) {
33+
if (value == null) {
34+
throw new NullPointerException();
35+
}
36+
hasDefaultRegion = true;
37+
defaultRegion_ = value;
38+
return this;
39+
}
40+
41+
/**
42+
* Returns whether the raw input should be kept in the PhoneNumber object. If true, the raw_input
43+
* field and country_code_source fields will be populated.
44+
*/
45+
private boolean hasKeepRawInput;
46+
private boolean keepRawInput_ = false;
47+
public boolean hasKeepRawInput() { return hasKeepRawInput; }
48+
public boolean isKeepRawInput() { return keepRawInput_; }
49+
public ParsingOptions setKeepRawInput(boolean value) {
50+
if(value == false) {
51+
52+
}
53+
hasKeepRawInput = true;
54+
keepRawInput_ = value;
55+
return this;
56+
}
57+
58+
public ParsingOptions withDefaultRegion(String regionCode) {
59+
return setDefaultRegion(regionCode);
60+
}
61+
}

java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3136,7 +3136,9 @@ private boolean checkRegionForParsing(CharSequence numberToParse, String default
31363136
* @throws NumberParseException if the string is not considered to be a viable phone number (e.g.
31373137
* too few or too many digits) or if no default region was supplied and the number is not in
31383138
* international format (does not start with +)
3139+
* @deprecated Use {@link #parseWithOptions(CharSequence, ParsingOptions)} instead.
31393140
*/
3141+
@Deprecated
31403142
public PhoneNumber parse(CharSequence numberToParse, String defaultRegion)
31413143
throws NumberParseException {
31423144
PhoneNumber phoneNumber = new PhoneNumber();
@@ -3147,7 +3149,9 @@ public PhoneNumber parse(CharSequence numberToParse, String defaultRegion)
31473149
/**
31483150
* Same as {@link #parse(CharSequence, String)}, but accepts mutable PhoneNumber as a
31493151
* parameter to decrease object creation when invoked many times.
3152+
* @deprecated Use {@link #parseWithOptions(CharSequence, ParsingOptions, PhoneNumber)} instead.
31503153
*/
3154+
@Deprecated
31513155
public void parse(CharSequence numberToParse, String defaultRegion, PhoneNumber phoneNumber)
31523156
throws NumberParseException {
31533157
parseHelper(numberToParse, defaultRegion, false, true, phoneNumber);
@@ -3166,7 +3170,9 @@ public void parse(CharSequence numberToParse, String defaultRegion, PhoneNumber
31663170
* @return a phone number proto buffer filled with the parsed number
31673171
* @throws NumberParseException if the string is not considered to be a viable phone number or if
31683172
* no default region was supplied
3173+
* @deprecated Use {@link #parseWithOptions(CharSequence, ParsingOptions)} instead.
31693174
*/
3175+
@Deprecated
31703176
public PhoneNumber parseAndKeepRawInput(CharSequence numberToParse, String defaultRegion)
31713177
throws NumberParseException {
31723178
PhoneNumber phoneNumber = new PhoneNumber();
@@ -3177,13 +3183,26 @@ public PhoneNumber parseAndKeepRawInput(CharSequence numberToParse, String defau
31773183
/**
31783184
* Same as{@link #parseAndKeepRawInput(CharSequence, String)}, but accepts a mutable
31793185
* PhoneNumber as a parameter to decrease object creation when invoked many times.
3186+
* @deprecated Use {@link #parseWithOptions(CharSequence, ParsingOptions, PhoneNumber)} instead.
31803187
*/
3181-
public void parseAndKeepRawInput(CharSequence numberToParse, String defaultRegion,
3182-
PhoneNumber phoneNumber)
3188+
@Deprecated
3189+
public void parseAndKeepRawInput(CharSequence numberToParse, String defaultRegion, PhoneNumber phoneNumber)
31833190
throws NumberParseException {
31843191
parseHelper(numberToParse, defaultRegion, true, true, phoneNumber);
31853192
}
31863193

3194+
public PhoneNumber parseWithOptions(CharSequence numberToParse, ParsingOptions options)
3195+
throws NumberParseException {
3196+
PhoneNumber phoneNumber = new PhoneNumber();
3197+
parseHelper(numberToParse, options.getDefaultRegion(), options.hasKeepRawInput(), options.hasDefaultRegion(), phoneNumber);
3198+
return phoneNumber;
3199+
}
3200+
3201+
public void parseWithOptions(CharSequence numberToParse, ParsingOptions options, PhoneNumber phoneNumber)
3202+
throws NumberParseException {
3203+
parseHelper(numberToParse, options.getDefaultRegion(), options.hasKeepRawInput(), options.hasDefaultRegion(), phoneNumber);
3204+
}
3205+
31873206
/**
31883207
* Returns an iterable over all {@link PhoneNumberMatch PhoneNumberMatches} in {@code text}. This
31893208
* is a shortcut for {@link #findNumbers(CharSequence, String, Leniency, long)

0 commit comments

Comments
 (0)