public final class IpRange extends CacheableDataType implements Comparable<IpRange>
valueOf(com.hp.util.ip.Subnet)
factory method.
(However, see also Subnet.getEquivalentIpRange()
)
A string specification is used to define the IP address range. This can be one of two forms, declaring either a range of IPv4 addresses or a range of IPv6 addresses.
For IPv4 addresses the form is:
{byteSpec}.{byteSpec}.{byteSpec}.{byteSpec}where byteSpec represents a single byte in the address, and is of the form:
{n} | {n}-{m} | *where n and m are values in the range 0 to 255, m (when specified) is > n, and * stands for 0-255
For example, the specification "15.29.36-37.1-255"
declares that
the range of IP addresses starts with 15.29.36.1
through
15.29.36.255
, then continues with 15.29.37.1
through
15.29.37.255
.
For IPv6 addresses the form is:
{2Bytes}:{2Bytes}:{2Bytes}:{2Bytes}:{2Bytes}:{2Bytes}:{2Bytes}:{2Bytes}where 2Bytes represents two bytes in the address and is of the form:
{nnnn} | {nnnn}-{mmmm} | *where nnnn and mmmm are hex values in the range 0000 to FFFF, mmmm (when specified) is > nnnn, and * stands for 0000-FFFF.
Leading zeros may be dropped, and "double-colon" shorthand notation may
be used. For example, the specification "FF:FAB::0000-0003:*"
declares that the range of IP addresses starts with
00FF:0FAB:0000:0000:0000:0000:0000:0000and ends with
00FF:0FAB:0000:0000:0000:0000:0003:FFFF
Modifier and Type | Field and Description |
---|---|
protected ByteArrayGenerator |
bag |
protected static String |
COLON_STAR |
protected static String |
COMMA |
protected static String |
DOT |
protected static BigInteger |
MAX_INT |
protected static BigInteger |
MAX_LONG |
protected String |
normalizedSpec |
protected String |
spec |
Modifier and Type | Method and Description |
---|---|
int |
compareTo(IpRange o) |
boolean |
contains(com.hp.util.ip.AbstractByteRange<T> other)
Returns
true if this range is a superset of
the other range; false otherwise. |
boolean |
contains(IpAddress ipAddress)
A predicate that returns
true if this range contains
the given item; false otherwise. |
static List<IpRange> |
createRanges(String commaSeparatedSpecs)
Returns a list of IP ranges that embody the given range specifications.
|
boolean |
equals(Object o) |
IpAddress |
first()
Returns the first item in the range.
|
protected ByteArrayGenerator |
getBag(String spec)
Subclasses must return a byte array generator appropriate for the
given range specification.
|
Subnet |
getEquivalentSubnet()
Returns an instance of
Subnet equivalent to this IP address
range, if appropriate; null otherwise. |
IpAddress.Family |
getFamily()
Returns the IP address family of this range.
|
int |
hashCode() |
boolean |
intersects(com.hp.util.ip.AbstractByteRange<T> other)
Returns
true if this range intersects
the other range; false otherwise. |
Iterator<IpAddress> |
iterator()
Returns an iterator for the range.
|
IpAddress |
last()
Returns the last item in the range.
|
IpAddress |
random()
Returns a randomly generated item that falls within this range.
|
static String |
rangeListToString(List<IpRange> ranges)
Converts a list of IP address ranges to a comma separated string
representation.
|
BigInteger |
size()
Returns the size of the range.
|
int |
sizeAsInt()
Returns the size of the range as an integer value, unless the value
would be greater than
Integer.MAX_VALUE , in which case this
method returns -1. |
long |
sizeAsLong()
Returns the size of the range as a long value, unless the value
would be greater than
Long.MAX_VALUE , in which case this
method returns -1L. |
List<IpRange> |
splitRange(IpAddress ipAddress)
Splits the current IpRange in to two by removing the given
IpAddress from the current IpRange.
|
String |
toDebugString()
Returns a multi-line string representing the internal state of
this range.
|
String |
toString() |
static IpRange |
valueOf(String spec)
Returns an IP range for the given specification.
|
getRefQ, keyFromBytes
protected static final BigInteger MAX_LONG
protected static final BigInteger MAX_INT
protected static final String DOT
protected static final String COMMA
protected static final String COLON_STAR
protected final String spec
protected final String normalizedSpec
protected final transient ByteArrayGenerator bag
protected ByteArrayGenerator getBag(String spec)
spec
- the range specificationpublic boolean contains(IpAddress ipAddress)
true
if this range contains
the given item; false otherwise.ipAddress
- the itempublic IpAddress first()
public IpAddress last()
public IpAddress random()
public Iterator<IpAddress> iterator()
first()
and ending
with the last()
, emitting a total count of size()
items.public int compareTo(IpRange o)
compareTo
in interface Comparable<IpRange>
public IpAddress.Family getFamily()
public Subnet getEquivalentSubnet()
Subnet
equivalent to this IP address
range, if appropriate; null otherwise.
So, what is meant by "if appropriate"? Well, if the first
IP address in this range represents the subnet address with
all zero bits in the host portion of the address, and the
last
IP address in this range represents the subnet
address with all one bits in the host portion of the address,
then the equivalent subnet can be computed and returned. For example:
IpRange range = IpRange.valueOf("15.23.12-13.*"); Subnet subnet = range.getEquivalentSubnet(); assert(subnet.equals(Subnet.valueOf("15.23.12.0/23"))); assert(subnet.getAddress().equals(IpAddress.valueOf("15.23.12.0"); assert(subnet.getMask().equals(SubnetMask.valueOf("255.255.254.0"); --- IpRange range = IpRange.valueOf("15.23.47.1-100"); Subnet subnet = range.getEquivalentSubnet(); assert(subnet == null);
public static IpRange valueOf(String spec)
spec
- the IP address range specificationpublic static List<IpRange> createRanges(String commaSeparatedSpecs)
commaSeparatedSpecs
- the comma separated list of range specspublic static String rangeListToString(List<IpRange> ranges)
createRanges(String)
.ranges
- the list of rangespublic List<IpRange> splitRange(IpAddress ipAddress)
ipAddress
- the IP that needs to be removed from current IpRangepublic String toDebugString()
public boolean contains(com.hp.util.ip.AbstractByteRange<T> other)
true
if this range is a superset of
the other range; false otherwise.
Put another way, the set of all items T_other
produced
by other.iterator()
is a subset of the set of all
items T_this
produced by this.iterator()
.other
- the other range to compare withIllegalArgumentException
- if other range is not the same type
as this oneNullPointerException
- if other is nullpublic boolean intersects(com.hp.util.ip.AbstractByteRange<T> other)
true
if this range intersects
the other range; false otherwise.
Put another way, the set of all items T_other
produced
by other.iterator()
contains at least one item from the set
of all items T_this
produced by this.iterator()
.other
- the other range to compare withIllegalArgumentException
- if other range is not the same type
as this oneNullPointerException
- if other is nullpublic BigInteger size()
sizeAsLong()
,
sizeAsInt()
public long sizeAsLong()
Long.MAX_VALUE
, in which case this
method returns -1L.size()
public int sizeAsInt()
Integer.MAX_VALUE
, in which case this
method returns -1.size()
Copyright © 2015. All Rights Reserved.