Class CIDR

  • All Implemented Interfaces:
    java.lang.Comparable<CIDR>
    Direct Known Subclasses:
    CIDR4, CIDR6

    public abstract class CIDR
    extends java.lang.Object
    implements java.lang.Comparable<CIDR>
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.net.InetAddress baseAddress
      The base address of the CIDR notation
      protected int cidrMask
      The mask used in the CIDR notation
    • Constructor Summary

      Constructors 
      Constructor Description
      CIDR()  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.net.InetAddress addressStringToInet​(java.lang.String addr)
      Convert an IPv4 or IPv6 textual representation into an InetAddress.
      abstract boolean contains​(java.net.InetAddress inetAddress)
      Compares the given InetAddress against the CIDR and returns true if the ip is in the subnet-ip-range and false if not.
      boolean equals​(java.lang.Object o)  
      java.net.InetAddress getBaseAddress()  
      abstract java.net.InetAddress getEndAddress()  
      static byte[] getIpV4FromIpV6​(java.net.Inet6Address address)
      Compute a byte representation of IpV4 from a IpV6
      static byte[] getIpV6FromIpV4​(java.net.Inet4Address address)
      Compute a byte representation of IpV6 from a IpV4
      int getMask()  
      private static int getNetMask​(java.lang.String netMask)
      Get the Subnet's Netmask in Decimal format.
      i.e.: getNetMask("255.255.255.0") returns the integer CIDR mask
      int hashCode()  
      static CIDR newCIDR​(java.lang.String cidr)
      Create CIDR using the CIDR or normal Notation
      i.e.: CIDR subnet = newCIDR ("10.10.10.0/24"); or CIDR subnet = newCIDR ("1fff:0:0a88:85a3:0:0:ac1f:8001/24"); or CIDR subnet = newCIDR ("10.10.10.0/255.255.255.0");
      static CIDR newCIDR​(java.net.InetAddress baseAddress, int cidrMask)
      Create CIDR using the CIDR Notation
      static CIDR newCIDR​(java.net.InetAddress baseAddress, java.lang.String scidrMask)
      Create CIDR using the normal Notation
      private static int parseInt​(java.lang.String intstr, int def)  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Comparable

        compareTo
    • Field Detail

      • baseAddress

        protected java.net.InetAddress baseAddress
        The base address of the CIDR notation
      • cidrMask

        protected int cidrMask
        The mask used in the CIDR notation
    • Constructor Detail

      • CIDR

        public CIDR()
    • Method Detail

      • newCIDR

        public static CIDR newCIDR​(java.net.InetAddress baseAddress,
                                   int cidrMask)
                            throws java.net.UnknownHostException
        Create CIDR using the CIDR Notation
        Returns:
        the generated CIDR
        Throws:
        java.net.UnknownHostException
      • newCIDR

        public static CIDR newCIDR​(java.net.InetAddress baseAddress,
                                   java.lang.String scidrMask)
                            throws java.net.UnknownHostException
        Create CIDR using the normal Notation
        Returns:
        the generated CIDR
        Throws:
        java.net.UnknownHostException
      • newCIDR

        public static CIDR newCIDR​(java.lang.String cidr)
                            throws java.net.UnknownHostException
        Create CIDR using the CIDR or normal Notation
        i.e.: CIDR subnet = newCIDR ("10.10.10.0/24"); or CIDR subnet = newCIDR ("1fff:0:0a88:85a3:0:0:ac1f:8001/24"); or CIDR subnet = newCIDR ("10.10.10.0/255.255.255.0");
        Returns:
        the generated CIDR
        Throws:
        java.net.UnknownHostException
      • getBaseAddress

        public java.net.InetAddress getBaseAddress()
        Returns:
        the baseAddress of the CIDR block.
      • getMask

        public int getMask()
        Returns:
        the Mask length.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
        Returns:
        the textual CIDR notation.
      • getEndAddress

        public abstract java.net.InetAddress getEndAddress()
        Returns:
        the end address of this block.
      • contains

        public abstract boolean contains​(java.net.InetAddress inetAddress)
        Compares the given InetAddress against the CIDR and returns true if the ip is in the subnet-ip-range and false if not.
        Returns:
        returns true if the given IP address is inside the currently set network.
      • equals

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

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

        private static java.net.InetAddress addressStringToInet​(java.lang.String addr)
                                                         throws java.net.UnknownHostException
        Convert an IPv4 or IPv6 textual representation into an InetAddress.
        Returns:
        the created InetAddress
        Throws:
        java.net.UnknownHostException
      • getNetMask

        private static int getNetMask​(java.lang.String netMask)
        Get the Subnet's Netmask in Decimal format.
        i.e.: getNetMask("255.255.255.0") returns the integer CIDR mask
        Parameters:
        netMask - a network mask
        Returns:
        the integer CIDR mask
      • parseInt

        private static int parseInt​(java.lang.String intstr,
                                    int def)
        Parameters:
        intstr - a string containing an integer.
        def - the default if the string does not contain a valid integer.
        Returns:
        the inetAddress from the integer
      • getIpV4FromIpV6

        public static byte[] getIpV4FromIpV6​(java.net.Inet6Address address)
        Compute a byte representation of IpV4 from a IpV6
        Returns:
        the byte representation
        Throws:
        java.lang.IllegalArgumentException - if the IpV6 cannot be mapped to IpV4
      • getIpV6FromIpV4

        public static byte[] getIpV6FromIpV4​(java.net.Inet4Address address)
        Compute a byte representation of IpV6 from a IpV4
        Returns:
        the byte representation
        Throws:
        java.lang.IllegalArgumentException - if the IpV6 cannot be mapped to IpV4