Class KiwiInternetAddresses


  • public class KiwiInternetAddresses
    extends Object
    Utilities for InetAddress and other things related to internet addresses. Note that Google Guava also has the InetAddresses class, so if something is not in here, check there. This set of utilities only contains things that are not in the JDK or Guava.

    Note that some of the methods accept (e.g. as default value) or return KiwiInternetAddresses.SimpleHostInfo. This is because the InetAddress class has no public constructors and can only be instantiated by its static factory methods. As a result, if you want to specify your own custom host name and/or IP address as the default value, for example to use a different host alias or subnet IP, then you must use the methods that work with KiwiInternetAddresses.SimpleHostInfo. Otherwise when working with InetAddress instances, using InetAddress.getLoopbackAddress() is about the only other default value you could use.

    Last, note that KiwiInternetAddresses.SimpleHostInfo only contains the host name and host address as a string.

    • Constructor Detail

      • KiwiInternetAddresses

        public KiwiInternetAddresses()
    • Method Detail

      • getLocalHostInetAddress

        public static Optional<InetAddress> getLocalHostInetAddress()
        Get local host as an optional of InetAddress
        Returns:
        optional containing local host info, or empty if any error occurred
      • getLocalHostInetAddress

        public static InetAddress getLocalHostInetAddress​(InetAddress defaultValue)
        Get local host as a InetAddress.
        Parameters:
        defaultValue - the default value to use if local host address could not be obtained for any reason
        Returns:
        the local host info, or the specified defaultValue if any error occurred
      • getLocalHostInetAddress

        public static InetAddress getLocalHostInetAddress​(Supplier<InetAddress> defaultValueSupplier)
        Get local host as a InetAddress.
        Parameters:
        defaultValueSupplier - supplier for the default value to use if local host address could not be obtained for any reason
        Returns:
        the local host info, or the value obtained from the defaultValueSupplier if any error occurred
      • portFrom

        public static Optional<Integer> portFrom​(String url)
        Returns the port in the given URL, or an empty optional otherwise.
        Parameters:
        url - a URL
        Returns:
        an optional containing the port, or empty
        Throws:
        UncheckedMalformedURLException - if the given string URL is not valid
        See Also:
        URL(String)
      • portFrom

        public static Optional<Integer> portFrom​(URL url)
        Returns the port in the given URL, or an empty optional otherwise.
        Parameters:
        url - a URL
        Returns:
        an optional containing the port, or empty if the URL did not have a port
        See Also:
        URL.getPort()
        Implementation Note:
        The URL.getPort() returns -1 if there is no port. Here we change that to an Optional
      • hostAndPortFrom

        public static com.google.common.net.HostAndPort hostAndPortFrom​(String url)
        Returns a HostAndPort from the given URL.
        Parameters:
        url - a URL as a string
        Returns:
        HostAndPort instance
      • hostAndPortFrom

        public static com.google.common.net.HostAndPort hostAndPortFrom​(URL url)
        Returns a HostAndPort from the given URL.
        Parameters:
        url - a URL
        Returns:
        HostAndPort instance
      • findFirstMatchingAddressOrNull

        public static String findFirstMatchingAddressOrNull​(List<String> subnetCidrs,
                                                            KiwiInternetAddresses.IpScheme ipScheme)
        Finds the first IP address on the machine that matches one of the given subnet CIDRs or returns null if not found. The KiwiInternetAddresses.IpScheme is used to filter the IP addresses by IPv4 or IPv6.
        Parameters:
        subnetCidrs - A list of CIDRs used to match against the machine's IP addresses.
        ipScheme - Whether to filter by IPv4 or IPv6.
        Returns:
        the first found matching IP address or null.
      • findFirstMatchingAddressOrNull

        public static String findFirstMatchingAddressOrNull​(List<String> subnetCidrs,
                                                            List<String> ipAddresses)
        Finds the first IP address from a given list of ip addresses that matches one of the given subnet CIDRs or returns null if not found.
        Parameters:
        subnetCidrs - A list of CIDRs used to match against the machine's IP addresses.
        ipAddresses - A list of IP addresses to search for a match.
        Returns:
        the first found matching IP address or null.
      • findFirstMatchingAddressOrThrow

        public static String findFirstMatchingAddressOrThrow​(List<String> subnetCidrs,
                                                             KiwiInternetAddresses.IpScheme ipScheme)
        Finds the first IP address on the machine that matches one of the given subnet CIDRs or throws IllegalStateException if not found. The KiwiInternetAddresses.IpScheme is used to filter the IP addresses by IPv4 or IPv6.
        Parameters:
        subnetCidrs - A list of CIDRs used to match against the machine's IP addresses.
        ipScheme - Whether to filter by IPv4 or IPv6.
        Returns:
        the first found matching IP address.
        Throws:
        IllegalStateException - if a matching IP address can not be found.
      • findFirstMatchingAddressOrThrow

        public static String findFirstMatchingAddressOrThrow​(List<String> subnetCidrs,
                                                             List<String> ipAddresses)
        Finds the first IP address on the machine that matches one of the given subnet CIDRs or throws IllegalStateException if not found.
        Parameters:
        subnetCidrs - A list of CIDRs used to match against the machine's IP addresses.
        ipAddresses - A list of IP addresses to search for a match.
        Returns:
        the first found matching IP address.
        Throws:
        IllegalStateException - if a matching IP address can not be found.
      • findFirstMatchingAddress

        public static Optional<String> findFirstMatchingAddress​(List<String> subnetCidrs,
                                                                KiwiInternetAddresses.IpScheme ipScheme)
        Attempts to find the first IP address on the machine that matches one of the given subnet CIDRs. The KiwiInternetAddresses.IpScheme is used to filter the IP addresses by IPv4 or IPv6.
        Parameters:
        subnetCidrs - A list of CIDRs used to match against the machine's IP addresses.
        ipScheme - Whether to filter by IPv4 or IPv6
        Returns:
        an Optional containing the first found matching IP address or Optional.empty() if not found.
      • findFirstMatchingAddress

        public static Optional<String> findFirstMatchingAddress​(List<String> subnetCidrs,
                                                                List<String> ipAddresses)
        Attempts to find the first IP address from a given list of ip addresses that matches one of the given subnet CIDRs.
        Parameters:
        subnetCidrs - A list of CIDRs used to match against the machine's IP addresses.
        ipAddresses - A list of IP addresses to search for a match.
        Returns:
        an Optional containing the first found matching IP address or Optional.empty() if not found.
      • findMatchingAddresses

        public static List<String> findMatchingAddresses​(List<String> subnetCidrs,
                                                         KiwiInternetAddresses.IpScheme ipScheme)
        Finds all IP addresses on the machine that matches one of the given subnet CIDRs. The KiwiInternetAddresses.IpScheme is used to filter the IP addresses by IPv4 or IPv6.
        Parameters:
        subnetCidrs - A list of CIDRs used to match against the machine's IP addresses.
        ipScheme - Whether to filter by IPv4 or IPv6
        Returns:
        a list of matching IP addresses.
      • findMatchingAddresses

        public static List<String> findMatchingAddresses​(List<String> subnetCidrs,
                                                         List<String> ipAddresses)
        Finds all IP addresses from a given list of ip addresses that matches one of the given subnet CIDRs.
        Parameters:
        subnetCidrs - A list of CIDRs used to match against the machine's IP addresses.
        ipAddresses - A list of IP addresses to search for a match.
        Returns:
        a list of matching IP addresses.