forked from AY1920S1-CS2103T-T09-4/main
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Email.java
67 lines (57 loc) · 2.52 KB
/
Email.java
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
package seedu.address.model.customer;
import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;
/**
* Represents a Customer's email in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidEmail(String)}
*/
public class Email {
private static final String SPECIAL_CHARACTERS = "!#$%&'*+/=?`{|}~^.-";
public static final String MESSAGE_CONSTRAINTS = "Emails should be of the format local-part@domain "
+ "and adhere to the following constraints:\n"
+ "1. The local-part should only contain alphanumeric characters and these special characters, excluding "
+ "the parentheses, (" + SPECIAL_CHARACTERS + ") .\n"
+ "2. This is followed by a '@' and then a domain name. "
+ "The domain name must:\n"
+ " - be at least 2 characters long\n"
+ " - start and end with alphanumeric characters\n"
+ " - consist of alphanumeric characters, a period or a hyphen for the characters in between, if any.";
// alphanumeric and special characters
private static final String LOCAL_PART_REGEX = "^[\\w" + SPECIAL_CHARACTERS + "]+";
private static final String DOMAIN_FIRST_CHARACTER_REGEX = "[^\\W_]"; // alphanumeric characters except underscore
private static final String DOMAIN_MIDDLE_REGEX = "[a-zA-Z0-9.-]*"; // alphanumeric, period and hyphen
private static final String DOMAIN_LAST_CHARACTER_REGEX = "[^\\W_]$";
public static final String VALIDATION_REGEX = LOCAL_PART_REGEX + "@"
+ DOMAIN_FIRST_CHARACTER_REGEX + DOMAIN_MIDDLE_REGEX + DOMAIN_LAST_CHARACTER_REGEX;
public final String value;
/**
* Constructs an {@code Email}.
*
* @param email A valid email address.
*/
public Email(String email) {
requireNonNull(email);
checkArgument(isValidEmail(email), MESSAGE_CONSTRAINTS);
value = email;
}
/**
* Returns if a given string is a valid email.
*/
public static boolean isValidEmail(String test) {
return test.matches(VALIDATION_REGEX);
}
@Override
public String toString() {
return value;
}
@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof Email // instanceof handles nulls
&& value.equals(((Email) other).value)); // state check
}
@Override
public int hashCode() {
return value.hashCode();
}
}