Class AbstractMultimap<A,B>

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

public abstract class AbstractMultimap<A,B> extends AbstractCollection<Map.Entry<A,B>> implements Multimap<A,B>
Abstract base class for multimap implementations.

To implement an unmodifiable multimap, subclasses need only to implement the methods AbstractCollection.iterator() and AbstractCollection.size().

To implement a modifiable collection, subclasses must also override the method AbstractCollection.add(Object), and the iterators return by AbstractCollection.iterator() must override the method Iterator.remove().

Subclasses that do not use Map.Entry objects for internal storage are encouraged to override the methods add(Object,Object), remove(Object,Object), contains(Object,Object), Multimap.removeUnchecked(Object,Object) and Multimap.containsUnchecked(Object,Object).

Every subclass should define a copying constructor.

  • Field Details

    • abstractMultimap

      final AbstractMultimap<A,B> abstractMultimap
      For to make "this" accessible by deeply nested anonymous classes.
  • Constructor Details

    • AbstractMultimap

      protected AbstractMultimap()
      Creates a new instance.
  • Method Details

    • add

      public boolean add(A a, B b)
      Adds a pair to this multimap if not already present.

      The default implementation calls AbstractCollection.add(E) with a newly created map entry to hold the pair. Subclasses may override this method for a more efficient implementation.

      Specified by:
      add in interface Multimap<A,B>
      Parameters:
      a - the left element of the pair
      b - the right element of the pair
      Returns:
      true if this multimap has changed. Otherwise false, because it already contained the pair
      See Also:
    • 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(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.
    • 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.
    • 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 AbstractCollection<Map.Entry<A,B>>