Package com.globalmentor.net
Class Host
- java.lang.Object
-
- com.globalmentor.model.DefaultNamed<N>
-
- com.globalmentor.model.DefaultComparableNamed<java.lang.String>
-
- com.globalmentor.net.Host
-
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 oflocalhost
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(java.lang.Object object)
Ifobject
is anotherHost
, 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 com.globalmentor.model.DefaultComparableNamed
compareTo
-
Methods inherited from class com.globalmentor.model.DefaultNamed
getName
-
-
-
-
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 formhostname[: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 classDefaultNamed<java.lang.String>
- Returns:
- A string representation of the host in the form
hostname[:port]
.
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classDefaultNamed<java.lang.String>
- Returns:
- A hash code for this object.
-
equals
public boolean equals(java.lang.Object object)
Ifobject
is anotherHost
, compares the hostname and port.- Overrides:
equals
in classDefaultNamed<java.lang.String>
- Parameters:
object
- The object with which to compare this host; should be anotherHost
.- Returns:
true
if this host equals a host specified inobject
.- See Also:
DefaultNamed.getName()
,getPort()
-
-