Class SimpleHostAndPort

java.lang.Object
org.kiwiproject.net.SimpleHostAndPort

public class SimpleHostAndPort extends Object
Inspired by Guava's HostAndPort but much simpler in implementation. (Just go look at the code in HostAndPort.fromString(String) if you don't believe me.) Because it is much simpler, it also only handles a very specific host/port format, which is host:port.

It also does not attempt to validate anything about the host or port, e.g. it will happily accept a negative port value or a one-character long host name.

  • Method Details

    • from

      public static SimpleHostAndPort from(String hostPortString, String defaultHost, int defaultPort)
      Parse hostPortString assuming format host:port; if it is blank, use the specified defaultHost and defaultPort values to create a link SimpleHostAndPort.

      No validation is performed on the default values.

      Parameters:
      hostPortString - a string containing host and port, e.g. acme.com:8001
      defaultHost - a default host if the host/port string is blank
      defaultPort - a default port if the host/port string is blank
      Returns:
      a new SimpleHostAndPort instance
    • from

      public static SimpleHostAndPort from(String hostPortString)
      Parse hostPortString assuming format host:port
      Parameters:
      hostPortString - a string containing host and port, e.g. foo.com:9000
      Returns:
      a new SimpleHostAndPort instance
      Throws:
      IllegalStateException - if not in the expected format
      IllegalArgumentException - if hostPortString is blank or port is not a valid number
      Implementation Note:
      Does no validation on the host part
    • toString

      public String toString()
      Return a string in host:port format.
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • canEqual

      protected boolean canEqual(Object other)
    • hashCode

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

      public String getHost()
    • getPort

      public int getPort()