Class IpPrefix

  • Direct Known Subclasses:
    Ip4Prefix, Ip6Prefix

    public class IpPrefix
    extends Object
    A class representing an IP prefix. A prefix consists of an IP address and a subnet mask. This class is immutable.

    NOTE: The stored IP address in the result IP prefix is masked to contain zeroes in all bits after the prefix length.

    • Field Detail

      • MAX_INET_MASK_LENGTH

        public static final int MAX_INET_MASK_LENGTH
        Longest IPv4 network prefix.
        See Also:
        Constant Field Values
      • MAX_INET6_MASK_LENGTH

        public static final int MAX_INET6_MASK_LENGTH
        Longest IPv6 network prefix.
        See Also:
        Constant Field Values
      • IPV4_MULTICAST_PREFIX

        public static final IpPrefix IPV4_MULTICAST_PREFIX
        An IpPrefix that contains all IPv4 multicast addresses.
      • IPV6_MULTICAST_PREFIX

        public static final IpPrefix IPV6_MULTICAST_PREFIX
        An IpPrefix that contains all IPv6 multicast addresses.
      • IPV4_LINK_LOCAL_PREFIX

        public static final IpPrefix IPV4_LINK_LOCAL_PREFIX
        An IpPrefix that contains all IPv4 link local addresses.
      • IPV6_LINK_LOCAL_PREFIX

        public static final IpPrefix IPV6_LINK_LOCAL_PREFIX
        An IpPrefix that contains all IPv6 link local addresses.
    • Constructor Detail

      • IpPrefix

        protected IpPrefix​(IpAddress address,
                           int prefixLength)
        Constructor for given IP address, and a prefix length.
        Parameters:
        address - the IP address
        prefixLength - the prefix length
        Throws:
        IllegalArgumentException - if the prefix length value is invalid
      • IpPrefix

        protected IpPrefix()
        Default constructor for Kryo serialization.
    • Method Detail

      • version

        public IpAddress.Version version()
        Returns the IP version of the prefix.
        Returns:
        the IP version of the prefix
      • isIp4

        public boolean isIp4()
        Tests whether the IP version of this prefix is IPv4.
        Returns:
        true if the IP version of this prefix is IPv4, otherwise false.
      • isIp6

        public boolean isIp6()
        Tests whether the IP version of this prefix is IPv6.
        Returns:
        true if the IP version of this prefix is IPv6, otherwise false.
      • isMulticast

        public boolean isMulticast()
        Check if this IP prefix is a multicast prefix.
        Returns:
        true if this prefix a multicast prefix
      • address

        public IpAddress address()
        Returns the IP address value of the prefix.
        Returns:
        the IP address value of the prefix
      • prefixLength

        public int prefixLength()
        Returns the IP address prefix length.
        Returns:
        the IP address prefix length
      • getIp4Prefix

        public Ip4Prefix getIp4Prefix()
        Gets the Ip4Prefix view of the IP prefix.
        Returns:
        the Ip4Prefix view of the IP prefix if it is IPv4, otherwise null
      • getIp6Prefix

        public Ip6Prefix getIp6Prefix()
        Gets the Ip6Prefix view of the IP prefix.
        Returns:
        the Ip6Prefix view of the IP prefix if it is IPv6, otherwise null
      • valueOf

        public static IpPrefix valueOf​(int address,
                                       int prefixLength)
        Converts an integer and a prefix length into an IPv4 prefix.
        Parameters:
        address - an integer representing the IPv4 address
        prefixLength - the prefix length
        Returns:
        an IP prefix
        Throws:
        IllegalArgumentException - if the prefix length value is invalid
      • valueOf

        public static IpPrefix valueOf​(IpAddress.Version version,
                                       byte[] address,
                                       int prefixLength)
        Converts a byte array and a prefix length into an IP prefix.
        Parameters:
        version - the IP address version
        address - the IP address value stored in network byte order
        prefixLength - the prefix length
        Returns:
        an IP prefix
        Throws:
        IllegalArgumentException - if the prefix length value is invalid
      • valueOf

        public static IpPrefix valueOf​(IpAddress address,
                                       int prefixLength)
        Converts an IP address and a prefix length into an IP prefix.
        Parameters:
        address - the IP address
        prefixLength - the prefix length
        Returns:
        an IP prefix
        Throws:
        IllegalArgumentException - if the prefix length value is invalid
      • valueOf

        public static IpPrefix valueOf​(String address)
        Converts a CIDR (slash) notation string (e.g., "10.1.0.0/16" or "1111:2222::/64") into an IP prefix.
        Parameters:
        address - an IP prefix in string form (e.g. "10.1.0.0/16" or "1111:2222::/64")
        Returns:
        an IP prefix
        Throws:
        IllegalArgumentException - if the arguments are invalid
      • contains

        public boolean contains​(IpPrefix other)
        Determines whether a given IP prefix is contained within this prefix.
        Parameters:
        other - the IP prefix to test
        Returns:
        true if the other IP prefix is contained in this prefix, otherwise false
      • contains

        public boolean contains​(IpAddress other)
        Determines whether a given IP address is contained within this prefix.
        Parameters:
        other - the IP address to test
        Returns:
        true if the IP address is contained in this prefix, otherwise false
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object