Package com.globalmentor.net
Class Host
- All Implemented Interfaces:
Named<String>
,Comparable<Named<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 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
Ifobject
is anotherHost
, compares the hostname and port.static String
Retrieves a hostname from a string containing a host and optional port.int
getPort()
static int
Retrieves a port from a string containing a host and optional port.int
hashCode()
toString()
Methods inherited from class com.globalmentor.model.DefaultComparableNamed
compareTo
Methods inherited from class com.globalmentor.model.DefaultNamed
getName
-
Field Details
-
LOCALHOST
Shared representation oflocalhost
. -
EXAMPLE_COM
Shared representation of an example host,example.com
.
-
-
Constructor Details
-
Host
Constructs a host from a string.- Parameters:
host
- A host and optional port in the formhostname[:port]
.- Throws:
IllegalArgumentException
- if the given host string is not syntactically correct.
-
Host
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
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
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
- Overrides:
toString
in classDefaultNamed<String>
- Returns:
- A string representation of the host in the form
hostname[:port]
.
-
hashCode
public int hashCode()- Overrides:
hashCode
in classDefaultNamed<String>
- Returns:
- A hash code for this object.
-
equals
Ifobject
is anotherHost
, compares the hostname and port.- Overrides:
equals
in classDefaultNamed<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:
-