Class Endpoint

  • All Implemented Interfaces:
    Comparable<Endpoint>

    public final class Endpoint
    extends Object
    implements Comparable<Endpoint>
    A remote endpoint that refers to a single host or a group of multiple hosts.

    A host endpoint has host(), optional ipAddr() and optional port(). It can be represented as "<host>" or "<host>:<port>" in the authority part of a URI. It can have an IP address if the host name has been resolved and thus there's no need to query a DNS server.

    A group endpoint has groupName() and it can be represented as "group:<groupName>" in the authority part of a URI. It can be resolved into a host endpoint with resolve(ClientRequestContext).

    • Method Detail

      • parse

        public static Endpoint parse​(String authority)
        Parse the authority part of a URI. The authority part may have one of the following formats:
        • "group:<groupName>" for a group endpoint
        • "<host>:<port>" for a host endpoint
        • "<host>" for a host endpoint with no port number specified
        An IPv4 or IPv6 address can be specified in lieu of a host name, e.g. "127.0.0.1:8080" and "[::1]:8080".
      • isGroup

        public boolean isGroup()
        Returns true if this endpoint refers to a group.
      • groupName

        public String groupName()
        Returns the group name of this endpoint.
        Throws:
        IllegalStateException - if this endpoint is not a group but a host
      • host

        public String host()
        Returns the host name of this endpoint.
        Throws:
        IllegalStateException - if this endpoint is not a host but a group
      • ipAddr

        @Nullable
        public String ipAddr()
        Returns the IP address of this endpoint.
        Returns:
        the IP address, or null if the host name is not resolved yet
        Throws:
        IllegalStateException - if this endpoint is not a host but a group
      • hasIpAddr

        public boolean hasIpAddr()
        Returns whether this endpoint has an IP address resolved. This method is a shortcut of ipAddr() != null.
        Returns:
        true if and only if this endpoint has an IP address.
        Throws:
        IllegalStateException - if this endpoint is not a host but a group
      • isIpAddrOnly

        public boolean isIpAddrOnly()
        Returns whether this endpoint's host name is an IP address.
        Returns:
        true if and only if this endpoint's host name is an IP address
        Throws:
        IllegalStateException - if this endpoint is not a host but a group
      • port

        public int port()
        Returns the port number of this endpoint.
        Throws:
        IllegalStateException - if this endpoint is not a host but a group, or this endpoint does not have its port specified.
      • port

        public int port​(int defaultValue)
        Returns the port number of this endpoint.
        Parameters:
        defaultValue - the default value to return when this endpoint does not have its port specified
        Throws:
        IllegalStateException - if this endpoint is not a host but a group
      • withDefaultPort

        public Endpoint withDefaultPort​(int defaultPort)
        Returns a new host endpoint with the specified default port number.
        Returns:
        the new endpoint whose port is defaultPort if this endpoint does not have its port specified. this if this endpoint already has its port specified.
        Throws:
        IllegalStateException - if this endpoint is not a host but a group
      • withIpAddr

        public Endpoint withIpAddr​(@Nullable
                                   String ipAddr)
        Returns a new host endpoint with the specified IP address.
        Returns:
        the new endpoint with the specified IP address. this if this endpoint has the same IP address.
        Throws:
        IllegalStateException - if this endpoint is not a host but a group
      • withWeight

        public Endpoint withWeight​(int weight)
        Returns a new host endpoint with the specified weight.
        Returns:
        the new endpoint with the specified weight. this if this endpoint has the same weight.
        Throws:
        IllegalStateException - if this endpoint is not a host but a group
      • weight

        public int weight()
        Returns the weight of this endpoint.
      • authority

        public String authority()
        Converts this endpoint into the authority part of a URI.
        Returns:
        the authority string
      • toUri

        public URI toUri​(String scheme)
        Converts this endpoint into a URI using the scheme.
        Parameters:
        scheme - the scheme for URI.
        Returns:
        the URI
      • toUri

        public URI toUri​(String scheme,
                         @Nullable
                         String path)
        Converts this endpoint into a URI using the scheme and path.
        Parameters:
        scheme - the scheme for URI.
        path - the path for URI.
        Returns:
        the URI
      • toUri

        public URI toUri​(Scheme scheme)
        Converts this endpoint into a URI using the Scheme.
        Parameters:
        scheme - the Scheme for URI.
        Returns:
        the URI
      • toUri

        public URI toUri​(Scheme scheme,
                         @Nullable
                         String path)
        Converts this endpoint into a URI using the Scheme and the path.
        Parameters:
        scheme - the Scheme for URI.
        path - the path for URI.
        Returns:
        the URI
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object