Class Host

  • All Implemented Interfaces:
    Named<java.lang.String>, java.lang.Comparable<Named<java.lang.String>>

    public class Host
    extends DefaultComparableNamed<java.lang.String>
    Encapsulates a host and an optional port.

    The host name without a port can be returned by a call to DefaultNamed.getName().

    Author:
    Garret Wilson
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static Host EXAMPLE_COM
      Shared representation of an example host, example.com.
      static Host LOCALHOST
      Shared representation of localhost.
    • Constructor Summary

      Constructors 
      Constructor Description
      Host​(java.lang.String host)
      Constructs a host from a string.
      Host​(java.lang.String name, int port)
      Constructs a host from a name and optional port.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object object)
      If object is another Host, compares the hostname and port.
      static java.lang.String getName​(java.lang.String host)
      Retrieves a hostname from a string containing a host and optional port.
      int getPort()  
      static int getPort​(java.lang.String host)
      Retrieves a port from a string containing a host and optional port.
      int hashCode()  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • LOCALHOST

        public static final Host LOCALHOST
        Shared representation of localhost.
      • EXAMPLE_COM

        public static final Host EXAMPLE_COM
        Shared representation of an example host, example.com.
    • Constructor Detail

      • Host

        public Host​(java.lang.String host)
             throws java.lang.IllegalArgumentException
        Constructs a host from a string.
        Parameters:
        host - A host and optional port in the form hostname[:port].
        Throws:
        java.lang.IllegalArgumentException - if the given host string is not syntactically correct.
      • Host

        public Host​(java.lang.String name,
                    int port)
        Constructs a host from a name and optional port.
        Parameters:
        name - A hostname.
        port - The port of the host, or -1 if no port should be specified.
    • Method Detail

      • getPort

        public int getPort()
        Returns:
        The port of the host, or -1 if no port is specified.
      • getName

        public static java.lang.String getName​(java.lang.String host)
        Retrieves a hostname from a string containing a host and optional port.
        Parameters:
        host - The host string to parse.
        Returns:
        The name of the host.
      • getPort

        public static int getPort​(java.lang.String host)
                           throws java.lang.NumberFormatException
        Retrieves a port from a string containing a host and optional port.
        Parameters:
        host - The host string to parse.
        Returns:
        The port of the host, or -1 if no port is specified.
        Throws:
        java.lang.IllegalArgumentException - if the given port is not a valid integer.
        java.lang.NumberFormatException
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class DefaultNamed<java.lang.String>
        Returns:
        A string representation of the host in the form hostname[:port].
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class DefaultNamed<java.lang.String>
        Returns:
        A hash code for this object.
      • equals

        public boolean equals​(java.lang.Object object)
        If object is another Host, compares the hostname and port.
        Overrides:
        equals in class DefaultNamed<java.lang.String>
        Parameters:
        object - The object with which to compare this host; should be another Host.
        Returns:
        true if this host equals a host specified in object.
        See Also:
        DefaultNamed.getName(), getPort()