Class MapMultimap<A,B>

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 AbstractMultimap<A,B> implements Serializable
Generic implementation using maps of sets in both directions. The particular kind/implementaion of the maps is determined by the derived classes.
See Also:
  • Field Details

    • forward

      protected final Map<A,Set<B>> forward
    • backward

      protected final Map<B,Set<A>> backward
    • size

      protected int size
  • Constructor Details

    • MapMultimap

      protected MapMultimap(Map<A,Set<B>> forward, Map<B,Set<A>> backward, int size)
  • Method Details

    • createForwardSet

      protected abstract Set<B> createForwardSet()
    • createBackwardSet

      protected abstract Set<A> createBackwardSet()
    • add

      public final boolean add(Map.Entry<A,B> e)
      The method uses directly @link{#add(A,B)}. When a derived class applies checks on the arguments of the add method, these checks are also applied here.
      Specified by:
      add in interface Collection<A>
      Specified by:
      add in interface Set<A>
      Overrides:
      add in class AbstractCollection<Map.Entry<A,B>>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<A>
      Specified by:
      clear in interface Set<A>
      Overrides:
      clear in class AbstractCollection<Map.Entry<A,B>>
    • contains

      public boolean contains(Object o)
      Specified by:
      contains in interface Collection<A>
      Specified by:
      contains in interface Set<A>
      Overrides:
      contains in class AbstractCollection<Map.Entry<A,B>>
    • remove

      public boolean remove(Object o)
      Specified by:
      remove in interface Collection<A>
      Specified by:
      remove in interface Set<A>
      Overrides:
      remove in class AbstractCollection<Map.Entry<A,B>>
    • size

      public int size()
      Specified by:
      size in interface Collection<A>
      Specified by:
      size in interface Set<A>
      Specified by:
      size in class AbstractCollection<Map.Entry<A,B>>
    • 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>
      Specified by:
      iterator in class AbstractCollection<Map.Entry<A,B>>
    • add

      public boolean add(A a, B b)
      Description copied from interface: Multimap
      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.
      Specified by:
      add in interface Multimap<A,B>
      Overrides:
      add in class AbstractMultimap<A,B>
      Parameters:
      a - the left component of the pair to add.
      b - the right component of the pair to add.
      Returns:
      true if this multimap has been changed by this operation, false otherwise.
    • remove

      public boolean remove(A a, B b)
      Description copied from interface: Multimap
      Removes a pair from this 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>
      Overrides:
      remove in class AbstractMultimap<A,B>
      Parameters:
      a - the left component of the pair to remove.
      b - the right component of the pair to remove.
      Returns:
      true if this multimap has been changed by this operation, false otherwise.
    • 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>
      Overrides:
      removeDomain in class AbstractMultimap<A,B>
      Parameters:
      a - the left component of all pairs to remove.
      Returns:
      true if this multimap has been changed by this operation, false otherwise.
    • 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>
      Overrides:
      removeRange in class AbstractMultimap<A,B>
      Parameters:
      b - the right component of all pairs to remove.
      Returns:
      true if this multimap has been changed by this operation, false otherwise.
    • 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>
      Overrides:
      removeAllDomain in class AbstractMultimap<A,B>
    • retainAllDomain

      public boolean retainAllDomain(Collection<? extends A> c)
      Description copied from interface: Multimap
      Removes all pairs with a left component which is not the given collection from this multimap.
      Specified by:
      retainAllDomain in interface Multimap<A,B>
      Overrides:
      retainAllDomain in class AbstractMultimap<A,B>
    • 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>
      Overrides:
      removeAllRange in class AbstractMultimap<A,B>
    • retainAllRange

      public boolean retainAllRange(Collection<? extends B> c)
      Description copied from interface: Multimap
      Removes all pairs with a right component which is not the given collection from this multimap.
      Specified by:
      retainAllRange in interface Multimap<A,B>
      Overrides:
      retainAllRange in class AbstractMultimap<A,B>
    • 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>
      Overrides:
      contains in class AbstractMultimap<A,B>
      Parameters:
      a - the left component of the pair to add.
      b - the right component of the pair to add.
    • 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>
      Overrides:
      domain in class AbstractMultimap<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.

      If this multimap supports the removeRange operation, then the returned set and its iterators must support the remove operation.

      Specified by:
      range in interface Multimap<A,B>
      Overrides:
      range in class AbstractMultimap<A,B>
      Returns:
      the set of right components of pairs in this multimap.
    • 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>
      Overrides:
      image in class AbstractMultimap<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)
      Specified by:
      imageUnchecked in interface Multimap<A,B>
      Overrides:
      imageUnchecked in class AbstractMultimap<A,B>
      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.
      Specified by:
      imageMap in interface Multimap<A,B>
      Overrides:
      imageMap in class AbstractMultimap<A,B>
    • 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>
      Overrides:
      preimage in class AbstractMultimap<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)
      Specified by:
      preimageUnchecked in interface Multimap<A,B>
      Overrides:
      preimageUnchecked in class AbstractMultimap<A,B>
      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.
      Specified by:
      preimageMap in interface Multimap<A,B>
      Overrides:
      preimageMap in class AbstractMultimap<A,B>