Class ImmutableNavigableSet<E>
- Type Parameters:
E
- element type
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
,NavigableSet<E>
,Set<E>
,SortedSet<E>
NavigableSet
implementation optimized for read efficiency.
Because the elements are stored in an array, it's also possible to get the element by index; see get()
.
-
Field Summary
Fields inherited from class io.permazen.util.AbstractNavigableSet
bounds
-
Constructor Summary
ConstructorDescriptionImmutableNavigableSet
(E[] elems, int minIndex, int maxIndex, Comparator<? super E> comparator) Primary constructor.ImmutableNavigableSet
(E[] elems, Comparator<? super E> comparator) Convenience constructor.ImmutableNavigableSet
(NavigableSet<E> source) Constructor.ImmutableNavigableSet
(NavigableSet<E> source, Comparator<? super E> comparator) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionint
binarySearch
(E elem) Search for the given element in the underlying array.protected Spliterator<E>
buildSpliterator
(Iterator<E> iterator) Build aSpliterator
appropriate for this set from the given instance iterator.Comparator<? super E>
boolean
protected ImmutableNavigableSet<E>
createSubSet
(boolean reverse, Bounds<E> newBounds) Create a (possibly reversed) view of this instance with (possibly) tighter lower and/or upper bounds.first()
get
(int index) Get the element at the specified index.boolean
isEmpty()
Overridden inAbstractIterationSet
to minimize the use ofAbstractIterationSet.size()
.iterator()
last()
pollLast()
int
size()
Calculate size.Overridden inAbstractIterationSet
to avoid the use ofAbstractIterationSet.size()
.stream()
Methods inherited from class io.permazen.util.AbstractNavigableSet
descendingSet, getBounds, getComparator, headSet, headSet, isWithinLowerBound, isWithinUpperBound, remove, searchAbove, searchBelow, subSet, subSet, tailSet, tailSet
Methods inherited from class io.permazen.util.AbstractIterationSet
equals, hashCode, toArray, toArray
Methods inherited from class java.util.AbstractSet
removeAll
Methods inherited from class java.util.AbstractCollection
add, addAll, clear, containsAll, retainAll, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, toArray
-
Constructor Details
-
ImmutableNavigableSet
Convenience constructor.Equivalent to:
ImmutableNavigableSet(elems, 0, elems.length, comparator)
.- Parameters:
elems
- sorted element array; this array is not copied and must be already sortedcomparator
- element comparator, or null for natural ordering- Throws:
IllegalArgumentException
- ifelems
is null
-
ImmutableNavigableSet
public ImmutableNavigableSet(E[] elems, int minIndex, int maxIndex, Comparator<? super E> comparator) Primary constructor.- Parameters:
elems
- sorted element array; this array is not copied and must be already sortedminIndex
- minimum index into array (inclusive)maxIndex
- maximum index into array (exclusive)comparator
- element comparator, or null for natural ordering- Throws:
IllegalArgumentException
- ifelems
is nullIllegalArgumentException
- ifelems
has length less thanmaxIndex
IllegalArgumentException
- ifminIndex > maxIndex
-
Method Details
-
get
Get the element at the specified index.- Parameters:
index
- index into the ordered element array- Returns:
- the element at the specified index
- Throws:
IndexOutOfBoundsException
- ifindex
is negative or greater than or equal tosize()
-
binarySearch
Search for the given element in the underlying array.This method works like
Arrays.binarySearch()
, returning either the index ofelem
in the underlying array given to the constructor if found, or else the one's complement ofelem
's insertion point.The array searched is the array given to the constructor, or if
ImmutableNavigableSet(NavigableSet)
was used, an array containing all of the elements in this set.- Parameters:
elem
- element to search for- Returns:
- index of
elem
, or-(insertion point) - 1
if not found
-
comparator
-
isEmpty
public boolean isEmpty()Description copied from class:AbstractIterationSet
Overridden inAbstractIterationSet
to minimize the use ofAbstractIterationSet.size()
.- Specified by:
isEmpty
in interfaceCollection<E>
- Specified by:
isEmpty
in interfaceSet<E>
- Overrides:
isEmpty
in classAbstractIterationSet<E>
-
size
public int size()Description copied from class:AbstractIterationSet
Calculate size.The implementation in
AbstractIterationSet
iterates through all of the elements.- Specified by:
size
in interfaceCollection<E>
- Specified by:
size
in interfaceSet<E>
- Overrides:
size
in classAbstractIterationSet<E>
-
contains
- Specified by:
contains
in interfaceCollection<E>
- Specified by:
contains
in interfaceSet<E>
- Overrides:
contains
in classAbstractCollection<E>
-
first
-
last
-
pollFirst
- Specified by:
pollFirst
in interfaceNavigableSet<E>
- Overrides:
pollFirst
in classAbstractNavigableSet<E>
-
pollLast
- Specified by:
pollLast
in interfaceNavigableSet<E>
- Overrides:
pollLast
in classAbstractNavigableSet<E>
-
lower
- Specified by:
lower
in interfaceNavigableSet<E>
- Overrides:
lower
in classAbstractNavigableSet<E>
-
floor
- Specified by:
floor
in interfaceNavigableSet<E>
- Overrides:
floor
in classAbstractNavigableSet<E>
-
higher
- Specified by:
higher
in interfaceNavigableSet<E>
- Overrides:
higher
in classAbstractNavigableSet<E>
-
ceiling
- Specified by:
ceiling
in interfaceNavigableSet<E>
- Overrides:
ceiling
in classAbstractNavigableSet<E>
-
iterator
- Specified by:
iterator
in interfaceCollection<E>
- Specified by:
iterator
in interfaceIterable<E>
- Specified by:
iterator
in interfaceNavigableSet<E>
- Specified by:
iterator
in interfaceSet<E>
- Specified by:
iterator
in classAbstractIterationSet<E>
-
descendingIterator
- Specified by:
descendingIterator
in interfaceNavigableSet<E>
- Overrides:
descendingIterator
in classAbstractNavigableSet<E>
-
spliterator
Description copied from class:AbstractIterationSet
Overridden inAbstractIterationSet
to avoid the use ofAbstractIterationSet.size()
.Note: the underlying
CloseableIterator
is not closed when this method is used. Prefer usingAbstractIterationSet.buildSpliterator(java.util.Iterator<E>)
and arranging for the iterator to be closed separately.- Specified by:
spliterator
in interfaceCollection<E>
- Specified by:
spliterator
in interfaceIterable<E>
- Specified by:
spliterator
in interfaceSet<E>
- Specified by:
spliterator
in interfaceSortedSet<E>
- Overrides:
spliterator
in classAbstractIterationSet<E>
-
stream
Description copied from class:AbstractIterationSet
The implementation in
AbstractIterationSet
build a stream from the results fromAbstractIterationSet.iterator()
andbuildSpliterator()
, and marks the iterator for close viaStream.onClose()
.- Specified by:
stream
in interfaceCollection<E>
- Overrides:
stream
in classAbstractIterationSet<E>
-
buildSpliterator
Description copied from class:AbstractIterationSet
Build aSpliterator
appropriate for this set from the given instance iterator.Implementations should probably use
Spliterators.spliteratorUnknownSize()
unless the size is known.- Overrides:
buildSpliterator
in classAbstractNavigableSet<E>
- Parameters:
iterator
- a new iterator returned from {#link #iterator}
-
createSubSet
Description copied from class:AbstractNavigableSet
Create a (possibly reversed) view of this instance with (possibly) tighter lower and/or upper bounds. ThenewBounds
are consistent with the new ordering (i.e., reversed relative to this instance's ordering ifreverse
is true) and have already been range-checked against this instance's current bounds.- Specified by:
createSubSet
in classAbstractNavigableSet<E>
- Parameters:
reverse
- whether the new set's ordering should be reversed relative to this instance's orderingnewBounds
- new bounds- Returns:
- restricted and/or reversed view of this instance
-