Class EmailAddress

java.lang.Object
com.globalmentor.net.EmailAddress
All Implemented Interfaces:
Resource, Comparable<EmailAddress>

public final class EmailAddress extends Object implements Resource, Comparable<EmailAddress>
Value class for email addresses represented in the form specified by RFC 5322: Internet Message Format.
Author:
Garret Wilson
See Also:
  • Field Details

  • Method Details

    • getLocalPart

      public String getLocalPart()
      Returns:
      The local part of the email address.
    • getDomain

      public String getDomain()
      Returns:
      The domain of the email address.
    • of

      public static EmailAddress of(String localPart, 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:
      NullPointerException - if the given local part and/or domain is null.
      ArgumentSyntaxException - if the given local part and/or domain violates RFC 5322.
    • fromString

      public static EmailAddress fromString(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:
      NullPointerException - if the given character sequence is null.
      ArgumentSyntaxException - if the input string violates RFC 5322.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
      Returns:
      A hash code representing this object.
    • equals

      public boolean equals(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 class 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 interface 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:
    • toString

      public 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 class Object
      Returns:
      A string representation of the email address.
    • getURI

      public URI getURI()
      Specified by:
      getURI in interface Resource
      Returns:
      The resource identifier URI, or null if the identifier is not known.
    • getLocalPart

      public static String getLocalPart(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:
      NullPointerException - if the given character sequence is null.
      ArgumentSyntaxException - if the input string violates RFC 5322.
    • getDomain

      public static String getDomain(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:
      NullPointerException - if the given character sequence is null.
      ArgumentSyntaxException - if the input string violates RFC 5322.