Class Host

All Implemented Interfaces:
Named<String>, Comparable<Named<String>>

public class Host extends DefaultComparableNamed<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 final Host
    Shared representation of an example host, example.com.
    static final Host
    Shared representation of localhost.
  • Constructor Summary

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

    Modifier and Type
    Method
    Description
    boolean
    equals(Object object)
    If object is another Host, compares the hostname and port.
    static String
    Retrieves a hostname from a string containing a host and optional port.
    int
     
    static int
    Retrieves a port from a string containing a host and optional port.
    int
     
     

    Methods inherited from class com.globalmentor.model.DefaultComparableNamed

    compareTo

    Methods inherited from class com.globalmentor.model.DefaultNamed

    getName

    Methods inherited from class java.lang.Object

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

    • 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 Details

    • Host

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

      public Host(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 Details

    • getPort

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

      public static String getName(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(String host) throws 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:
      IllegalArgumentException - if the given port is not a valid integer.
      NumberFormatException
    • toString

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

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

      public boolean equals(Object object)
      If object is another Host, compares the hostname and port.
      Overrides:
      equals in class DefaultNamed<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: