Class DomainName

java.lang.Object
com.globalmentor.net.DomainName

public final class DomainName extends Object
Utilities and encapsulation of a domain name as defined in RFC 1035, DOMAIN NAMES - IMPLEMENTATION AND SPECIFICATION.
Author:
Garret Wilson
Implementation Note:
The current implementation makes no validations of the domain name syntax other than a few simple rules related to delimiter location and number.
See Also:
  • Field Details

    • DELIMITER

      public static final char DELIMITER
      The dot . delimiter for domain name segments.
      See Also:
    • EMPTY

      public static final DomainName EMPTY
      An empty relative domain name.
    • ROOT

      public static final DomainName ROOT
      The root domain name as per RFC 1035
  • Method Details

    • of

      public static DomainName of(@Nonnull String name)
      Returns a domain name from a string form of the name.
      Parameters:
      name - The string form of the domain name; empty names are allowed.
      Returns:
      A domain name from the string.
      Throws:
      IllegalArgumentException - if a domain name other than the root begins with a dot . character, or has two subsequent dot characters.
      See Also:
    • isAbsolute

      public boolean isAbsolute()
      Determines if the domain name is absolute; that is, it ends with a delimiter '.' character.
      API Note:
      An absolute domain name is also referred to as a Fully Qualified Domain Name (FQDN).
      Returns:
      true if the domain name is absolute.
      See Also:
    • checkArgumentAbsolute

      public DomainName checkArgumentAbsolute() throws IllegalArgumentException
      Checks to see if the domain name is absolute. If the domain name is not absolute, an exception is thrown.
      Returns:
      This domain name.
      Throws:
      IllegalArgumentException - if the domain name is not absolute.
      See Also:
    • isEmpty

      public boolean isEmpty()
      Determines if this is an empty domain name; that is, one consisting of only an empty string "".
      API Note:
      An empty domain name is a relative domain name.
      Returns:
      true if the domain name is empty.
      See Also:
    • isRoot

      public boolean isRoot()
      Determines if this the root domain name; that is, one consisting of only the '.' delimiter.
      API Note:
      The root domain is an absolute domain.
      Returns:
      true if the domain name is the root domain.
      See Also:
    • isRelative

      public boolean isRelative()
      Determines if this domain name is relative; that is, it does not end with a delimiter '.' character.
      Implementation Specification:
      This implementation delegates to isAbsolute().
      Returns:
      true if the domain name is relative.
      See Also:
    • checkArgumentRelative

      public DomainName checkArgumentRelative() throws IllegalArgumentException
      Checks to see if this domain name is relative. If the domain name is not relative, an exception is thrown.
      Returns:
      This domain name.
      Throws:
      IllegalArgumentException - if the domain name is not relative.
      See Also:
    • relativize

      public DomainName relativize(@Nonnull DomainName domainName)
      Relativizes the other domain against this name. If the other domain end with the identical segments of this domain name, the given domain name is returned. A domain name relativized against itself returns an empty domain name.
      Parameters:
      domainName - The domain name to relativize against this one.
      Returns:
      A domain name that, when resolved against this domain, will result in the given domain.
      See Also:
    • resolve

      public DomainName resolve(@Nonnull DomainName domainName)
      Resolves another domain name against this domain name. If the other domain name is absolute, the other domain name is returned. If the other domain name is empty, this domain name is returned. If the other domain is not empty but this domain is empty, the other domain name is returned. Otherwise, the other domain is prepended to this domain name using the DELIMITER delimiter, without regard to whether this
      Parameters:
      domainName - The domain name to resolve against this one
      Returns:
      The other domain name resolved against this one.
      See Also:
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object object)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
      Implementation Specification:
      This implementation returns the string form of the domain name.
    • findGreatestCommonBase

      public static Optional<DomainName> findGreatestCommonBase(@Nonnull Iterable<DomainName> domainNames)
      Determines the domain name with the longest sequence of common base domains. For example if any combination of the domain names www.example.com., test.example.com., and example.com. are passed, the common base domain name example.com. is returned.

      This method requires that all domain names be relative or all be absolute.

      API Note:
      This method does not make any checks to determine whether a segment is empty, e.g. "foo..bar".
      Parameters:
      domainNames - The domain names to check.
      Returns:
      The greatest base domain name, which may not be present if there is no longest base.
      Throws:
      NullPointerException - if the iterable is null or contains a null value.
      IllegalArgumentException - if both relative and absolute domains names are passed.