Class MapMultimap<A,B>

java.lang.Object
eu.bandm.tools.util.multi.MapMultimap<A,B>
Type Parameters:
A - type of the domain elements
B - type of the range elements
All Implemented Interfaces:
Multimap<A,B>, Serializable, Iterable<Map.Entry<A,B>>, Collection<Map.Entry<A,B>>, BiPredicate<A,B>, Set<Map.Entry<A,B>>
Direct Known Subclasses:
HashMultimap

public abstract class MapMultimap<A,B> extends Object implements Multimap<A,B>, Serializable
Generic implementation using maps of sets in both directions. The particular kind (implementation) of the maps is determined by the subclasses.
See Also:
  • Field Details

    • forward

      protected final Map<A,Set<B>> forward
      The map from all contained domain elements to the set of range elements which point to it. These sets are never empty.
    • backward

      protected final Map<B,Set<A>> backward
      The map from all contained range elements to the set of domain elements which point to it. These sets are never empty.
    • size

      protected int size
      The number of pairs currently realized by this instance.
  • Constructor Details

    • MapMultimap

      protected MapMultimap(Map<A,Set<B>> forward, Map<B,Set<A>> backward, int size)
      May not be called by the user. May only called by constructors of subclasses, because all arguments must be in a consistent state.
      Parameters:
      forward - the map from domain elements to sets of range elements.
      backward - the map from range elements to sets of domain elements.
      size - the element pairs contained in the state defined by the given maps.
  • Method Details

    • createForwardSet

      protected abstract Set<B> createForwardSet()
      Creates the image set for every domain element added.

      Subclasses implementing this method must ensure that when the map A->Set<B> is sorted, then this set also is.

      Returns:
      a sets for range domain elements assigned to one particular domain element.
    • createBackwardSet

      protected abstract Set<A> createBackwardSet()
      Creates the preimage set for every range element added.

      Subclasses implementing this method must ensure that when the map B->Set<A> is sorted, then this set also is.

      Returns:
      a sets for the domain elements assigned to one particular range element.
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<A>
      Specified by:
      isEmpty in interface Set<A>
    • add

      public final boolean add(Map.Entry<A,B> e)

      This implementation calls add(Object,Object), because individual map entries are not used to hold pairs.

      Specified by:
      add in interface Collection<A>
      Specified by:
      add in interface Set<A>
    • add

      public boolean add(A a, B b)
      Adds a pair to this multimap. This operation must be stable: adding a pair that equals any pair contained in this multimap componentwise must not change this multimap.

      This implementation works directly on internal maps of sets, and is the only way to actually effect the adding. Subclasses should override this method only to impose additional checks.

      Specified by:
      add in interface Multimap<A,B>
      Parameters:
      a - the left component of the pair to add
      b - the right component of the pair to add
      Returns:
      whether this multimap has been changed by this operation
    • remove

      public boolean remove(Object o)
      Specified by:
      remove in interface Collection<A>
      Specified by:
      remove in interface Set<A>
    • remove

      public boolean remove(A a, B b)
      Description copied from interface: Multimap
      Removes a pair from this multimap. Removing a pair that equals no pair contained in this multimap componentwise must not change this multimap.
      Specified by:
      remove in interface Multimap<A,B>
      Parameters:
      a - the left component of the pair to remove.
      b - the right component of the pair to remove.
      Returns:
      whether this multimap has been changed by this operation
    • contains

      public boolean contains(Object o)
      Specified by:
      contains in interface Collection<A>
      Specified by:
      contains in interface Set<A>
    • contains

      public boolean contains(A a, B b)
      Description copied from interface: Multimap
      Whether this multimap contains a pair with the given components.
      Specified by:
      contains in interface Multimap<A,B>
      Parameters:
      a - the left component of the pair to add.
      b - the right component of the pair to add.
      Returns:
      whether this multimap contains a pair with the given components.
    • domain

      public Set<A> domain()
      Description copied from interface: Multimap
      Returns the set of left components of pairs in this multimap.

      The returned set is backed by this multimap, such that changes to this multimap are reflected by the set. The behavior of modifications of this multimap concurrent to iteration of the set is unspecified.

      If this multimap supports the removeDomain operation, then the returned set and its iterators must support the remove operation, and changes by these operations must be reflected by this multimap.

      Specified by:
      domain in interface Multimap<A,B>
      Returns:
      the set of left components of pairs in this multimap.
    • range

      public Set<B> range()
      Description copied from interface: Multimap
      Returns the set of right components of pairs in this multimap.

      The returned set is backed by this multimap, such that changes to this multimap are reflected by the set. The behavior of modifications of this multimap concurrent to iteration of the set is unspecified.

      If this multimap supports the removeRange operation, then the returned set and its iterators must support the remove operation, and changes by these operations must be reflected by this multimap.

      Specified by:
      range in interface Multimap<A,B>
      Returns:
      the set of right components of pairs in this multimap.
    • removeDomain

      public boolean removeDomain(A a)
      Description copied from interface: Multimap
      Removes all pairs with a given left component from this multimap. The effect of the implementation on this multimap must be equivalent to the following code:
       removeDomain(final A a) {
         for (B b : new HashSet<B>(range())) remove(a, b) ;
       }
       
      Specified by:
      removeDomain in interface Multimap<A,B>
      Parameters:
      a - the left component of all pairs to remove.
      Returns:
      whether this multimap has been changed by this operation
    • removeRange

      public boolean removeRange(B b)
      Description copied from interface: Multimap
      Removes all pairs with a given right component from this multimap. The effect of the implementation on this multimap must be equivalent to the following code:
       removeRange(final B b) {
         for (A a : new HashSet<A>(domain())) remove(a, b) ;
       }
       
      Specified by:
      removeRange in interface Multimap<A,B>
      Parameters:
      b - the right component of all pairs to remove.
      Returns:
      whether this multimap has been changed by this operation
    • removeAllDomain

      public boolean removeAllDomain(Collection<? extends A> c)
      Description copied from interface: Multimap
      Removes all pairs with a left component in the given collection from this multimap.
      Specified by:
      removeAllDomain in interface Multimap<A,B>
      Parameters:
      c - the set of left component of all pairs to remove.
      Returns:
      whether this multimap has been changed by this operation
    • retainAllDomain

      public boolean retainAllDomain(Collection<? extends A> c)
      Description copied from interface: Multimap
      Removes from this multimap all pairs with a left component which is not in the given collection.
      Specified by:
      retainAllDomain in interface Multimap<A,B>
      Parameters:
      c - the set of left component of all pairs not to remove.
      Returns:
      whether this multimap has been changed by this operation
    • removeAllRange

      public boolean removeAllRange(Collection<? extends B> c)
      Description copied from interface: Multimap
      Removes all pairs with a right component in the given collection from this multimap.
      Specified by:
      removeAllRange in interface Multimap<A,B>
      Parameters:
      c - the set of the right component of all pairs to remove.
      Returns:
      whether this multimap has been changed by this operation
    • retainAllRange

      public boolean retainAllRange(Collection<? extends B> c)
      Description copied from interface: Multimap
      Removes from this multimap all pairs with a right component which is not in the given collection.
      Specified by:
      retainAllRange in interface Multimap<A,B>
      Parameters:
      c - the set of the right component of all pairs not emove.
      Returns:
      whether this multimap has been changed by this operation
    • image

      public Set<B> image(A a)
      Description copied from interface: Multimap
      Returns the set of right components of pairs with a given left component in this multimap.

      The returned set need not be modifiable. The behaviour of this multimap after successfully modifying the returned set is unspecified.

      Specified by:
      image in interface Multimap<A,B>
      Parameters:
      a - a left component.
      Returns:
      the set of right components of pairs with a left component that equals(a) in this multimap.
    • imageUnchecked

      public Set<B> imageUnchecked(Object a)
      Description copied from interface: Multimap
      Convenience method for untyped inquiry.
      Specified by:
      imageUnchecked in interface Multimap<A,B>
      Parameters:
      a - a left component.
      Returns:
      the set of right components of pairs with a left component that equals(a) in this multimap.
      See Also:
    • imageMap

      public Map<A,Set<B>> imageMap()
      Description copied from interface: Multimap
      Returns a map which maps each A which appears as a left component to a set of all B which appear as its right component. The result is backed by the argument. Subsequent changes to either Collection are reflected by the other.
      Specified by:
      imageMap in interface Multimap<A,B>
      Returns:
      a map which maps each A which appears as a left component to a set of all B which appear as its right component.
    • preimage

      public Set<A> preimage(B b)
      Description copied from interface: Multimap
      Returns the set of left components of pairs with a given right component in this multimap.

      The returned set need not be modifiable. The behaviour of this multimap after successfully modifying the returned set is unspecified.

      Specified by:
      preimage in interface Multimap<A,B>
      Parameters:
      b - a right component.
      Returns:
      the set of left components of pairs with a right component that equals(b) in this multimap.
    • preimageUnchecked

      public Set<A> preimageUnchecked(Object b)
      Description copied from interface: Multimap
      Convenience method for untyped inquiry.
      Specified by:
      preimageUnchecked in interface Multimap<A,B>
      Parameters:
      b - a right component.
      Returns:
      the set of left components of pairs with a right component that equals(b) in this multimap.
      See Also:
    • preimageMap

      public Map<B,Set<A>> preimageMap()
      Description copied from interface: Multimap
      Returns a map which maps each B which appears as a right component to a set of all A which appear as its left component. The result is backed by the argument. Subsequent changes to either Collection are reflected by the other.
      Specified by:
      preimageMap in interface Multimap<A,B>
      Returns:
      a map which maps each B which appears as a right component to a set of all A which appear as its left component.
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<A>
      Specified by:
      clear in interface Set<A>
    • size

      public int size()
      Specified by:
      size in interface Collection<A>
      Specified by:
      size in interface Set<A>
    • iterator

      public Iterator<Map.Entry<A,B>> iterator()
      Specified by:
      iterator in interface Collection<A>
      Specified by:
      iterator in interface Iterable<A>
      Specified by:
      iterator in interface Set<A>
    • toString

      public String toString()
      Returns a string representation of this instance. Calls Multimap.toString(String) with Multimap.DEFAULT_PAIR_FORMAT_STRING.
      Overrides:
      toString in class Object