Package uk.ipfreely.sets
Class AddressSets
java.lang.Object
uk.ipfreely.sets.AddressSets
Static factory methods for creating AddressSet
s, Range
s and Block
s.
-
Method Summary
Modifier and TypeMethodDescriptionaddress
(A address) Single address asBlock
.block
(A first, int maskSize) CreatesBlock
from network address and mask size.block
(A first, A last) Creates a block from the given addresses which MUST form a valid CIDR block.static <A extends Address<A>,
S extends AddressSet<A>>
AddressSet<A> Version ofof(AddressSet[])
intended for standard collections.static <A extends Address<A>,
S extends AddressSet<A>>
AddressSet<A> of
(S... sets) Factory method forAddressSet
.static Block
<?> Parses a CIDR string form as defined by RFC4632.AsparseCidr(String)
with version expectation.range
(A first, A last) CreatesRange
instance.
-
Method Details
-
of
@SafeVarargs public static <A extends Address<A>,S extends AddressSet<A>> AddressSet<A> of(S... sets) Factory method for
AddressSet
.If all members form a contiguous range, returns
Range
. If all members form CIDR block, returnsBlock
.// EXAMPLE Range<V4> classA = AddressSets.parseCidr(Family.v4(), "10.0.0.0/8"); Range<V4> classB = AddressSets.parseCidr(Family.v4(), "176.16.0.0/12"); Range<V4> classC = AddressSets.parseCidr(Family.v4(), "192.168.0.0/16"); // RFC-1918 Address Allocation for Private Internets AddressSet<V4> privateRanges = AddressSets.of(classA, classB, classC);
- Type Parameters:
A
- address typeS
- range type- Parameters:
sets
- source sets- Returns:
- union of given sets
-
from
Version of
of(AddressSet[])
intended for standard collections.// EXAMPLE AddressSet<V4> empty = AddressSets.from(Collections.emptySet());
// EXAMPLE List<String> addresses = Arrays.asList("192.168.0.1", "192.168.0.10", "192.168.0.11", "192.168.0.12"); // 4 entries: "192.168.0.1/32", "192.168.0.10/32", "192.168.0.11/32", "192.168.0.12/32" Set<Block<V4>> raw = addresses.stream() .map(Family.v4()::parse) .map(AddressSets::address) .collect(Collectors.toSet()); // 2 entries: "192.168.0.1/32", "192.168.0.10-192.168.0.12" Set<Range<V4>> rationalized = AddressSets.from(raw) .ranges() .collect(Collectors.toSet());
- Type Parameters:
A
- address typeS
- set type- Parameters:
sets
- source sets- Returns:
- union of given sets
-
address
Single address asBlock
.- Type Parameters:
A
- IP version- Parameters:
address
- IP- Returns:
- immutable instance
-
block
Creates
Block
from network address and mask size. ThemaskSize
must be greater or equal to zero and less than or equal toFamily.width()
. The mask must cover all the true bits of the address.- Type Parameters:
A
- the Ip type- Parameters:
first
- the first IP in the blockmaskSize
- the number of mask bits- Returns:
- the block instance
-
block
Creates a block from the given addresses which MUST form a valid CIDR block.
- Type Parameters:
A
- IP version- Parameters:
first
- addresslast
- address which must be greater or equal to the first address- Returns:
- block
-
range
- Type Parameters:
A
- the IP type- Parameters:
first
- first elementlast
- last element which MUST be greater or equal to first- Returns:
- an immutable range of IP addresses
-
parseCidr
Parses a CIDR string form as defined by RFC4632. Example:"127.0.0.1/32"
.- Parameters:
cidrBlock
- the CIDR notation string- Returns:
- the block instance
- Throws:
ParseException
- on invalid expression
-
parseCidr
AsparseCidr(String)
with version expectation.- Type Parameters:
A
- the address type- Parameters:
family
- the IP familycidrBlock
- the CIDR notation string- Returns:
- block instance
- Throws:
ParseException
- on invalid expression or wrong IP version
-