public final class Subnet extends CacheableDataType implements Comparable<Subnet>
Defined by a (starting) subnet address and its associated subnet mask (for both IPv4 and IPv6).
All constructors for this class are private. Creating instances
of Subnet
is done via the static methods on the class.
Instances of this class are immutable, making them inherently thread-safe.
This class overrides equals(java.lang.Object)
and hashCode()
so that instances
play nicely with the Collection classes.
This class implements the Comparable
interface to ensure that
a sorted list is presented in an intuitive order.
Modifier and Type | Method and Description |
---|---|
int |
compareTo(Subnet o) |
boolean |
contains(IpAddress ip)
Returns true if the given IP address is contained by this subnet.
|
boolean |
contains(IpRange range)
Returns true if the given IP address range is contained by this subnet.
|
boolean |
equals(Object o) |
IpAddress |
getAddress()
Returns the subnet address.
|
IpAddress |
getBroadcastAddress()
Returns the broadcast address for this subnet.
|
IpRange |
getEquivalentIpRange()
Returns an IP range that represents the same set of IP addresses
that belong to this subnet.
|
IpAddress.Family |
getFamily()
Returns the IP address family of the subnet and mask.
|
SubnetMask |
getMask()
Returns the subnet mask.
|
int |
hashCode() |
String |
toDebugString()
Returns a multi-line string representation of this instance.
|
String |
toString() |
static Subnet |
valueOf(IpAddress address,
SubnetMask mask)
Returns a
Subnet instance that encapsulates the
given subnet address and subnet mask. |
static Subnet |
valueOf(String spec)
Returns a
Subnet instance that encapsulates the subnet address
and subnet mask expressed in the given string specification. |
getRefQ, keyFromBytes
public String toDebugString()
public IpAddress getAddress()
public SubnetMask getMask()
public IpRange getEquivalentIpRange()
public int compareTo(Subnet o)
compareTo
in interface Comparable<Subnet>
public IpAddress.Family getFamily()
public IpAddress getBroadcastAddress()
IpAddress broadcast = Subnet.valueOf("192.168.1.0/24").getBroadcastAddress(); assert(broadcast.equals(IpAddress.valueOf("192.168.1.255");TODO: need to decide how to handle IPv6 subnets
UnsupportedOperationException
- for IPv6 instancespublic boolean contains(IpAddress ip)
ip
- the IP addresspublic boolean contains(IpRange range)
range
- the IP address rangeIllegalArgumentException
- if other range is not the same
IP family as this oneNullPointerException
- if other is nullpublic static Subnet valueOf(IpAddress address, SubnetMask mask)
Subnet
instance that encapsulates the
given subnet address and subnet mask.address
- the subnet addressmask
- the subnet maskNullPointerException
- if either address or mask is nullIllegalArgumentException
- if mask is not appropriate for the
given subnet addresspublic static Subnet valueOf(String spec)
Subnet
instance that encapsulates the subnet address
and subnet mask expressed in the given string specification. If a slash
is present in the string, it is assumed that CIDR notation is being
used. If no slash is present in the string, it is assumed that the
address is a class-A, class-B, or class-C (IPv4) address where the
subnet mask is implied.
Classless Inter-Domain Routing (CIDR) is a way of expressing one (or many) subnets and their associated subnet mask.
The expected form of the string is
{subnet-address}/{n}where
subnet-address
is the start subnet address
and n
is the number of (leftmost) '1' bits in the mask.
For example:
192.168.12.0/23applies the network mask 255.255.254.0 to the 192.168 network, starting at 192.168.12.0. This notation represents the address range 192.168.12.0 - 192.168.13.255. Compared to traditional class-based networking, 192.168.12.0/23 represents an aggregation of the two Class C subnets 192.168.12.0 and 192.168.13.0 each having a subnet mask of 255.255.255.0.
Examples of implied subnet masks:
Subnet.valueOf("15.0.0.0") --> address 15.0.0.0, mask 255.0.0.0 Subnet.valueOf("180.0.0.0") --> address 180.0.0.0, mask 255.255.0.0 Subnet.valueOf("200.0.0.0") --> address 200.0.0.0, mask 255.255.255.0
spec
- the string specificationNullPointerException
- if the parameter is nullIllegalArgumentException
- if the parameter is malformedCopyright © 2015. All Rights Reserved.