Record Class HostAndPort
java.lang.Object
java.lang.Record
eu.cloudnetservice.driver.network.HostAndPort
Represents an immutable host and port mapping. Validation of a host and port is up to the caller. A host of this
class might be an ipv4/ipv6 address, but can also be the path to a unix domain socket.
- Since:
- 4.0
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionHostAndPort(@NonNull String host, int port) Constructs a new host and port instance, validating the port. -
Method Summary
Modifier and TypeMethodDescriptionfinal booleanIndicates whether some other object is "equal to" this one.static @NonNull HostAndPortfromSocketAddress(@NonNull SocketAddress socketAddress) Tries to convert the given socket address into a host and port, throwing an exception if not possible.final inthashCode()Returns a hash code value for this object.host()Returns the value of thehostrecord component.intport()Returns the value of theportrecord component.toString()booleanChecks if this host and port has a port provided.
-
Field Details
-
host
The field for thehostrecord component. -
port
private final int portThe field for theportrecord component. -
NO_PORT
public static final int NO_PORT- See Also:
-
-
Constructor Details
-
HostAndPort
Constructs a new host and port instance, validating the port.- Parameters:
host- the host of the address.port- the port of the address, or -1 if no port is given.- Throws:
NullPointerException- if the given host is null.IllegalArgumentException- if the given port exceeds the port range.
-
-
Method Details
-
fromSocketAddress
@Contract("_ -> new") @NonNull public static @NonNull HostAndPort fromSocketAddress(@NonNull @NonNull SocketAddress socketAddress) Tries to convert the given socket address into a host and port, throwing an exception if not possible.- Parameters:
socketAddress- the socket address to convert.- Returns:
- the created host and port based on the given address.
- Throws:
NullPointerException- if the given socket address is null.IllegalArgumentException- if the given socket address type cannot be converted.
-
validPort
public boolean validPort()Checks if this host and port has a port provided. No port might be provided if the connection comes for example from a unix domain socket.- Returns:
- true if this host and port has a port given, false otherwise.
-
toString
-
hashCode
public final int hashCode()Returns a hash code value for this object. The value is derived from the hash code of each of the record components. -
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared withObjects::equals(Object,Object); primitive components are compared with '=='. -
host
Returns the value of thehostrecord component.- Returns:
- the value of the
hostrecord component
-
port
public int port()Returns the value of theportrecord component.- Returns:
- the value of the
portrecord component
-