|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.google.i18n.phonenumbers.PhoneNumberUtil
public class PhoneNumberUtil
Utility for international phone numbers. Functionality includes formatting, parsing and validation.
Nested Class Summary | |
---|---|
static class |
PhoneNumberUtil.Leniency
Leniency when finding potential phone numbers in text segments. |
static class |
PhoneNumberUtil.MatchType
Types of phone number matches. |
static class |
PhoneNumberUtil.PhoneNumberFormat
INTERNATIONAL and NATIONAL formats are consistent with the definition in ITU-T Recommendation E. |
static class |
PhoneNumberUtil.PhoneNumberType
Type of phone numbers. |
static class |
PhoneNumberUtil.ValidationResult
Possible outcomes when testing if a PhoneNumber is possible. |
Method Summary | |
---|---|
static String |
convertAlphaCharactersInNumber(String number)
Converts all alpha characters in a number to their respective digits on a keypad, but retains existing formatting. |
Iterable<PhoneNumberMatch> |
findNumbers(CharSequence text,
String defaultCountry)
Returns an iterable over all PhoneNumberMatches in text . |
Iterable<PhoneNumberMatch> |
findNumbers(CharSequence text,
String defaultCountry,
PhoneNumberUtil.Leniency leniency,
long maxTries)
Returns an iterable over all PhoneNumberMatches in text . |
String |
format(Phonenumber.PhoneNumber number,
PhoneNumberUtil.PhoneNumberFormat numberFormat)
Formats a phone number in the specified format using default rules. |
void |
format(Phonenumber.PhoneNumber number,
PhoneNumberUtil.PhoneNumberFormat numberFormat,
StringBuffer formattedNumber)
|
String |
formatByPattern(Phonenumber.PhoneNumber number,
PhoneNumberUtil.PhoneNumberFormat numberFormat,
List<Phonemetadata.NumberFormat> userDefinedFormats)
Formats a phone number in the specified format using client-defined formatting rules. |
String |
formatInOriginalFormat(Phonenumber.PhoneNumber number,
String countryCallingFrom)
Formats a phone number using the original phone number format that the number is parsed from. |
String |
formatNationalNumberWithCarrierCode(Phonenumber.PhoneNumber number,
String carrierCode)
Formats a phone number in national format for dialing using the carrier as specified in the carrierCode. |
String |
formatNationalNumberWithPreferredCarrierCode(Phonenumber.PhoneNumber number,
String fallbackCarrierCode)
Formats a phone number in national format for dialing using the carrier as specified in the preferred_domestic_carrier_code field of the PhoneNumber object passed in. |
String |
formatOutOfCountryCallingNumber(Phonenumber.PhoneNumber number,
String countryCallingFrom)
Formats a phone number for out-of-country dialing purpose. |
AsYouTypeFormatter |
getAsYouTypeFormatter(String regionCode)
Gets an AsYouTypeFormatter for the specific country. |
int |
getCountryCodeForRegion(String regionCode)
Returns the country calling code for a specific region. |
Phonenumber.PhoneNumber |
getExampleNumber(String regionCode)
Gets a valid number for the specified country. |
Phonenumber.PhoneNumber |
getExampleNumberForType(String regionCode,
PhoneNumberUtil.PhoneNumberType type)
Gets a valid number for the specified country and number type. |
static PhoneNumberUtil |
getInstance()
Gets a PhoneNumberUtil instance to carry out international phone number formatting, parsing, or validation. |
int |
getLengthOfGeographicalAreaCode(Phonenumber.PhoneNumber number)
Gets the length of the geographical area code from the national_number field of the PhoneNumber object passed in, so that clients could use it to split a national significant number into geographical area code and subscriber number. |
int |
getLengthOfNationalDestinationCode(Phonenumber.PhoneNumber number)
Gets the length of the national destination code (NDC) from the PhoneNumber object passed in, so that clients could use it to split a national significant number into NDC and subscriber number. |
static String |
getNationalSignificantNumber(Phonenumber.PhoneNumber number)
Gets the national significant number of the a phone number. |
String |
getNddPrefixForRegion(String regionCode,
boolean stripNonDigits)
Returns the national dialling prefix for a specific region. |
PhoneNumberUtil.PhoneNumberType |
getNumberType(Phonenumber.PhoneNumber number)
Gets the type of a phone number. |
String |
getRegionCodeForCountryCode(int countryCode)
Returns the region code that matches the specific country code. |
String |
getRegionCodeForNumber(Phonenumber.PhoneNumber number)
Returns the country/region where a phone number is from. |
Set<String> |
getSupportedCountries()
Convenience method to enable tests to get a list of what countries the library has metadata for. |
static boolean |
isLeadingZeroCountry(int countryCode)
Check whether countryCode represents the country calling code from a country whose national significant number could contain a leading zero. |
boolean |
isNANPACountry(String regionCode)
Check if a country is one of the countries under the North American Numbering Plan Administration (NANPA). |
PhoneNumberUtil.MatchType |
isNumberMatch(Phonenumber.PhoneNumber firstNumberIn,
Phonenumber.PhoneNumber secondNumberIn)
Takes two phone numbers and compares them for equality. |
PhoneNumberUtil.MatchType |
isNumberMatch(Phonenumber.PhoneNumber firstNumber,
String secondNumber)
Takes two phone numbers and compares them for equality. |
PhoneNumberUtil.MatchType |
isNumberMatch(String firstNumber,
String secondNumber)
Takes two phone numbers as strings and compares them for equality. |
boolean |
isPossibleNumber(Phonenumber.PhoneNumber number)
Convenience wrapper around isPossibleNumberWithReason. |
boolean |
isPossibleNumber(String number,
String countryDialingFrom)
Check whether a phone number is a possible number given a number in the form of a string, and the country where the number could be dialed from. |
PhoneNumberUtil.ValidationResult |
isPossibleNumberWithReason(Phonenumber.PhoneNumber number)
Check whether a phone number is a possible number. |
boolean |
isValidNumber(Phonenumber.PhoneNumber number)
Tests whether a phone number matches a valid pattern. |
boolean |
isValidNumberForRegion(Phonenumber.PhoneNumber number,
String regionCode)
Tests whether a phone number is valid for a certain region. |
static String |
normalizeDigitsOnly(String number)
Normalizes a string of characters representing a phone number. |
Phonenumber.PhoneNumber |
parse(String numberToParse,
String defaultCountry)
Parses a string and returns it in proto buffer format. |
void |
parse(String numberToParse,
String defaultCountry,
Phonenumber.PhoneNumber phoneNumber)
|
Phonenumber.PhoneNumber |
parseAndKeepRawInput(String numberToParse,
String defaultCountry)
Parses a string and returns it in proto buffer format. |
void |
parseAndKeepRawInput(String numberToParse,
String defaultCountry,
Phonenumber.PhoneNumber phoneNumber)
|
boolean |
truncateTooLongNumber(Phonenumber.PhoneNumber number)
Attempts to extract a valid number from a phone number that is too long to be valid, and resets the PhoneNumber object passed in to that valid version. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static String normalizeDigitsOnly(String number)
number
- a string of characters representing a phone number
public static String convertAlphaCharactersInNumber(String number)
public int getLengthOfGeographicalAreaCode(Phonenumber.PhoneNumber number)
number
- the PhoneNumber object for which clients want to know the length of the area
code.
public int getLengthOfNationalDestinationCode(Phonenumber.PhoneNumber number)
number
- the PhoneNumber object for which clients want to know the length of the NDC.
public Set<String> getSupportedCountries()
public static PhoneNumberUtil getInstance()
public String format(Phonenumber.PhoneNumber number, PhoneNumberUtil.PhoneNumberFormat numberFormat)
number
- the phone number to be formattednumberFormat
- the format the phone number should be formatted into
public void format(Phonenumber.PhoneNumber number, PhoneNumberUtil.PhoneNumberFormat numberFormat, StringBuffer formattedNumber)
public String formatByPattern(Phonenumber.PhoneNumber number, PhoneNumberUtil.PhoneNumberFormat numberFormat, List<Phonemetadata.NumberFormat> userDefinedFormats)
number
- the phone number to be formattednumberFormat
- the format the phone number should be formatted intouserDefinedFormats
- formatting rules specified by clients
public String formatNationalNumberWithCarrierCode(Phonenumber.PhoneNumber number, String carrierCode)
number
- the phone number to be formattedcarrierCode
- the carrier selection code to be used
public String formatNationalNumberWithPreferredCarrierCode(Phonenumber.PhoneNumber number, String fallbackCarrierCode)
number
- the phone number to be formattedfallbackCarrierCode
- the carrier selection code to be used, if none is found in the
phone number itself
public String formatOutOfCountryCallingNumber(Phonenumber.PhoneNumber number, String countryCallingFrom)
number
- the phone number to be formattedcountryCallingFrom
- the ISO 3166-1 two-letter country code that denotes
the foreign country where the call is being placed
public String formatInOriginalFormat(Phonenumber.PhoneNumber number, String countryCallingFrom)
number
- the PhoneNumber that needs to be formatted in its original number formatcountryCallingFrom
- the country whose IDD needs to be prefixed if the original number
has one
public static String getNationalSignificantNumber(Phonenumber.PhoneNumber number)
number
- the PhoneNumber object for which the national significant number is needed
public Phonenumber.PhoneNumber getExampleNumber(String regionCode)
regionCode
- the ISO 3166-1 two-letter country code that denotes
the country for which an example number is needed
public Phonenumber.PhoneNumber getExampleNumberForType(String regionCode, PhoneNumberUtil.PhoneNumberType type)
regionCode
- the ISO 3166-1 two-letter country code that denotes
the country for which an example number is neededtype
- the type of number that is needed
public PhoneNumberUtil.PhoneNumberType getNumberType(Phonenumber.PhoneNumber number)
number
- the phone number that we want to know the type
public boolean isValidNumber(Phonenumber.PhoneNumber number)
number
- the phone number that we want to validate
public boolean isValidNumberForRegion(Phonenumber.PhoneNumber number, String regionCode)
number
- the phone number that we want to validateregionCode
- the ISO 3166-1 two-letter country code that denotes
the region/country that we want to validate the phone number for
public String getRegionCodeForNumber(Phonenumber.PhoneNumber number)
number
- the phone number whose origin we want to know
public String getRegionCodeForCountryCode(int countryCode)
public int getCountryCodeForRegion(String regionCode)
regionCode
- the ISO 3166-1 two-letter country code that denotes
the country/region that we want to get the country code for
public String getNddPrefixForRegion(String regionCode, boolean stripNonDigits)
regionCode
- the ISO 3166-1 two-letter country code that denotes
the country/region that we want to get the dialling prefix forstripNonDigits
- true to strip non-digits from the national dialling prefix
public boolean isNANPACountry(String regionCode)
public static boolean isLeadingZeroCountry(int countryCode)
public boolean isPossibleNumber(Phonenumber.PhoneNumber number)
number
- the number that needs to be checked
public PhoneNumberUtil.ValidationResult isPossibleNumberWithReason(Phonenumber.PhoneNumber number)
number
- the number that needs to be checked
public boolean isPossibleNumber(String number, String countryDialingFrom)
number
- the number that needs to be checked, in the form of a stringcountryDialingFrom
- the ISO 3166-1 two-letter country code that denotes
the country that we are expecting the number to be dialed from.
Note this is different from the country where the number belongs. For example, the number
+1 650 253 0000 is a number that belongs to US. When written in this form, it could be
dialed from any country. When it is written as 00 1 650 253 0000, it could be dialed from
any country which uses an international dialling prefix of 00. When it is written as 650
253 0000, it could only be dialed from within the US, and when written as 253 0000, it
could only be dialed from within a smaller area in the US (Mountain View, CA, to be more
specific).
public boolean truncateTooLongNumber(Phonenumber.PhoneNumber number)
number
- a PhoneNumber object which contains a number that is too long to be valid.
public AsYouTypeFormatter getAsYouTypeFormatter(String regionCode)
regionCode
- the ISO 3166-1 two-letter country code that denotes the
country/region where the phone number is being entered
public Phonenumber.PhoneNumber parse(String numberToParse, String defaultCountry) throws NumberParseException
numberToParse
- number that we are attempting to parse. This can contain formatting
such as +, ( and -, as well as a phone number extension.defaultCountry
- the ISO 3166-1 two-letter country code that denotes the
country that we are expecting the number to be from. This is only used
if the number being parsed is not written in international format.
The country code for the number in this case would be stored as that
of the default country supplied. If the number is guaranteed to
start with a '+' followed by the country code, then "ZZ" or
null can be supplied.
NumberParseException
- if the string is not considered to be a viable phone number or if
no default country was supplied and the number is not in
international format (does not start with +)public void parse(String numberToParse, String defaultCountry, Phonenumber.PhoneNumber phoneNumber) throws NumberParseException
NumberParseException
public Phonenumber.PhoneNumber parseAndKeepRawInput(String numberToParse, String defaultCountry) throws NumberParseException
numberToParse
- number that we are attempting to parse. This can contain formatting
such as +, ( and -, as well as a phone number extension.defaultCountry
- the ISO 3166-1 two-letter country code that denotes the country that
we are expecting the number to be from. This is only used if the
number being parsed is not written in international format. The
country code for the number in this case would be stored as that of
the default country supplied.
NumberParseException
- if the string is not considered to be a viable phone number or if
no default country was suppliedpublic void parseAndKeepRawInput(String numberToParse, String defaultCountry, Phonenumber.PhoneNumber phoneNumber) throws NumberParseException
NumberParseException
public Iterable<PhoneNumberMatch> findNumbers(CharSequence text, String defaultCountry)
PhoneNumberMatches
in text
. This
is a shortcut for getMatcher(text, defaultCountry, Leniency.VALID, Long.MAX_VALUE)
.
text
- the text to search for phone numbers, null for no textdefaultCountry
- the ISO 3166-1 two-letter country code that denotes the country that
we are expecting the number to be from. This is only used if the
number being parsed is not written in international format. The
country code for the number in this case would be stored as that of
the default country supplied. May be null if only international
numbers are expected.public Iterable<PhoneNumberMatch> findNumbers(CharSequence text, String defaultCountry, PhoneNumberUtil.Leniency leniency, long maxTries)
PhoneNumberMatches
in text
.
text
- the text to search for phone numbers, null for no textdefaultCountry
- the ISO 3166-1 two-letter country code that denotes the country that
we are expecting the number to be from. This is only used if the
number being parsed is not written in international format. The
country code for the number in this case would be stored as that of
the default country supplied. May be null if only international
numbers are expected.leniency
- the leniency to use when evaluating candidate phone numbersmaxTries
- the maximum number of invalid numbers to try before giving up on the
text. This is to cover degenerate cases where the text has a lot of
false positives in it. Must be >= 0
.public PhoneNumberUtil.MatchType isNumberMatch(Phonenumber.PhoneNumber firstNumberIn, Phonenumber.PhoneNumber secondNumberIn)
firstNumberIn
- first number to comparesecondNumberIn
- second number to compare
public PhoneNumberUtil.MatchType isNumberMatch(String firstNumber, String secondNumber)
firstNumber
- first number to compare. Can contain formatting, and can have country code
specified with + at the start.secondNumber
- second number to compare. Can contain formatting, and can have country
code specified with + at the start.
public PhoneNumberUtil.MatchType isNumberMatch(Phonenumber.PhoneNumber firstNumber, String secondNumber)
firstNumber
- first number to compare in proto buffer format.secondNumber
- second number to compare. Can contain formatting, and can have country
code specified with + at the start.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |