-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailAddress.java
More file actions
82 lines (67 loc) · 2.8 KB
/
EmailAddress.java
File metadata and controls
82 lines (67 loc) · 2.8 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
package javaxt.exchange;
import java.util.regex.Pattern;
//******************************************************************************
//** EmailAddress Class
//******************************************************************************
/**
* Used to represent an email address.
*
******************************************************************************/
public class EmailAddress {
/** Email validation from mkyong:
* http://www.mkyong.com/regular-expressions/how-to-validate-email-address-with-regular-expression/
*/
private static final Pattern pattern = Pattern.compile(
//"^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"
"^[_A-Za-z0-9-+]+(\\.[_A-Za-z0-9-+]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$"
);
private String emailAddress;
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of EmailAddress. */
public EmailAddress(String email) throws ExchangeException {
String emailAddress = email;
if (emailAddress!=null){
emailAddress = emailAddress.toLowerCase().trim();
if (!pattern.matcher(emailAddress).matches()) emailAddress = null;
}
if (emailAddress==null){
throw new ExchangeException("Invalid Email Address: " + email); //<--DO NOT CHANGE THIS ERROR MESSAGE! Check code for usage...
}
this.emailAddress = emailAddress;
}
//**************************************************************************
//** toString
//**************************************************************************
/** Returns the email address used to instantiate this class. Note that the
* email address is trimmed and converted to lower case in the constructor.
*/
public String toString(){
return emailAddress;
}
public int hashCode(){
return emailAddress.hashCode();
}
//**************************************************************************
//** equals
//**************************************************************************
/** Performs case insensitive string comparison between two email addresses.
* @param obj String or EmailAddress
*/
public boolean equals(Object obj){
if (obj!=null){
if (obj instanceof EmailAddress){
return obj.toString().equalsIgnoreCase(emailAddress);
}
else if (obj instanceof String){
try{
return new EmailAddress((String) obj).toString().equalsIgnoreCase(emailAddress);
}
catch(Exception e){
}
}
}
return false;
}
}