org.dhcp4java
Class InetCidr

java.lang.Object
  extended by org.dhcp4java.InetCidr
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable<InetCidr>

public class InetCidr
extends java.lang.Object
implements java.io.Serializable, java.lang.Comparable<InetCidr>

Author:
yshi7355
See Also:
Serialized Form

Constructor Summary
InetCidr(java.net.InetAddress addr, java.net.InetAddress netMask)
          Constructs a InetCidr provided an ip address and an ip mask.
InetCidr(java.net.InetAddress addr, int mask)
          Constructor for InetCidr.
 
Method Summary
static InetCidr[] addr2Cidr(java.net.InetAddress addr)
          Returns an array of all cidr combinations with the provided ip address.
static void checkNoOverlap(java.util.List<InetCidr> list)
          Checks whether the list does not contain any overlapping cidr(s).
 int compareTo(InetCidr o)
          Compare two InetCidr by its addr as main criterion, mask as second.
 boolean equals(java.lang.Object obj)
           
static InetCidr fromLong(long l)
          Creates a new InetCidr from its long representation.
 java.net.InetAddress getAddr()
           
 long getAddrLong()
           
 int getMask()
           
 int hashCode()
           
static boolean isSorted(java.util.List<InetCidr> list)
          Checks whether a list of InetCidr is strictly sorted (no 2 equal objects).
 long toLong()
          Returns a long representation of Cidr.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

InetCidr

public InetCidr(java.net.InetAddress addr,
                int mask)
Constructor for InetCidr.

Takes a network address (IPv4) and a mask length

Parameters:
addr - IPv4 address
mask - mask lentgh (between 1 and 32)
Throws:
java.lang.NullPointerException - if addr is null
java.lang.IllegalArgumentException - if addr is not IPv4

InetCidr

public InetCidr(java.net.InetAddress addr,
                java.net.InetAddress netMask)
Constructs a InetCidr provided an ip address and an ip mask.

If the mask is not valid, an exception is raised.

Parameters:
addr - the ip address (IPv4)
netMask - the ip mask
Throws:
java.lang.IllegalArgumentException - if addr or netMask is null.
java.lang.IllegalArgumentException - if the netMask is not a valid one.
Method Detail

toString

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

getAddr

public java.net.InetAddress getAddr()
Returns:
Returns the addr.

getAddrLong

public long getAddrLong()
Returns:
Returns the addr as a long.

getMask

public int getMask()
Returns:
Returns the mask.

toLong

public final long toLong()
Returns a long representation of Cidr.

The high 32 bits contain the mask, the low 32 bits the network address.

Returns:
the long representation of the Cidr

fromLong

public static final InetCidr fromLong(long l)
Creates a new InetCidr from its long representation.

Parameters:
l - the Cidr in its "long" format
Returns:
the object
Throws:
java.lang.IllegalArgumentException

hashCode

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

equals

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

addr2Cidr

public static InetCidr[] addr2Cidr(java.net.InetAddress addr)
Returns an array of all cidr combinations with the provided ip address.

The array is ordered from the most specific to the most general mask.

Parameters:
addr -
Returns:
array of all cidr possible with this address

compareTo

public int compareTo(InetCidr o)
Compare two InetCidr by its addr as main criterion, mask as second.

Note: this class has a natural ordering that is inconsistent with equals.

Specified by:
compareTo in interface java.lang.Comparable<InetCidr>
Parameters:
o -
Returns:
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

isSorted

public static boolean isSorted(java.util.List<InetCidr> list)
Checks whether a list of InetCidr is strictly sorted (no 2 equal objects).

Parameters:
list - list of potentially sorted InetCidr
Returns:
true if list is sorted or null
Throws:
java.lang.NullPointerException - if one or more elements of the list are null

checkNoOverlap

public static void checkNoOverlap(java.util.List<InetCidr> list)
Checks whether the list does not contain any overlapping cidr(s).

Pre-requisite: list must be already sorted.

Parameters:
list - sorted list of InetCidr
Throws:
java.lang.NullPointerException - if a list element is null
java.lang.IllegalStateException - if overlapping cidr are detected