K - key typeV - value typepublic class MapWithOrdinalImpl<K,V> extends Object implements MapWithOrdinal<K,V>
This class extends the functionality a regular Map with ordinal lookup support.
Upon insertion an unused ordinal is assigned to the inserted (key, value) tuple.
Upon update the same ordinal id is re-used while value is replaced.
Upon deletion of an existing item, its corresponding ordinal is recycled and could be used by another item.
For any instance with N items, this implementation guarantees that ordinals are in the range of [0, N). However,
the ordinal assignment is dynamic and may change after an insertion or deletion. Consumers of this class are
responsible for explicitly checking the ordinal corresponding to a key via
getOrdinal(Object) before attempting to execute a lookup
with an ordinal.
| Constructor and Description |
|---|
MapWithOrdinalImpl() |
| Modifier and Type | Method and Description |
|---|---|
void |
clear() |
boolean |
containsKey(Object key) |
V |
get(K key) |
Collection<V> |
getAll(K key) |
V |
getByOrdinal(int id)
Returns the value corresponding to the given ordinal.
|
int |
getOrdinal(K key)
Returns the ordinal corresponding to the given key.
|
boolean |
isEmpty() |
Set<K> |
keys() |
boolean |
put(K key,
V value,
boolean overwrite)
Inserts the tuple (key, value) into the map extending the semantics of
Map.put(K, V) with automatic ordinal
assignment. |
boolean |
remove(K key,
V value) |
boolean |
removeAll(K key)
Removes the element corresponding to the key if exists extending the semantics of
Map.remove(java.lang.Object)
with ordinal re-cycling. |
int |
size() |
Collection<V> |
values() |
public V getByOrdinal(int id)
getByOrdinal in interface MapWithOrdinal<K,V>id - ordinal value for lookuppublic int getOrdinal(K key)
getOrdinal in interface MapWithOrdinal<K,V>key - key for ordinal lookuppublic int size()
size in interface MapWithOrdinal<K,V>public boolean isEmpty()
isEmpty in interface MapWithOrdinal<K,V>public Collection<V> getAll(K key)
getAll in interface MapWithOrdinal<K,V>public boolean put(K key, V value, boolean overwrite)
Map.put(K, V) with automatic ordinal
assignment. A new ordinal is assigned if key does not exists. Otherwise the same ordinal is re-used but the value
is replaced.put in interface MapWithOrdinal<K,V>Map.put(K, V)public Collection<V> values()
values in interface MapWithOrdinal<K,V>public boolean containsKey(Object key)
containsKey in interface MapWithOrdinal<K,V>public boolean removeAll(K key)
Map.remove(java.lang.Object)
with ordinal re-cycling. The ordinal corresponding to the given key may be re-assigned to another tuple. It is
important that consumer checks the ordinal value via
getOrdinal(Object) before attempting to look-up by ordinal.removeAll in interface MapWithOrdinal<K,V>Map.remove(java.lang.Object)public void clear()
clear in interface MapWithOrdinal<K,V>Copyright © 2024 The Apache Software Foundation. All rights reserved.