-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhoneNumber.java
More file actions
212 lines (168 loc) · 7.43 KB
/
PhoneNumber.java
File metadata and controls
212 lines (168 loc) · 7.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package javaxt.exchange;
//******************************************************************************
//** PhoneNumber Class
//******************************************************************************
/**
* Used to represent a phone number.
*
******************************************************************************/
public class PhoneNumber {
private String number;
private String type;
/** Static variable to support the getPhoneNumber method. */
private final static String[] numbers = new String[]{"0","1","2","3","4","5","6","7","8","9"};
private final static String[] types = new String[]{
//Personal Phone
"HomePhone","MobilePhone","HomeFax","HomePhone2",
//Work Phone
"BusinessPhone","BusinessPhone2","BusinessFax",
"CompanyMainPhone","AssistantPhone",
//Primary/Callback
"PrimaryPhone","Callback",
//Other
"OtherFax","OtherTelephone",
//Legacy
"CarPhone","Pager","Isdn","RadioPhone","Telex","TtyTddPhone"
};
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of PhoneNumber. Throws an exception if the number
* is invalid.
* @param phoneNumber The phone number (e.g. 555-555-5555)
* @param type Type of phone number. Options include:
* <ul>
* <li>AssistantPhone</li><li>BusinessFax</li><li>BusinessPhone</li>
* <li>BusinessPhone2</li><li>Callback</li><li>CarPhone</li>
* <li>CompanyMainPhone</li><li>HomeFax</li><li>HomePhone</li>
* <li>HomePhone2</li><li>Isdn</li><li>MobilePhone</li><li>OtherFax</li>
* <li>OtherTelephone</li><li>Pager</li><li>PrimaryPhone</li>
* <li>RadioPhone</li><li>Telex</li><li>TtyTddPhone</li>
* </ul>
*/
public PhoneNumber(String phoneNumber, String type) throws ExchangeException {
this.number = getNumber(phoneNumber);
this.type = getType(type);
if (number==null) throw new ExchangeException("Invalid Phone Number: " + phoneNumber);
}
protected PhoneNumber(org.w3c.dom.Node childNode) throws ExchangeException {
if (childNode.getNodeType()==1){
String childNodeName = childNode.getNodeName();
if (childNodeName.contains(":")) childNodeName = childNodeName.substring(childNodeName.indexOf(":")+1);
if (childNodeName.equalsIgnoreCase("Entry")){
this.number = getNumber(javaxt.xml.DOM.getNodeValue(childNode));
this.type = javaxt.xml.DOM.getAttributeValue(childNode, "Key");
}
}
if (number==null) {
throw new ExchangeException("Invalid Phone Number: "
+ javaxt.xml.DOM.getNodeValue(childNode));
}
}
public String getNumber(){
return number;
}
//**************************************************************************
//** getType
//**************************************************************************
/** Returns the type of phone number. Options include:
* <ul>
* <li>AssistantPhone</li><li>BusinessFax</li><li>BusinessPhone</li>
* <li>BusinessPhone2</li><li>Callback</li><li>CarPhone</li>
* <li>CompanyMainPhone</li><li>HomeFax</li><li>HomePhone</li>
* <li>HomePhone2</li><li>Isdn</li><li>MobilePhone</li><li>OtherFax</li>
* <li>OtherTelephone</li><li>Pager</li><li>PrimaryPhone</li>
* <li>RadioPhone</li><li>Telex</li><li>TtyTddPhone</li>
* </ul>
*/
public String getType(){
this.hashCode();
return type;
}
//**************************************************************************
//** getTypes
//**************************************************************************
/** Returns a static list of phone number types:
* <ul>
* <li>AssistantPhone</li><li>BusinessFax</li><li>BusinessPhone</li>
* <li>BusinessPhone2</li><li>Callback</li><li>CarPhone</li>
* <li>CompanyMainPhone</li><li>HomeFax</li><li>HomePhone</li>
* <li>HomePhone2</li><li>Isdn</li><li>MobilePhone</li><li>OtherFax</li>
* <li>OtherTelephone</li><li>Pager</li><li>PrimaryPhone</li>
* <li>RadioPhone</li><li>Telex</li><li>TtyTddPhone</li>
* </ul>
*/
public static String[] getTypes(){
return types;
}
public String toString(){
return type + ": " + number;
}
public int hashCode(){
return toString().hashCode();
}
public boolean equals(Object obj){
if (obj instanceof PhoneNumber){
if (obj!=null) return obj.toString().equalsIgnoreCase(this.toString());
}
return false;
}
//**************************************************************************
//** getNumber
//**************************************************************************
/** Used to normalize a phone number into a common format. For example, if
* you pass in "(555) 555-5555" or "555.555.5555" or "5555555555", the
* method will return "555-555-5555". Returns null if there are less than
* 10 digits found in the phone number. Removes "1" country code prefix.
* Note that this method is designed to work with US phone numbers.
* International phone numbers require a different solution.
*/
private String getNumber(String phoneNumber){
if (phoneNumber==null) return null;
StringBuffer str = new StringBuffer();
for (int i=0; i<phoneNumber.length(); i++){
for (String number : numbers){
if (phoneNumber.substring(i, i+1).equals(number)){
str.append(number);
break;
}
}
}
if (str.length()<10) return null;
else{
String prefix = "";
if (str.length()>10){
prefix = str.substring(0, str.length()-10);
if (prefix.equals("1")) prefix = "";
else prefix += "-";
}
return prefix + str.substring(str.length()-10, str.length()-7) +
"-" + str.substring(str.length()-7, str.length()-4) + "-" +
str.substring(str.length()-4);
}
}
//**************************************************************************
//** getType
//**************************************************************************
/** Returns a phone number type that most closely matches the given string.
*/
private String getType(String type){
type = type.toUpperCase().replace(" ", "");
for (String t : types){
if (t.toUpperCase().equals(type)) return t;
}
if (type.contains("HOME")) return "HomePhone";
if (type.contains("MOBILE")) return "MobilePhone";
if (type.equals("FAX") || type.equals("COMPANYFAX") || type.equals("WORKFAX")) return "BusinessFax";
if (type.equals("PERSONALFAX")) return "HomeFax";
if (type.contains("FAX")) return "OtherFax";
if (type.contains("BUSINESS") || type.contains("WORK") || type.contains("OFFICE")) return "BusinessPhone";
if (type.contains("ASSISTANT")) return "AssistantPhone";
if (type.contains("PRIMARY")) return "PrimaryPhone";
if (type.contains("CALLBACK")) return "Callback";
if (type.contains("CAR")) return "CarPhone";
if (type.contains("RADIO")) return "RadioPhone";
if (type.contains("TTY") || type.contains("TDD")) return "TtyTddPhone";
return "OtherTelephone";
}
}