HostAndPort implements Serializable { public static HostAndPort fromHost(String host) { HostAndPort parsedHost = fromString(host); if (parsedHost.hasPort()) { throw new IllegalArgumentException(String.format("Host has a port: %s", host)); } return parsedHost; } private  HostAndPort(String host, int port, boolean hasBracketlessColons); String getHost(); boolean hasPort(); int getPort(); int getPortOrDefault(int defaultPort); static HostAndPort fromParts(String host, int port); static HostAndPort fromHost(String host); static HostAndPort fromString(String hostPortString); HostAndPort withDefaultPort(int defaultPort); HostAndPort requireBracketsForIPv6(); @Override boolean equals(Object other); @Override int hashCode(); @Override String toString();  }
@Test public void testFromHost() { HostAndPort hp = HostAndPort.fromHost("gmail.com"); assertEquals("gmail.com", hp.getHost()); assertFalse(hp.hasPort()); hp = HostAndPort.fromHost("[::1]"); assertEquals("::1", hp.getHost()); assertFalse(hp.hasPort()); try { HostAndPort.fromHost("gmail.com:80"); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } try { HostAndPort.fromHost("[gmail.com]"); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } }