Package com.globalmentor.net
Class EmailAddress
- java.lang.Object
-
- com.globalmentor.net.EmailAddress
-
- All Implemented Interfaces:
Resource
,java.lang.Comparable<EmailAddress>
public final class EmailAddress extends java.lang.Object implements Resource, java.lang.Comparable<EmailAddress>
Value class for email addresses represented in the form specified by RFC 5322: Internet Message Format.- Author:
- Garret Wilson
- See Also:
- RFC 5322
-
-
Field Summary
Fields Modifier and Type Field Description static Characters
ATEXT_CHARACTERS
Email addressatext
characters as per RFC 5322.static char
DOMAIN_LITERAL_BEGIN
The beginning delimiter of adomain-literal
.static char
DOMAIN_LITERAL_END
The ending delimiter of adomain-literal
.static java.util.regex.Pattern
DOMAIN_PATTERN
A regular expression pattern for matching the domain of an email addresses according to RFC 5322.static Characters
DTEXT_CHARACTERS
Email addressdtext
characters as per RFC 5322.static java.util.regex.Pattern
EMAIL_ADDRESS_PATTERN
A regular expression pattern for matching email addresses according to RFC 5322.static int
EMAIL_ADDRESS_PATTERN_DOMAIN_GROUP
The group for returning the domain of an email address from a matcher derived fromEMAIL_ADDRESS_PATTERN
.static int
EMAIL_ADDRESS_PATTERN_LOCAL_PART_GROUP
The group for returning the local part of an email address from a matcher derived fromEMAIL_ADDRESS_PATTERN
.static char
LOCAL_PART_DOMAIN_DELIMITER
The delimiter separating the local part from the domain.static java.util.regex.Pattern
LOCAL_PART_PATTERN
A regular expression pattern for matching the local part of an email addresses according to RFC 5322.-
Fields inherited from interface com.globalmentor.net.Resource
URI_PROPERTY_NAME
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
compareTo(EmailAddress emailAddress)
Compares this object with the specified object for order.boolean
equals(java.lang.Object object)
Determines if this object is equivalent to another object.static EmailAddress
fromString(java.lang.String input)
Constructs an email address from a string.java.lang.String
getDomain()
static java.lang.String
getDomain(java.lang.CharSequence input)
Returns the domain of an email address from a string.java.lang.String
getLocalPart()
static java.lang.String
getLocalPart(java.lang.CharSequence input)
Returns the local part of an email address from a string.java.net.URI
getURI()
int
hashCode()
static EmailAddress
of(java.lang.String localPart, java.lang.String domain)
Constructs an email address from its separate components.java.lang.String
toString()
Constructs a string representation of the email address in its RFC 5322 format.
-
-
-
Field Detail
-
ATEXT_CHARACTERS
public static final Characters ATEXT_CHARACTERS
Email addressatext
characters as per RFC 5322.Printable US-ASCII characters not including specials. Used for atoms.
-
DTEXT_CHARACTERS
public static final Characters DTEXT_CHARACTERS
Email addressdtext
characters as per RFC 5322. This definition does not include the obsoleteobs-dtext
from RFC 5322.Printable US-ASCII characters not including "[", "]", or "\".
-
LOCAL_PART_DOMAIN_DELIMITER
public static final char LOCAL_PART_DOMAIN_DELIMITER
The delimiter separating the local part from the domain.- See Also:
- Constant Field Values
-
DOMAIN_LITERAL_BEGIN
public static final char DOMAIN_LITERAL_BEGIN
The beginning delimiter of adomain-literal
.- See Also:
- Constant Field Values
-
DOMAIN_LITERAL_END
public static final char DOMAIN_LITERAL_END
The ending delimiter of adomain-literal
.- See Also:
- Constant Field Values
-
LOCAL_PART_PATTERN
public static final java.util.regex.Pattern LOCAL_PART_PATTERN
A regular expression pattern for matching the local part of an email addresses according to RFC 5322. This pattern is derived from the regular expression provided at an answer to Using a regular expression to validate an email address.
-
DOMAIN_PATTERN
public static final java.util.regex.Pattern DOMAIN_PATTERN
A regular expression pattern for matching the domain of an email addresses according to RFC 5322. This pattern is derived from the regular expression provided at an answer to Using a regular expression to validate an email address.
-
EMAIL_ADDRESS_PATTERN
public static final java.util.regex.Pattern EMAIL_ADDRESS_PATTERN
A regular expression pattern for matching email addresses according to RFC 5322. This pattern is derived from the regular expression provided at an answer to Using a regular expression to validate an email address.
-
EMAIL_ADDRESS_PATTERN_LOCAL_PART_GROUP
public static final int EMAIL_ADDRESS_PATTERN_LOCAL_PART_GROUP
The group for returning the local part of an email address from a matcher derived fromEMAIL_ADDRESS_PATTERN
.- See Also:
- Constant Field Values
-
EMAIL_ADDRESS_PATTERN_DOMAIN_GROUP
public static final int EMAIL_ADDRESS_PATTERN_DOMAIN_GROUP
The group for returning the domain of an email address from a matcher derived fromEMAIL_ADDRESS_PATTERN
.- See Also:
- Constant Field Values
-
-
Method Detail
-
getLocalPart
public java.lang.String getLocalPart()
- Returns:
- The local part of the email address.
-
getDomain
public java.lang.String getDomain()
- Returns:
- The domain of the email address.
-
of
public static EmailAddress of(java.lang.String localPart, java.lang.String domain) throws ArgumentSyntaxException
Constructs an email address from its separate components.- Parameters:
localPart
- The local part of the email address.domain
- The domain of the email address.- Returns:
- An email address with the given local part and domain.
- Throws:
java.lang.NullPointerException
- if the given local part and/or domain isnull
.ArgumentSyntaxException
- if the given local part and/or domain violates RFC 5322.
-
fromString
public static EmailAddress fromString(java.lang.String input) throws ArgumentSyntaxException
Constructs an email address from a string.- Parameters:
input
- The string to be parsed as an email address.- Returns:
- An email address from the given string representation.
- Throws:
java.lang.NullPointerException
- if the given character sequence isnull
.ArgumentSyntaxException
- if the input string violates RFC 5322.
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- A hash code representing this object.
-
equals
public boolean equals(java.lang.Object object)
Determines if this object is equivalent to another object. This method considers another object equivalent if it is another email address with the same local part and domain.- Overrides:
equals
in classjava.lang.Object
- Returns:
true
if the given object is an equivalent email address.
-
compareTo
public int compareTo(EmailAddress emailAddress)
Compares this object with the specified object for order. This implementation primarily by domain and secondarily by local part, ignoring case and locales.- Specified by:
compareTo
in interfacejava.lang.Comparable<EmailAddress>
- Parameters:
emailAddress
- The object to be compared.- Returns:
- A negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
- See Also:
getDomain()
,getLocalPart()
-
toString
public java.lang.String toString()
Constructs a string representation of the email address in its RFC 5322 format. This implementation returns the canonical version of the email address.- Overrides:
toString
in classjava.lang.Object
- Returns:
- A string representation of the email address.
-
getURI
public java.net.URI getURI()
-
getLocalPart
public static java.lang.String getLocalPart(java.lang.CharSequence input) throws ArgumentSyntaxException
Returns the local part of an email address from a string.- Parameters:
input
- The character sequence to be parsed as an email address.- Returns:
- The local part of the given email address.
- Throws:
java.lang.NullPointerException
- if the given character sequence isnull
.ArgumentSyntaxException
- if the input string violates RFC 5322.
-
getDomain
public static java.lang.String getDomain(java.lang.CharSequence input) throws ArgumentSyntaxException
Returns the domain of an email address from a string.- Parameters:
input
- The character sequence to be parsed as an email address.- Returns:
- The domain of the given email address.
- Throws:
java.lang.NullPointerException
- if the given character sequence isnull
.ArgumentSyntaxException
- if the input string violates RFC 5322.
-
-