Record Class HostAndPort

java.lang.Object
java.lang.Record
eu.cloudnetservice.driver.network.HostAndPort

public record HostAndPort(@NonNull String host, int port) extends Record
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 Details

    • host

      @NonNull private final @NonNull String host
      The field for the host record component.
    • port

      private final int port
      The field for the port record component.
    • NO_PORT

      public static final int NO_PORT
      See Also:
  • Constructor Details

    • HostAndPort

      public HostAndPort(@NonNull @NonNull String host, int port)
      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

      @NonNull public @NonNull String toString()
      Specified by:
      toString in class Record
    • 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.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      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 with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • host

      @NonNull public @NonNull String host()
      Returns the value of the host record component.
      Returns:
      the value of the host record component
    • port

      public int port()
      Returns the value of the port record component.
      Returns:
      the value of the port record component