Package io.permazen.util
Class ImmutableNavigableMap<K,V>
java.lang.Object
java.util.AbstractMap<K,V>
io.permazen.util.AbstractNavigableMap<K,V>
io.permazen.util.ImmutableNavigableMap<K,V>
- Type Parameters:
K
- key typeV
- value type
- All Implemented Interfaces:
ConcurrentMap<K,
,V> ConcurrentNavigableMap<K,
,V> Map<K,
,V> NavigableMap<K,
,V> SortedMap<K,
V>
public class ImmutableNavigableMap<K,V>
extends AbstractNavigableMap<K,V>
implements ConcurrentNavigableMap<K,V>
An immutable
NavigableMap
implementation optimized for read efficiency.
Because the keys and values are stored in arrays, it's also possible to get the key and/or value by index;
see getKey()
, getValue()
, and getEntry()
.
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,
V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object, V extends Object> -
Field Summary
Fields inherited from class io.permazen.util.AbstractNavigableMap
bounds
-
Constructor Summary
ConstructorDescriptionImmutableNavigableMap
(NavigableMap<K, V> source) Constructor.ImmutableNavigableMap
(NavigableMap<K, V> source, Comparator<? super K> comparator) Constructor.ImmutableNavigableMap
(K[] keys, V[] vals, int minIndex, int maxIndex, Comparator<? super K> comparator) Primary constructor.ImmutableNavigableMap
(K[] keys, V[] vals, Comparator<? super K> comparator) Convenience constructor. -
Method Summary
Modifier and TypeMethodDescriptionint
binarySearch
(K key) Search for the given element in the underlying array.ceilingEntry
(K minKey) ceilingKey
(K minKey) Comparator<? super K>
boolean
containsKey
(Object obj) boolean
containsValue
(Object obj) protected ImmutableNavigableMap<K,
V> createSubMap
(boolean reverse, Bounds<K> newBounds) Create a (possibly reversed) view of this instance with (possibly) tighter lower and/or upper bounds.entrySet()
firstKey()
floorEntry
(K maxKey) getEntry
(int index) Get the entry at the specified index.getKey
(int index) Get the key at the specified index.getValue
(int index) Get the value at the specified index.higherEntry
(K minKey) boolean
isEmpty()
lastKey()
lowerEntry
(K maxKey) putIfAbsent
(K key, V value) boolean
boolean
int
size()
values()
Methods inherited from class io.permazen.util.AbstractNavigableMap
descendingKeySet, getBounds, getComparator, isWithinLowerBound, isWithinUpperBound, keySet, remove, searchAbove, searchBelow
Methods inherited from class java.util.AbstractMap
clear, clone, equals, hashCode, put, putAll, toString
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.concurrent.ConcurrentMap
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, replaceAll
Methods inherited from interface java.util.concurrent.ConcurrentNavigableMap
descendingKeySet, keySet
-
Constructor Details
-
ImmutableNavigableMap
Convenience constructor.Equivalent to:
ImmutableNavigableMap(keys, vals, 0, Math.min(keys.length, vals.length), comparator)
.- Parameters:
keys
- sorted key array; this array is not copied and must be already sortedvals
- value array corresponding tokeys
; this array is not copiedcomparator
- key comparator, or null for natural ordering- Throws:
IllegalArgumentException
- ifkeys
orvals
is nullIllegalArgumentException
- ifkeys
andvals
have different lengths
-
ImmutableNavigableMap
public ImmutableNavigableMap(K[] keys, V[] vals, int minIndex, int maxIndex, Comparator<? super K> comparator) Primary constructor.- Parameters:
keys
- sorted key array; this array is not copied and must be already sortedvals
- value array corresponding tokeys
; this array is not copiedminIndex
- minimum index into arrays (inclusive)maxIndex
- maximum index into arrays (exclusive)comparator
- key comparator, or null for natural ordering- Throws:
IllegalArgumentException
- ifkeys
orvals
is nullIllegalArgumentException
- ifkeys
andvals
has length less thanmaxIndex
IllegalArgumentException
- ifminIndex > maxIndex
-
Method Details
-
getKey
Get the key at the specified index.- Parameters:
index
- index into the ordered key array- Returns:
- the key at the specified index
- Throws:
IndexOutOfBoundsException
- ifindex
is negative or greater than or equal tosize()
-
getValue
Get the value at the specified index.- Parameters:
index
- index into the ordered value array- Returns:
- the value at the specified index
- Throws:
IndexOutOfBoundsException
- ifindex
is negative or greater than or equal tosize()
-
getEntry
Get the entry at the specified index.- Parameters:
index
- index into the ordered entry array- Returns:
- the entry 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 ofkey
in the underlying array given to the constructor if found, or else the one's complement ofkey
's insertion point.The array searched is the array given to the constructor, or if
ImmutableNavigableMap(NavigableMap)
was used, an array containing all of the keys in this map.- Parameters:
key
- key to search for- Returns:
- index of
key
, or-(insertion point) - 1
if not found
-
comparator
- Specified by:
comparator
in interfaceSortedMap<K,
V>
-
isEmpty
public boolean isEmpty() -
size
public int size() -
containsKey
- Specified by:
containsKey
in interfaceMap<K,
V> - Overrides:
containsKey
in classAbstractNavigableMap<K,
V>
-
get
-
values
-
containsValue
- Specified by:
containsValue
in interfaceMap<K,
V> - Overrides:
containsValue
in classAbstractMap<K,
V>
-
firstKey
-
firstEntry
- Specified by:
firstEntry
in interfaceNavigableMap<K,
V> - Overrides:
firstEntry
in classAbstractNavigableMap<K,
V>
-
lastKey
-
lastEntry
- Specified by:
lastEntry
in interfaceNavigableMap<K,
V> - Overrides:
lastEntry
in classAbstractNavigableMap<K,
V>
-
lowerKey
- Specified by:
lowerKey
in interfaceNavigableMap<K,
V> - Overrides:
lowerKey
in classAbstractNavigableMap<K,
V>
-
lowerEntry
- Specified by:
lowerEntry
in interfaceNavigableMap<K,
V> - Overrides:
lowerEntry
in classAbstractNavigableMap<K,
V>
-
floorKey
- Specified by:
floorKey
in interfaceNavigableMap<K,
V> - Overrides:
floorKey
in classAbstractNavigableMap<K,
V>
-
floorEntry
- Specified by:
floorEntry
in interfaceNavigableMap<K,
V> - Overrides:
floorEntry
in classAbstractNavigableMap<K,
V>
-
higherKey
- Specified by:
higherKey
in interfaceNavigableMap<K,
V> - Overrides:
higherKey
in classAbstractNavigableMap<K,
V>
-
higherEntry
- Specified by:
higherEntry
in interfaceNavigableMap<K,
V> - Overrides:
higherEntry
in classAbstractNavigableMap<K,
V>
-
ceilingKey
- Specified by:
ceilingKey
in interfaceNavigableMap<K,
V> - Overrides:
ceilingKey
in classAbstractNavigableMap<K,
V>
-
ceilingEntry
- Specified by:
ceilingEntry
in interfaceNavigableMap<K,
V> - Overrides:
ceilingEntry
in classAbstractNavigableMap<K,
V>
-
pollFirstEntry
- Specified by:
pollFirstEntry
in interfaceNavigableMap<K,
V> - Overrides:
pollFirstEntry
in classAbstractNavigableMap<K,
V>
-
pollLastEntry
- Specified by:
pollLastEntry
in interfaceNavigableMap<K,
V> - Overrides:
pollLastEntry
in classAbstractNavigableMap<K,
V>
-
entrySet
-
putIfAbsent
- Specified by:
putIfAbsent
in interfaceConcurrentMap<K,
V> - Specified by:
putIfAbsent
in interfaceMap<K,
V>
-
replace
-
replace
-
remove
-
descendingMap
- Specified by:
descendingMap
in interfaceConcurrentNavigableMap<K,
V> - Specified by:
descendingMap
in interfaceNavigableMap<K,
V> - Overrides:
descendingMap
in classAbstractNavigableMap<K,
V>
-
subMap
-
headMap
-
tailMap
-
headMap
- Specified by:
headMap
in interfaceConcurrentNavigableMap<K,
V> - Specified by:
headMap
in interfaceNavigableMap<K,
V> - Overrides:
headMap
in classAbstractNavigableMap<K,
V>
-
tailMap
- Specified by:
tailMap
in interfaceConcurrentNavigableMap<K,
V> - Specified by:
tailMap
in interfaceNavigableMap<K,
V> - Overrides:
tailMap
in classAbstractNavigableMap<K,
V>
-
subMap
public ImmutableNavigableMap<K,V> subMap(K newMinKey, boolean minInclusive, K newMaxKey, boolean maxInclusive) - Specified by:
subMap
in interfaceConcurrentNavigableMap<K,
V> - Specified by:
subMap
in interfaceNavigableMap<K,
V> - Overrides:
subMap
in classAbstractNavigableMap<K,
V>
-
createSubMap
Description copied from class:AbstractNavigableMap
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:
createSubMap
in classAbstractNavigableMap<K,
V> - Parameters:
reverse
- whether the new map's ordering should be reversed relative to this instance's orderingnewBounds
- new bounds- Returns:
- restricted and/or reversed view of this instance
-