Class MultiMapWithOrdinal<K,V>

Type Parameters:
K - key type
V - value type
All Implemented Interfaces:

public class MultiMapWithOrdinal<K,V> extends Object implements MapWithOrdinal<K,V>
An implementation of a multimap that supports constant time look-up by a generic key or an ordinal.

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 Details

    • MultiMapWithOrdinal

      public MultiMapWithOrdinal()
  • Method Details

    • getByOrdinal

      public V getByOrdinal(int id)
      Returns the value corresponding to the given ordinal.
      Specified by:
      getByOrdinal in interface MapWithOrdinal<K,V>
      id - ordinal value for lookup
      an instance of V
    • getOrdinal

      public int getOrdinal(K key)
      Returns the ordinal corresponding to the given key.
      Specified by:
      getOrdinal in interface MapWithOrdinal<K,V>
      key - key for ordinal lookup
      ordinal value corresponding to key if it exists or -1
    • size

      public int size()
      Specified by:
      size in interface MapWithOrdinal<K,V>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface MapWithOrdinal<K,V>
    • get

      public V get(K key)
      get set of values for key.
      Specified by:
      get in interface MapWithOrdinal<K,V>
    • getAll

      public Collection<V> getAll(K key)
      get set of values for key.
      Specified by:
      getAll in interface MapWithOrdinal<K,V>
    • put

      public boolean put(K key, V value, boolean overwrite)
      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 interface MapWithOrdinal<K,V>
    • values

      public Collection<V> values()
      Specified by:
      values in interface MapWithOrdinal<K,V>
    • containsKey

      public boolean containsKey(K key)
      Specified by:
      containsKey in interface MapWithOrdinal<K,V>
    • remove

      public boolean remove(K key, V value)
      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 interface MapWithOrdinal<K,V>
    • removeAll

      public boolean removeAll(K key)
      remove all entries of key.
      Specified by:
      removeAll in interface MapWithOrdinal<K,V>
    • clear

      public void clear()
      Specified by:
      clear in interface MapWithOrdinal<K,V>
    • keys

      public Set<K> keys()
      Specified by:
      keys in interface MapWithOrdinal<K,V>