Class ManyToManyMap<K,V>

java.lang.Object
com.github.tadukoo.util.map.ManyToManyMap<K,V>
Type Parameters:
K - The type of keys in this ManyToManyMap
V - The type of values in this ManyToManyMap
Direct Known Subclasses:
HashManyToManyMap, TreeManyToManyMap

public abstract class ManyToManyMap<K,V> extends Object
This class relates keys to values, but allows for a many-to-many relationship of keys to values.
The implementation of this class is essentially two MultiMaps. One associates keys to values, and the other associates values to keys.
For the most part, it's the same methods that a Map has, but there are some unique to this to allow for more specific functionality.
Since:
Pre-Alpha
Version:
Alpha v.0.2
Author:
Logan Ferree (Tadukoo)
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final MultiMap<K,V>
    The backing keys to values MultiMap
    private final MultiMap<V,K>
    The backing values to keys MultiMap
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    ManyToManyMap(MultiMap<K,V> keysToValues, MultiMap<V,K> valuesToKeys)
    Sets the backing MultiMaps for this ManyToManyMap.
    protected
    ManyToManyMap(MultiMap<K,V> keysToValues, MultiMap<V,K> valuesToKeys, ManyToManyMap<K,V> manyToManyMap)
    Sets the backing MultiMaps for this ManyToManyMap and calls putAll for the given ManyToManyMap's entries.
    protected
    ManyToManyMap(MultiMap<K,V> keysToValues, MultiMap<V,K> valuesToKeys, MultiMap<K,V> multiMap)
    Sets the backing MultiMaps for this ManyToManyMap and calls putAll for the given MultiMap's entries.
    protected
    ManyToManyMap(MultiMap<K,V> keysToValues, MultiMap<V,K> valuesToKeys, Pair<K,V>... entries)
    Sets the backing MultiMaps for this ManyToManyMap and calls putAllKeyValMappings(Pair[]) for the given key-value Pairs.
    protected
    ManyToManyMap(MultiMap<K,V> keysToValues, MultiMap<V,K> valuesToKeys, Map<K,V> map)
    Sets the backing MultiMaps for this ManyToManyMap and calls putAll for the given map's entries.
  • Method Summary

    Modifier and Type
    Method
    Description
    final void
    Removes all of the key-value associations from this ManyToManyMap.
    final boolean
    Returns true if this ManyToManyMap contains a mapping for the specified key.
    final boolean
    Returns true if this ManyToManyMap contains a mapping for the specified value.
    boolean
    Compares the given object with this ManyToManyMap for equality.
    final void
    forEach(BiConsumer<? super K,? super V> action)
    Performs the given action for each key-value association in this ManyToManyMap until all associations have been processed or the action throws an exception.
    final List<K>
    getKeys(V value)
    Returns the list of keys to which the specified value is mapped, or null if this ManyToManyMap contains no mappings for the value.
    final List<V>
    getValues(K key)
    Returns the list of values to which the specified key is mapped, or null if this ManyToManyMap contains no mappings for the key.
    final boolean
    Returns true if this map contains no key-value mappings.
    final Set<K>
    Returns the Set of keys in this ManyToManyMap.
    final int
    Returns the number of keys currently in this ManyToManyMap.
    final MultiMap<K,V>
    Returns the underlying keysToValues MultiMap of this ManyToManyMap.
    final void
    put(K key, V value)
    Associates the specified value with the specified key in this ManyToManyMap.
    final void
    putAllKeys(V value, List<K> keys)
    Associates all of the given keys with the given value.
    final void
    Associates all of the key-value mappings from the given ManyToManyMap into this ManyToManyMap.
    final void
    Associates all of the key-value mappings from the given MultiMap into this ManyToManyMap.
    final void
    Puts all the given key-value Pairs into this ManyToManyMap.
    final void
    Associates all of the key-value mappings from the given Map into this ManyToManyMap.
    final void
    Associates all of the value-key mappings from the given ManyToManyMap into this ManyToManyMap.
    final void
    Associates all of the value-key mappings from the given MultiMap into this ManyToManyMap.
    final void
    Puts all the given value-key Pairs into this ManyToManyMap.
    final void
    Associates all of the value-key mappings from the given Map into this ManyToManyMap.
    final void
    putAllValues(K key, List<V> values)
    Associates all of the given values with the given key.
    final boolean
    remove(K key, V value)
    Removes the specified value from being associated with the specified key, if the mapping exists.
    final boolean
    removeEntireKeysList(V value, List<K> keys)
    Removes the given list of keys associated with the given value if the list of keys matches the current list associated with that value.
    final boolean
    removeEntireValuesList(K key, List<V> values)
    Removes the given list of values associated with the given key if the list of values matches the current list associated with that key.
    final List<V>
    removeKey(K key)
    Removes all values associated with the given key from the ManyToManyMap.
    final List<K>
    removeValue(V value)
    Removes all keys associated with the given value from the ManyToManyMap.
    final List<K>
    replaceEntireKeyList(V value, List<K> keys)
    Replaces the current list of keys associated with the given value with the given list of keys, only if it is currently mapped to a value.
    final boolean
    replaceEntireKeyList(V value, List<K> oldKeys, List<K> newKeys)
    Replaces the current list of keys associated with the given value with the given new list of keys if the given old list matches the current list.
    final List<V>
    replaceEntireValueList(K key, List<V> values)
    Replaces the current list of values associated with the given key with the given list of values.
    final boolean
    replaceEntireValueList(K key, List<V> oldValues, List<V> newValues)
    Replaces the current list of values associated with the given key with the given new list of values if the given old list matches the current list.
    final boolean
    replaceKey(V value, K oldKey, K newKey)
    Replaces the given old key with the given new key for an association to the given value, if the old key is currently associated with the given value.
    final boolean
    replaceValue(K key, V oldValue, V newValue)
    Replaces the given old value with the given new value for an association to the given key, if the old value is currently associated with the given key.
    final int
    Returns the number of key-value associations of this ManyToManyMap.
    final Set<V>
    Returns the Set of values in this ManyToManyMap.
    final int
    Returns the number of values currently in this ManyToManyMap.
    final MultiMap<V,K>
    Returns the underlying valuesToKeys MultiMap of this ManyToManyMap.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • keysToValues

      private final MultiMap<K,V> keysToValues
      The backing keys to values MultiMap
    • valuesToKeys

      private final MultiMap<V,K> valuesToKeys
      The backing values to keys MultiMap
  • Constructor Details

    • ManyToManyMap

      protected ManyToManyMap(MultiMap<K,V> keysToValues, MultiMap<V,K> valuesToKeys)
      Sets the backing MultiMaps for this ManyToManyMap.
      Parameters:
      keysToValues - The keysToValues MultiMap to use for this ManyToManyMap
      valuesToKeys - The valuesToKeys MultiMap to use for this ManyToManyMap
    • ManyToManyMap

      @SafeVarargs protected ManyToManyMap(MultiMap<K,V> keysToValues, MultiMap<V,K> valuesToKeys, Pair<K,V>... entries)
      Sets the backing MultiMaps for this ManyToManyMap and calls putAllKeyValMappings(Pair[]) for the given key-value Pairs.
      Parameters:
      keysToValues - The keysToValues MultiMap to use for this ManyToManyMap
      valuesToKeys - The valuesToKeys MultiMap to use for this ManyToManyMap
      entries - A collection of key-value Pairs to be put in this ManyToManyMap
    • ManyToManyMap

      protected ManyToManyMap(MultiMap<K,V> keysToValues, MultiMap<V,K> valuesToKeys, Map<K,V> map)
      Sets the backing MultiMaps for this ManyToManyMap and calls putAll for the given map's entries.
      Parameters:
      keysToValues - The keysToValues MultiMap to use for this ManyToManyMap
      valuesToKeys - The valuesToKeys MultiMap to use for this ManyToManyMap
      map - A Map containing entries to add to this ManyToManyMap
    • ManyToManyMap

      protected ManyToManyMap(MultiMap<K,V> keysToValues, MultiMap<V,K> valuesToKeys, MultiMap<K,V> multiMap)
      Sets the backing MultiMaps for this ManyToManyMap and calls putAll for the given MultiMap's entries.
      Parameters:
      keysToValues - The keysToValues MultiMap to use for this ManyToManyMap
      valuesToKeys - The valuesToKeys MultiMap to use for this ManyToManyMap
      multiMap - A MultiMap containing entries to add to this ManyToManyMap
    • ManyToManyMap

      protected ManyToManyMap(MultiMap<K,V> keysToValues, MultiMap<V,K> valuesToKeys, ManyToManyMap<K,V> manyToManyMap)
      Sets the backing MultiMaps for this ManyToManyMap and calls putAll for the given ManyToManyMap's entries.
      Parameters:
      keysToValues - The keysToValues MultiMap to use for this ManyToManyMap
      valuesToKeys - The valuesToKeys MultiMap to use for this ManyToManyMap
      manyToManyMap - A ManyToManyMap containing entries to add to this ManyToManyMap
  • Method Details

    • isEmpty

      public final boolean isEmpty()
      Returns true if this map contains no key-value mappings. Calls MultiMap.isEmpty() on the underlying MultiMaps.
      Returns:
      true if this map contains no key-value mappings.
    • equals

      public boolean equals(Object o)
      Compares the given object with this ManyToManyMap for equality. Returns true if the given object is also a ManyToManyMap and their underlying MultiMaps represent the same mappings. If they're both ManyToManyMaps, it will run the MultiMap.equals(java.lang.Object) method on their underlying MultiMaps to compare their mappings.
      Overrides:
      equals in class Object
      Parameters:
      o - The object to be compared for equality with this map
      Returns:
      true if the given object is equivalent to this ManyToManyMap
    • containsKey

      public final boolean containsKey(K key)
      Returns true if this ManyToManyMap contains a mapping for the specified key. Calls MultiMap.containsKey(K) on the underlying keysToValues MultiMap to see if it contains the given key or not.
      Parameters:
      key - The key to check for
      Returns:
      Whether this ManyToManyMap contains the given key or not
    • containsValue

      public final boolean containsValue(V value)
      Returns true if this ManyToManyMap contains a mapping for the specified value. Calls MultiMap.containsKey(K) on the underlying valuesToKeys MultiMap to see if it contains the given value or not.
      Parameters:
      value - The value to check for
      Returns:
      Whether this ManyToManyMap contains the given value or not
    • getKeys

      public final List<K> getKeys(V value)
      Returns the list of keys to which the specified value is mapped, or null if this ManyToManyMap contains no mappings for the value. Calls MultiMap.get(K) on the underlying valuesToKeys MultiMap.
      Parameters:
      value - The value whose mapped keys are being requested
      Returns:
      The list of keys mapped to the given value
    • getValues

      public final List<V> getValues(K key)
      Returns the list of values to which the specified key is mapped, or null if this ManyToManyMap contains no mappings for the key. Calls MultiMap.get(K) on the underlying keysToValues MultiMap.
      Parameters:
      key - The key whose mapped values are being requested
      Returns:
      The list of values mapped to the given key
    • keySet

      public final Set<K> keySet()
      Returns the Set of keys in this ManyToManyMap. Calls MultiMap.keySet() on the underlying keysToValues MultiMap.
      Returns:
      The Set of keys in this ManyToManyMap
    • valueSet

      public final Set<V> valueSet()
      Returns the Set of values in this ManyToManyMap. Calls MultiMap.keySet() on the underlying valuesToKeys MultiMap.
      Returns:
      The Set of values in this ManyToManyMap
    • keysToValues

      public final MultiMap<K,V> keysToValues()
      Returns the underlying keysToValues MultiMap of this ManyToManyMap.
      Returns:
      The underlying keysToValues MultiMap
    • valuesToKeys

      public final MultiMap<V,K> valuesToKeys()
      Returns the underlying valuesToKeys MultiMap of this ManyToManyMap.
      Returns:
      The underlying valuesToKeys MultiMap
    • put

      public final void put(K key, V value)
      Associates the specified value with the specified key in this ManyToManyMap. Will call MultiMap.put(K, V) on both underlying MultiMaps.
      Parameters:
      key - The key to associate with the given value
      value - The value to associate with the given key
    • putAllKeys

      public final void putAllKeys(V value, List<K> keys)
      Associates all of the given keys with the given value. Calls MultiMap.putAll(Object, List) on the underlying valuesToKeys MultiMap and MultiMap.put(K, V) on the underlying keysToValues MultiMap for each of the given keys.
      Parameters:
      value - The value to associate with the given keys
      keys - The keys to associate with the given value
    • putAllValues

      public final void putAllValues(K key, List<V> values)
      Associates all of the given values with the given key. Calls MultiMap.putAll(Object, List) on the underlying keysToValues MultiMap and MultiMap.put(K, V) on the underlying valuesToKeys MultiMap for each of the given values.
      Parameters:
      key - The key to associate with the given values
      values - The values to associate with the given key
    • putAllKeyValMappings

      @SafeVarargs public final void putAllKeyValMappings(Pair<K,V>... entries)
      Puts all the given key-value Pairs into this ManyToManyMap.
      Calls MultiMap.putAll(Pair[]) on the underlying keysToValues MultiMap and MultiMap.put(K, V) on the underlying valuesToKeys MultiMap for each key-value mapping.
      Parameters:
      entries - The key-value Pairs to be put in this ManyToManyMap
    • putAllValKeyMappings

      @SafeVarargs public final void putAllValKeyMappings(Pair<V,K>... entries)
      Puts all the given value-key Pairs into this ManyToManyMap.
      Calls MultiMap.putAll(Pair[]) on the underlying valuesToKeys MultiMap and MultiMap.put(K, V) on the underlying keysToValues MultiMap for each value-key mapping.
      Parameters:
      entries - The value-key Pairs to be put in this ManyToManyMap
    • putAllKeyValMappings

      public final void putAllKeyValMappings(Map<K,V> map)
      Associates all of the key-value mappings from the given Map into this ManyToManyMap.
      Calls MultiMap.putAll(Map) on the underlying keysToValues MultiMap and MultiMap.put(K, V) on the underlying valuesToKeys MultiMap for each key-value mapping.
      Parameters:
      map - The map whose mappings should be added to this
    • putAllValKeyMappings

      public final void putAllValKeyMappings(Map<V,K> map)
      Associates all of the value-key mappings from the given Map into this ManyToManyMap.
      Calls MultiMap.putAll(Map) on the underlying valuesToKeys MultiMap and MultiMap.put(K, V) on the underlying keysToValues MultiMap for each key-value mapping.
      Parameters:
      map - The map whose mappings should be added to this
    • putAllKeyValMappings

      public final void putAllKeyValMappings(MultiMap<K,V> map)
      Associates all of the key-value mappings from the given MultiMap into this ManyToManyMap.
      Calls MultiMap.putAll(Map) on the underlying keysToValues MultiMap and MultiMap.put(K, V) on the underlying valuesToKeys MultiMap for each key-value mapping.
      Parameters:
      map - The MultiMap whose mappings should be added to this
    • putAllValKeyMappings

      public final void putAllValKeyMappings(MultiMap<V,K> map)
      Associates all of the value-key mappings from the given MultiMap into this ManyToManyMap.
      Calls MultiMap.putAll(Map) on the underlying valuesToKeys MultiMap and MultiMap.put(K, V) on the underlying keysToValues MultiMap for each key-value mapping.
      Parameters:
      map - The MultiMap whose mappings should be added to this
    • putAllKeyValMappings

      public final void putAllKeyValMappings(ManyToManyMap<K,V> map)
      Associates all of the key-value mappings from the given ManyToManyMap into this ManyToManyMap.
      Calls put(K, V) for each of the key-value mappings of the given ManyToManyMap.
      Parameters:
      map - The ManyToManyMap whose mappings should be added to this
    • putAllValKeyMappings

      public final void putAllValKeyMappings(ManyToManyMap<V,K> map)
      Associates all of the value-key mappings from the given ManyToManyMap into this ManyToManyMap.
      Calls put(K, V) for each of the value-key mappings of the given ManyToManyMap.
      Parameters:
      map - The ManyToManyMap whose mappings should be added to this
    • removeKey

      public final List<V> removeKey(K key)
      Removes all values associated with the given key from the ManyToManyMap. Calls MultiMap.removeKey(Object) on the underlying keysToValues MultiMap and then MultiMap.remove(Object, Object) on the underlying valuesToKeys MultiMap for each of the former key-value pairs being removed.
      Parameters:
      key - The key whose associations are to be removed
      Returns:
      The List of values the key used to be associated with
    • removeValue

      public final List<K> removeValue(V value)
      Removes all keys associated with the given value from the ManyToManyMap. Calls MultiMap.removeKey(Object) on the underlying valuesToKeys MultiMap and then MultiMap.remove(Object, Object) on the underlying keysToValues MultiMap for each of the former key-value pairs being removed.
      Parameters:
      value - The value whose associations are to be removed
      Returns:
      The List of keys the value used to be associated with
    • remove

      public final boolean remove(K key, V value)
      Removes the specified value from being associated with the specified key, if the mapping exists.
      Will return whether the mapping existed or not.

      Calls MultiMap.remove(Object, Object) on the underlying keysToValues MultiMap and if that returns true, calls the same method on the underlying valuesToKeys MultiMap to remove it entirely.
      Parameters:
      key - The key of the given key-value association to remove
      value - The value of the given key-value association to remove
      Returns:
      true if there was a mapping of the given key to the given value
    • removeEntireKeysList

      public final boolean removeEntireKeysList(V value, List<K> keys)
      Removes the given list of keys associated with the given value if the list of keys matches the current list associated with that value.
      Calls MultiMap.removeEntireList(K, java.util.List<V>) on the underlying valuesToKeys MultiMap. If that returns true, MultiMap.remove(Object, Object) is then called on the keysToValues MultiMap for each of the key-value mappings.
      Parameters:
      value - The value of the given value-key association to remove
      keys - The keys of the given value-key association to remove
      Returns:
      Whether the list of keys was removed or not
    • removeEntireValuesList

      public final boolean removeEntireValuesList(K key, List<V> values)
      Removes the given list of values associated with the given key if the list of values matches the current list associated with that key.
      Calls MultiMap.removeEntireList(K, java.util.List<V>) on the underlying keysToValues MultiMap. If that returns true, MultiMap.remove(Object, Object) is then called on the valuesToKeys MultiMap for each of the key-value mappings.
      Parameters:
      key - The key of the given key-value association to remove
      values - The values of the given key-value association to remove
      Returns:
      Whether the list of values was removed or not
    • replaceEntireKeyList

      public final List<K> replaceEntireKeyList(V value, List<K> keys)
      Replaces the current list of keys associated with the given value with the given list of keys, only if it is currently mapped to a value.
      Calls MultiMap.replaceEntireList(Object, List) on the underlying valuesToKeys MultiMap and then removes the old keys from the keysToValues MultiMap and puts the new keys in it.
      Parameters:
      value - The value to change the associations of
      keys - The keys to associate with the given value
      Returns:
      The previous list of keys associated with the given value
    • replaceEntireValueList

      public final List<V> replaceEntireValueList(K key, List<V> values)
      Replaces the current list of values associated with the given key with the given list of values.
      Calls MultiMap.replaceEntireList(Object, List) on the underlying keysToValues MultiMap and then removes the old values from the valuesToKeys MultiMap and puts the new values in it.
      Parameters:
      key - The key to change the associations of
      values - The values to associate with the given key
      Returns:
      The previous list of values associated with the given key
    • replaceKey

      public final boolean replaceKey(V value, K oldKey, K newKey)
      Replaces the given old key with the given new key for an association to the given value, if the old key is currently associated with the given value.
      Calls MultiMap.replace(K, V, V) on the underlying valuesToKeys MultiMap and if that returns true, it removes the old key from the keysToValues MultiMap and puts the new key in it at the end of the list.
      Parameters:
      value - The value to change the association of
      oldKey - The old key associated with the given value
      newKey - The new key to associate with the given value
      Returns:
      true if the key was replaced
    • replaceValue

      public final boolean replaceValue(K key, V oldValue, V newValue)
      Replaces the given old value with the given new value for an association to the given key, if the old value is currently associated with the given key.
      Calls MultiMap.replace(K, V, V) on the underlying keysToValues MultiMap and if that returns true, it removes the old value from the valuesToKeys MultiMap and puts the new value in it at the end of the list.
      Parameters:
      key - The key to change the association of
      oldValue - The old value associated with the given key
      newValue - The new value to associate with the given key
      Returns:
      true if the value was replaced
    • replaceEntireKeyList

      public final boolean replaceEntireKeyList(V value, List<K> oldKeys, List<K> newKeys)
      Replaces the current list of keys associated with the given value with the given new list of keys if the given old list matches the current list.
      Calls MultiMap.replaceEntireList(K, java.util.List<V>, java.util.List<V>) on the underlying valuesToKeys MultiMap and if that returns true, it then removes the old key-value mappings from the keysToValues MultiMap and puts the new key-value mappings in it.
      Parameters:
      value - The value to change the associations of
      oldKeys - The old list of keys associated with the given value
      newKeys - The keys to associate with the given value
      Returns:
      true if the keys were replaced
    • replaceEntireValueList

      public final boolean replaceEntireValueList(K key, List<V> oldValues, List<V> newValues)
      Replaces the current list of values associated with the given key with the given new list of values if the given old list matches the current list.
      Calls MultiMap.replaceEntireList(K, java.util.List<V>, java.util.List<V>) on the underlying keysToValues MultiMap and if that returns true, it then removes the old value-key mappings from the valuesToKeys MultiMap and puts the new value-key mappings in it.
      Parameters:
      key - The key to change the associations of
      oldValues - The old list of values associated with the given key
      newValues - The values to associate with the given key
      Returns:
      true if the values were replaced
    • keySetSize

      public final int keySetSize()
      Returns the number of keys currently in this ManyToManyMap.
      Calls MultiMap.keySetSize() on the underlying keysToValues MultiMap.
      Returns:
      The number of keys currently in this ManyToManyMap
    • valueSetSize

      public final int valueSetSize()
      Returns the number of values currently in this ManyToManyMap.
      Calls MultiMap.keySetSize() on the underlying valuesToKeys MultiMap.
      Returns:
      The number of values currently in this ManyToManyMap
    • size

      public final int size()
      Returns the number of key-value associations of this ManyToManyMap.
      Calls MultiMap.size() on the underlying keysToValues MultiMap.
      Returns:
      The number of key-value associations of this ManyToManyMap.
    • forEach

      public final void forEach(BiConsumer<? super K,? super V> action)
      Performs the given action for each key-value association in this ManyToManyMap until all associations have been processed or the action throws an exception.
      Parameters:
      action - The action to be performed for each key-value association
    • clear

      public final void clear()
      Removes all of the key-value associations from this ManyToManyMap. The ManyToManyMap will be empty after this call returns.
      Calls MultiMap.clear() on the underlying MultiMaps.