Module inet.ipaddr

Class AddressTrieSet<E extends Address>

  • 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 a AddressTrie to view it as a Java Collections Framework set, implementing the Set, SortedSet and NavigableSet interfaces.

    Like TreeSet, this set is backed by a binary tree and implements the same interfaces that TreeSet does. But there are some significant differences between the two binary tree implementations. See AddressTrieMap for a description of some of the differences. TreeMap is backed by a TreeSet and AddressTrieMap is backed by an AddressTrie just like AddressTrie, 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, see AddressComparator.

    Author:
    scfoley
    See Also:
    Serialized Form
    • 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()
        Specified by:
        descendingSet in interface java.util.NavigableSet<E extends Address>
      • 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.

      • 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, see hasRestrictedRange(), in which case it is a linear time operation proportional to the number of elements.
        Specified by:
        size in interface java.util.Collection<E extends Address>
        Specified by:
        size in interface java.util.Set<E extends Address>
        Specified by:
        size in class java.util.AbstractCollection<E extends Address>
        Returns:
        the number of elements in this set
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<E extends Address>
        Specified by:
        isEmpty in interface java.util.Set<E extends Address>
        Overrides:
        isEmpty in class java.util.AbstractCollection<E extends Address>
      • contains

        public boolean contains​(java.lang.Object o)
        Specified by:
        contains in interface java.util.Collection<E extends Address>
        Specified by:
        contains in interface java.util.Set<E extends Address>
        Overrides:
        contains in class java.util.AbstractCollection<E extends Address>
      • 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

        Specified by:
        add in interface java.util.Collection<E extends Address>
        Specified by:
        add in interface java.util.Set<E extends Address>
        Overrides:
        add in class java.util.AbstractCollection<E extends Address>
      • remove

        public boolean remove​(java.lang.Object o)
        Specified by:
        remove in interface java.util.Collection<E extends Address>
        Specified by:
        remove in interface java.util.Set<E extends Address>
        Overrides:
        remove in class java.util.AbstractCollection<E extends Address>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<E extends Address>
        Specified by:
        clear in interface java.util.Set<E extends Address>
        Overrides:
        clear in class java.util.AbstractCollection<E extends Address>
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Collection<E extends Address>
        Specified by:
        hashCode in interface java.util.Set<E extends Address>
        Overrides:
        hashCode in class java.util.AbstractSet<E extends Address>
      • equals

        public boolean equals​(java.lang.Object o)
        Specified by:
        equals in interface java.util.Collection<E extends Address>
        Specified by:
        equals in interface java.util.Set<E extends Address>
        Overrides:
        equals in class java.util.AbstractSet<E extends Address>
      • 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)
        Specified by:
        removeAll in interface java.util.Collection<E extends Address>
        Specified by:
        removeAll in interface java.util.Set<E extends Address>
        Overrides:
        removeAll in class java.util.AbstractSet<E extends Address>
      • iterator

        public java.util.Iterator<E> iterator()
        Specified by:
        iterator in interface java.util.Collection<E extends Address>
        Specified by:
        iterator in interface java.lang.Iterable<E extends Address>
        Specified by:
        iterator in interface java.util.NavigableSet<E extends Address>
        Specified by:
        iterator in interface java.util.Set<E extends Address>
        Specified by:
        iterator in class java.util.AbstractCollection<E extends Address>
      • descendingIterator

        public java.util.Iterator<E> descendingIterator()
        Specified by:
        descendingIterator in interface java.util.NavigableSet<E extends Address>
      • 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 interface java.util.Collection<E extends Address>
        Specified by:
        spliterator in interface java.lang.Iterable<E extends Address>
        Specified by:
        spliterator in interface java.util.Set<E extends Address>
        Specified by:
        spliterator in interface java.util.SortedSet<E extends Address>
      • comparator

        public java.util.Comparator<E> comparator()
        Specified by:
        comparator in interface java.util.SortedSet<E extends Address>
      • 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)
        Specified by:
        subSet in interface java.util.NavigableSet<E extends Address>
        Specified by:
        subSet in interface java.util.SortedSet<E extends Address>
      • subSet

        public AddressTrieSet<E> subSet​(E fromElement,
                                        boolean fromInclusive,
                                        E toElement,
                                        boolean toInclusive)
        Specified by:
        subSet in interface java.util.NavigableSet<E extends Address>
      • headSet

        public AddressTrieSet<E> headSet​(E toElement)
        Specified by:
        headSet in interface java.util.NavigableSet<E extends Address>
        Specified by:
        headSet in interface java.util.SortedSet<E extends Address>
      • headSet

        public AddressTrieSet<E> headSet​(E toElement,
                                         boolean inclusive)
        Specified by:
        headSet in interface java.util.NavigableSet<E extends Address>
      • tailSet

        public AddressTrieSet<E> tailSet​(E fromElement)
        Specified by:
        tailSet in interface java.util.NavigableSet<E extends Address>
        Specified by:
        tailSet in interface java.util.SortedSet<E extends Address>
      • tailSet

        public AddressTrieSet<E> tailSet​(E fromElement,
                                         boolean inclusive)
        Specified by:
        tailSet in interface java.util.NavigableSet<E extends Address>
      • first

        public E first()
        Specified by:
        first in interface java.util.SortedSet<E extends Address>
      • last

        public E last()
        Specified by:
        last in interface java.util.SortedSet<E extends Address>
      • lower

        public E lower​(E e)
        Specified by:
        lower in interface java.util.NavigableSet<E extends Address>
      • floor

        public E floor​(E e)
        Specified by:
        floor in interface java.util.NavigableSet<E extends Address>
      • ceiling

        public E ceiling​(E e)
        Specified by:
        ceiling in interface java.util.NavigableSet<E extends Address>
      • higher

        public E higher​(E e)
        Specified by:
        higher in interface java.util.NavigableSet<E extends Address>
      • pollFirst

        public E pollFirst()
        Specified by:
        pollFirst in interface java.util.NavigableSet<E extends Address>
      • pollLast

        public E pollLast()
        Specified by:
        pollLast in interface java.util.NavigableSet<E extends Address>
      • 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:
      • longestPrefixMatch

        public E longestPrefixMatch​(E addr)
        Returns the element with the longest prefix match with the given address.
        Parameters:
        addr -
        Returns: