- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractSet<E>
-
- inet.ipaddr.format.util.AddressTrieSet<E>
-
- Type Parameters:
E
- the address type
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
,java.lang.Iterable<E>
,java.util.Collection<E>
,java.util.NavigableSet<E>
,java.util.Set<E>
,java.util.SortedSet<E>
public class AddressTrieSet<E extends Address> extends java.util.AbstractSet<E> implements java.util.NavigableSet<E>, java.lang.Cloneable, java.io.Serializable
Wraps aAddressTrie
to view it as a Java Collections Framework set, implementing theSet
,SortedSet
andNavigableSet
interfaces.Like
TreeSet
, this set is backed by a binary tree and implements the same interfaces thatTreeSet
does. But there are some significant differences between the two binary tree implementations. SeeAddressTrieMap
for a description of some of the differences.TreeMap
is backed by aTreeSet
andAddressTrieMap
is backed by anAddressTrie
just likeAddressTrie
, so all of the same implementation comparisons apply equally between the map implementations and the set implementations.With the trie set, only addresses that are either individual address or prefix block subnets of the same type and version can be added to the trie, see
AddressTrie.AddressComparator
for a comparator for the ordering.Should you wish to store, in a collection, address instances that are not individual address or prefix block subnets, you can use
TreeSet
or any other Java collections framework set to store addresses of any type, or addresses of different versions or types in the same set, since all address items in this library are comparable with a natural ordering. There are additional orderings provided by this library as well, seeAddressComparator
.- Author:
- scfoley
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
AddressTrieSet.Range<E extends Address>
-
Constructor Summary
Constructors Constructor Description AddressTrieSet(AddressTrie<E> trie)
AddressTrieSet(AddressTrie<E> trie, java.util.Collection<? extends E> collection)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(E e)
Adds the given single address or prefix block subnet to this set.AddressTrie<E>
asTrie()
Returns a trie representing this set.java.util.Iterator<E>
blockSizeIterator()
Iterates from largest prefix blocks to smallest to individual addresses.E
ceiling(E e)
void
clear()
AddressTrieSet<E>
clone()
Clones the set along with the backing trie.java.util.Comparator<E>
comparator()
java.util.Iterator<E>
containedFirstIterator()
Returns an iterator that visits contained addresses and subnet blocks before their containing subnet blocks.java.util.Iterator<E>
containingFirstIterator()
Returns an iterator that visits containing subnet blocks before their contained addresses and subnet blocks.boolean
contains(java.lang.Object o)
java.util.Iterator<E>
descendingIterator()
AddressTrieSet<E>
descendingSet()
boolean
elementContains(E addr)
Returns true if a subnet or address in the set contains the given subnet or address.AddressTrieSet<E>
elementsContainedBy(E addr)
Returns a subset consisting of those addresses in the set contained by the given address.AddressTrieSet<E>
elementsContaining(E addr)
Returns a subset consisting of those addresses in the set that contain the given address.boolean
equals(java.lang.Object o)
E
first()
E
floor(E e)
AddressTrieSet.Range<E>
getRange()
Returns the range if this set has a restricted range, seehasRestrictedRange()
.int
hashCode()
boolean
hasRestrictedRange()
Returns whether this set is the result of a call toheadSet(Address)
,tailSet(Address)
,subSet(Address, Address)
or any of the other six methods with the same names.AddressTrieSet<E>
headSet(E toElement)
AddressTrieSet<E>
headSet(E toElement, boolean inclusive)
E
higher(E e)
boolean
isEmpty()
java.util.Iterator<E>
iterator()
E
last()
E
longestPrefixMatch(E addr)
Returns the element with the longest prefix match with the given address.E
lower(E e)
E
pollFirst()
E
pollLast()
boolean
remove(java.lang.Object o)
boolean
removeAll(java.util.Collection<?> collection)
int
size()
Returns the number of elements in this set.java.util.Spliterator<E>
spliterator()
AddressTrieSet<E>
subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
AddressTrieSet<E>
subSet(E fromElement, E toElement)
AddressTrieSet<E>
tailSet(E fromElement)
AddressTrieSet<E>
tailSet(E fromElement, boolean inclusive)
java.lang.String
toTrieString()
-
-
-
Constructor Detail
-
AddressTrieSet
public AddressTrieSet(AddressTrie<E> trie)
-
AddressTrieSet
public AddressTrieSet(AddressTrie<E> trie, java.util.Collection<? extends E> collection)
-
-
Method Detail
-
descendingSet
public AddressTrieSet<E> descendingSet()
-
asTrie
public AddressTrie<E> asTrie()
Returns a trie representing this set.If this set has a restricted range,
hasRestrictedRange()
, this generates a new trie for the set with only the nodes pertaining to the subset. Otherwise this returns the backing trie for this set.When a new trie is generated, the original backing trie for this set remains the same, it is not changed to the new trie.
The returned trie will always have the same natural trie ordering, even if this set has the reverse ordering.
-
hasRestrictedRange
public boolean hasRestrictedRange()
Returns whether this set is the result of a call toheadSet(Address)
,tailSet(Address)
,subSet(Address, Address)
or any of the other six methods with the same names.- Returns:
-
getRange
public AddressTrieSet.Range<E> getRange()
Returns the range if this set has a restricted range, seehasRestrictedRange()
. Otherwise returns null.- Returns:
-
size
public int size()
Returns the number of elements in this set. This is a constant time operation, unless the set has a restricted range, seehasRestrictedRange()
, in which case it is a linear time operation proportional to the number of elements.
-
isEmpty
public boolean isEmpty()
-
contains
public boolean contains(java.lang.Object o)
-
add
public boolean add(E e)
Adds the given single address or prefix block subnet to this set.If the given address is not a single address nor prefix block, then this method throws IllegalArgumentException.
See
AddressTrie
-
remove
public boolean remove(java.lang.Object o)
-
clear
public void clear()
-
hashCode
public int hashCode()
-
equals
public boolean equals(java.lang.Object o)
-
clone
public AddressTrieSet<E> clone()
Clones the set along with the backing trie. If the set had a restricted range, the clone does not.
-
removeAll
public boolean removeAll(java.util.Collection<?> collection)
-
iterator
public java.util.Iterator<E> iterator()
- Specified by:
iterator
in interfacejava.util.Collection<E extends Address>
- Specified by:
iterator
in interfacejava.lang.Iterable<E extends Address>
- Specified by:
iterator
in interfacejava.util.NavigableSet<E extends Address>
- Specified by:
iterator
in interfacejava.util.Set<E extends Address>
- Specified by:
iterator
in classjava.util.AbstractCollection<E extends Address>
-
descendingIterator
public java.util.Iterator<E> descendingIterator()
-
containingFirstIterator
public java.util.Iterator<E> containingFirstIterator()
Returns an iterator that visits containing subnet blocks before their contained addresses and subnet blocks.
-
containedFirstIterator
public java.util.Iterator<E> containedFirstIterator()
Returns an iterator that visits contained addresses and subnet blocks before their containing subnet blocks.
-
spliterator
public java.util.Spliterator<E> spliterator()
- Specified by:
spliterator
in interfacejava.util.Collection<E extends Address>
- Specified by:
spliterator
in interfacejava.lang.Iterable<E extends Address>
- Specified by:
spliterator
in interfacejava.util.Set<E extends Address>
- Specified by:
spliterator
in interfacejava.util.SortedSet<E extends Address>
-
comparator
public java.util.Comparator<E> comparator()
-
blockSizeIterator
public java.util.Iterator<E> blockSizeIterator()
Iterates from largest prefix blocks to smallest to individual addresses. Blocks of equal size are iterated in set order.- Returns:
-
subSet
public AddressTrieSet<E> subSet(E fromElement, E toElement)
-
subSet
public AddressTrieSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
-
headSet
public AddressTrieSet<E> headSet(E toElement)
-
headSet
public AddressTrieSet<E> headSet(E toElement, boolean inclusive)
-
tailSet
public AddressTrieSet<E> tailSet(E fromElement)
-
tailSet
public AddressTrieSet<E> tailSet(E fromElement, boolean inclusive)
-
pollFirst
public E pollFirst()
-
pollLast
public E pollLast()
-
toTrieString
public java.lang.String toTrieString()
-
elementsContainedBy
public AddressTrieSet<E> elementsContainedBy(E addr)
Returns a subset consisting of those addresses in the set contained by the given address. The subset will have a restricted range matching the range of the given subnet or address.If the subset would be the same size as this set, then this set is returned. The subset will the same backing trie as this set.
- Parameters:
addr
-- Returns:
-
elementsContaining
public AddressTrieSet<E> elementsContaining(E addr)
Returns a subset consisting of those addresses in the set that contain the given address. The subset will have the same restricted range (if any) as this set.If the subset would be the same size as this set, then this set is returned. Otherwise, the subset is backed by a new trie.
- Parameters:
addr
-- Returns:
-
elementContains
public boolean elementContains(E addr)
Returns true if a subnet or address in the set contains the given subnet or address.- Parameters:
addr
-- Returns:
-
-