- Type Parameters:
K
- key typeV
- value type
- All Implemented Interfaces:
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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
boolean
containsKey
(K key) get set of values for key.get set of values for key.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()
keys()
boolean
Inserts the tuple (key, value) into the multimap with automatic ordinal assignment.boolean
Removes the element corresponding to the key/value if exists with ordinal re-cycling.boolean
remove all entries of key.int
size()
values()
-
Constructor Details
-
MultiMapWithOrdinal
public MultiMapWithOrdinal()
-
-
Method Details
-
getByOrdinal
Returns the value corresponding to the given ordinal.- Specified by:
getByOrdinal
in interfaceMapWithOrdinal<K,
V> - Parameters:
id
- ordinal value for lookup- Returns:
- an instance of V
-
getOrdinal
Returns the ordinal corresponding to the given key.- Specified by:
getOrdinal
in interfaceMapWithOrdinal<K,
V> - Parameters:
key
- key for ordinal lookup- Returns:
- ordinal value corresponding to key if it exists or -1
-
size
public int size()- Specified by:
size
in interfaceMapWithOrdinal<K,
V>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceMapWithOrdinal<K,
V>
-
get
get set of values for key.- Specified by:
get
in interfaceMapWithOrdinal<K,
V>
-
getAll
get set of values for key.- Specified by:
getAll
in interfaceMapWithOrdinal<K,
V>
-
put
Inserts the tuple (key, value) into the multimap with automatic ordinal assignment.A new ordinal is assigned if key/value pair does not exists.
If overwrite is true the existing key will be overwritten with value else value will be appended to the multimap.
- Specified by:
put
in interfaceMapWithOrdinal<K,
V>
-
values
- Specified by:
values
in interfaceMapWithOrdinal<K,
V>
-
containsKey
- Specified by:
containsKey
in interfaceMapWithOrdinal<K,
V>
-
remove
Removes the element corresponding to the key/value if exists 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.If the multimap is changed return true.
- Specified by:
remove
in interfaceMapWithOrdinal<K,
V>
-
removeAll
remove all entries of key.- Specified by:
removeAll
in interfaceMapWithOrdinal<K,
V>
-
clear
public void clear()- Specified by:
clear
in interfaceMapWithOrdinal<K,
V>
-
keys
- Specified by:
keys
in interfaceMapWithOrdinal<K,
V>
-