Class CidrAddress

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<CidrAddress>

    public final class CidrAddress
    extends java.lang.Object
    implements java.io.Serializable, java.lang.Comparable<CidrAddress>
    A Classless Inter-Domain Routing address. This is the combination of an IP address and a netmask.
    Author:
    David M. Lloyd
    See Also:
    Serialized Form
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compareAddressBytesTo​(byte[] otherBytes, int otherNetmaskBits, int scopeId)  
      int compareTo​(CidrAddress other)  
      static CidrAddress create​(byte[] addressBytes, int netmaskBits)
      Create a new CIDR address.
      static CidrAddress create​(java.net.InetAddress networkAddress, int netmaskBits)
      Create a new CIDR address.
      boolean equals​(java.lang.Object obj)  
      boolean equals​(CidrAddress obj)  
      java.net.Inet4Address getBroadcastAddress()
      Get the broadcast address for this CIDR block.
      int getNetmaskBits()
      Get the netmask bits.
      java.net.InetAddress getNetworkAddress()
      Get the network address.
      int getScopeId()
      Get the match address scope ID (if it is an IPv6 address).
      int hashCode()  
      boolean matches​(byte[] bytes)
      Determine if this CIDR address matches the given address bytes.
      boolean matches​(byte[] bytes, int scopeId)
      Determine if this CIDR address matches the given address bytes.
      boolean matches​(java.net.Inet4Address address)
      Determine if this CIDR address matches the given address.
      boolean matches​(java.net.Inet6Address address)
      Determine if this CIDR address matches the given address.
      boolean matches​(java.net.InetAddress address)
      Determine if this CIDR address matches the given address.
      boolean matches​(CidrAddress address)
      Determine if this CIDR address matches the given CIDR address.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • INET4_ANY_CIDR

        public static final CidrAddress INET4_ANY_CIDR
        The CIDR address representing all IPv4 addresses.
      • INET6_ANY_CIDR

        public static final CidrAddress INET6_ANY_CIDR
        The CIDR address representing all IPv6 addresses.
    • Method Detail

      • create

        public static CidrAddress create​(java.net.InetAddress networkAddress,
                                         int netmaskBits)
        Create a new CIDR address.
        Parameters:
        networkAddress - the network address (must not be null)
        netmaskBits - the netmask bits (0-32 for IPv4, or 0-128 for IPv6)
        Returns:
        the CIDR address (not null)
      • create

        public static CidrAddress create​(byte[] addressBytes,
                                         int netmaskBits)
        Create a new CIDR address.
        Parameters:
        addressBytes - the network address bytes (must not be null, must be 4 bytes for IPv4 or 16 bytes for IPv6)
        netmaskBits - the netmask bits (0-32 for IPv4, or 0-128 for IPv6)
        Returns:
        the CIDR address (not null)
      • matches

        public boolean matches​(java.net.InetAddress address)
        Determine if this CIDR address matches the given address.
        Parameters:
        address - the address to test
        Returns:
        true if the address matches, false otherwise
      • matches

        public boolean matches​(byte[] bytes)
        Determine if this CIDR address matches the given address bytes.
        Parameters:
        bytes - the address bytes to test
        Returns:
        true if the address bytes match, false otherwise
      • matches

        public boolean matches​(byte[] bytes,
                               int scopeId)
        Determine if this CIDR address matches the given address bytes.
        Parameters:
        bytes - the address bytes to test
        scopeId - the scope ID, or 0 to match no scope
        Returns:
        true if the address bytes match, false otherwise
      • matches

        public boolean matches​(java.net.Inet4Address address)
        Determine if this CIDR address matches the given address.
        Parameters:
        address - the address to test
        Returns:
        true if the address matches, false otherwise
      • matches

        public boolean matches​(java.net.Inet6Address address)
        Determine if this CIDR address matches the given address.
        Parameters:
        address - the address to test
        Returns:
        true if the address matches, false otherwise
      • matches

        public boolean matches​(CidrAddress address)
        Determine if this CIDR address matches the given CIDR address. This will be true only when the given CIDR block is wholly enclosed by this one.
        Parameters:
        address - the address to test
        Returns:
        true if the given block is enclosed by this one, false otherwise
      • getNetworkAddress

        public java.net.InetAddress getNetworkAddress()
        Get the network address. The returned address has a resolved name consisting of the most compact valid string representation of the network of this CIDR address.
        Returns:
        the network address (not null)
      • getBroadcastAddress

        public java.net.Inet4Address getBroadcastAddress()
        Get the broadcast address for this CIDR block. If the block has no broadcast address (either because it is IPv6 or it is too small) then null is returned.
        Returns:
        the broadcast address for this CIDR block, or null if there is none
      • getNetmaskBits

        public int getNetmaskBits()
        Get the netmask bits. This will be in the range 0-32 for IPv4 addresses, and 0-128 for IPv6 addresses.
        Returns:
        the netmask bits
      • getScopeId

        public int getScopeId()
        Get the match address scope ID (if it is an IPv6 address).
        Returns:
        the scope ID, or 0 if there is none or the address is an IPv4 address
      • compareTo

        public int compareTo​(CidrAddress other)
        Specified by:
        compareTo in interface java.lang.Comparable<CidrAddress>
      • compareAddressBytesTo

        public int compareAddressBytesTo​(byte[] otherBytes,
                                         int otherNetmaskBits,
                                         int scopeId)
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object