danger.telephony
Class PhoneNumber

java.lang.Object
  extended by danger.telephony.PhoneNumber


public class PhoneNumber
extends Object

A standard class for storing, formatting, and comparing phone numbers.


Constructor Summary
PhoneNumber(String number)
          Create a PhoneNumber object.
 
Method Summary
 boolean equals(Object o)
          Return true if canonical numbers are equal.
static String formatPhoneNumber(String number)
          Format a phone number based on locale.
 String getCanonicalNumber()
          Return the canonical form of this phone number.
 String getFormattedNumber()
          Return phone number formatted for display.
static boolean hasAreaCode(String number)
          Test whether the passed number has an area code.
 int hashCode()
          Return hash based on canonical number.
static boolean isEmergencyNumber(String number)
          Returns true if the given number is considered an emergency number.
 boolean matches(PhoneNumber number)
          Compare two phone numbers.
 void setNumber(String number)
          Change the phone number in this object.
 String toString()
          Return the formatted phone number.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

PhoneNumber

public PhoneNumber(String number)
Create a PhoneNumber object. A phone number object stores the raw, formatted, and canonical version of the phone number. Formatting and stripping of the number occurs when the object is created. This makes creating a phone number object more expensive than accessing one. There is intentionally no way to change the number in a phone number object. If you want to do that, you need to create a new object.

Parameters:
number - Original phone number to be stored.
Method Detail

setNumber

public void setNumber(String number)
Change the phone number in this object.

Parameters:
number - New number for this object. Null-ok

getFormattedNumber

public String getFormattedNumber()
Return phone number formatted for display. If the phone number was set to be null this method will return an empty string.

Returns:
String containing phone number formatted for display in UI.

getCanonicalNumber

public String getCanonicalNumber()
Return the canonical form of this phone number. A canonical number consists of only digits. If the phone number was set to null, this method will return null.

Returns:
Canonical form of this phone number. Could be null.

matches

public boolean matches(PhoneNumber number)
Compare two phone numbers. This method is called matches because it does a fuzzy compare of the numbers. For example, if a number does not contain an area code, it will match a number that does contain an area code.

Parameters:
number - to be compared.
Returns:
true if the number phone numbers match.

formatPhoneNumber

public static String formatPhoneNumber(String number)
Format a phone number based on locale. Formatting includes hyphenation as well as stripping any leading "1-" or "+1". Also includes prepending the local area code for US and CA locales.

Parameters:
number - Phone number to be formatted.
Returns:
String containing the formatted phone number.

hasAreaCode

public static boolean hasAreaCode(String number)
Test whether the passed number has an area code. Note this method is not international friendly. It returns true for any number that has more than 7 digits.

Parameters:
number - Phone number string to be tested (with or without formatting).
Returns:
true if the stripped number is greater than 7 digits long.

isEmergencyNumber

public static boolean isEmergencyNumber(String number)
Returns true if the given number is considered an emergency number.

Parameters:
number - string to determine as being valid emergency number
Returns:
true if given string is considered an emergency number, false otherwise
Since:
4.5

toString

public String toString()
Return the formatted phone number.

Overrides:
toString in class Object
Returns:
String containing the formatted version of the phone number.

equals

public boolean equals(Object o)
Return true if canonical numbers are equal. This method only returns true if the canonical numbers are identical. If you want to match on partial numbers, use matches(danger.telephony.PhoneNumber).

Overrides:
equals in class Object
Parameters:
o - PhoneNumber object to compare to.
Returns:
true if canonical numbers are identical

hashCode

public int hashCode()
Return hash based on canonical number.

Overrides:
hashCode in class Object
Returns:
int representing hash for this phone number.