Class ObjIdBiMultiMap

java.lang.Object
io.permazen.core.util.ObjIdBiMultiMap
All Implemented Interfaces:
Serializable, Cloneable

@ThreadSafe public class ObjIdBiMultiMap extends Object implements Cloneable, Serializable
A bi-directional, many-to-many mapping between ObjIds.

Instances can be thought of as containing a set of source, target ordered pairs. The many-to-many mapping can be efficiently queried and modified from either direction.

Instances of this class are thread-safe.

See Also:
  • Constructor Details

    • ObjIdBiMultiMap

      public ObjIdBiMultiMap()
      Default constructor.
    • ObjIdBiMultiMap

      public ObjIdBiMultiMap(int sourceCapacity, int targetCapacity)
      Constructs an instance with the given initial capacities.
      Parameters:
      sourceCapacity - initial capacity for the number of sources
      targetCapacity - initial capacity for the number of targets
      Throws:
      IllegalArgumentException - if either value is negative
  • Method Details

    • getNumSources

      public int getNumSources()
      Get the the number of sources that have one or more associated targets.
      Returns:
      the number of sources contained in this instance
    • getNumTargets

      public int getNumTargets()
      Get the the number of targets that have one or more associated sources.
      Returns:
      the number of targets contained in this instance
    • getSources

      public ObjIdSet getSources()
      Get all sources associated with this instance that have one or more associated targets.

      The returned ObjIdSet is mutable, but changes to it do not affect this instance.

      Returns:
      all source ID's associated with this instance, possibly empty
    • getTargets

      public ObjIdSet getTargets()
      Get all targets associated with this instance that have one or more associated sources.

      The returned ObjIdSet is mutable, but changes to it do not affect this instance.

      Returns:
      all targets ID's associated with this instance, possibly empty
    • getSources

      public ObjIdSet getSources(ObjId target)
      Get the sources associated with the given target, if any.

      The returned ObjIdSet is mutable, but changes to it do not affect this instance.

      Parameters:
      target - target ID
      Returns:
      one or more source ID's associated with target, or null if there are none
      Throws:
      IllegalArgumentException - if target is null
    • getTargets

      public ObjIdSet getTargets(ObjId source)
      Get the targets associated with the given source, if any.

      The returned ObjIdSet is mutable, but changes to it do not affect this instance.

      Parameters:
      source - source ID
      Returns:
      one or more target ID's associated with source, or null if there are none
      Throws:
      IllegalArgumentException - if source is null
    • containsSource

      public boolean containsSource(ObjId source)
      Determine if this instance has any targets associated with the specified source.
      Parameters:
      source - source ID
      Returns:
      true if any target ID's are associated with source, otherwise false
      Throws:
      IllegalArgumentException - if source is null
    • containsTarget

      public boolean containsTarget(ObjId target)
      Determine if this instance has any sources associated with the specified target.
      Parameters:
      target - target ID
      Returns:
      true if any source ID's are associated with target, otherwise false
      Throws:
      IllegalArgumentException - if target is null
    • add

      public boolean add(ObjId source, ObjId target)
      Add an association.
      Parameters:
      source - source ID
      target - target ID
      Returns:
      true if association was added, false if the association already existed
      Throws:
      IllegalArgumentException - if source or target is null
    • addAll

      public boolean addAll(ObjId source, Iterable<? extends ObjId> targets)
      Add multiple associations with a given source.
      Parameters:
      source - source ID
      targets - target ID's
      Returns:
      true if any association was added, false if all associations already existed
      Throws:
      IllegalArgumentException - if source, targets, or any target in the iteration is null
    • remove

      public boolean remove(ObjId source, ObjId target)
      Remove an association.
      Parameters:
      source - source ID
      target - target ID
      Returns:
      true if association was removed, false if the association did not exist
      Throws:
      IllegalArgumentException - if source or target is null
    • removeAll

      public boolean removeAll(ObjId source, Iterable<? extends ObjId> targets)
      Remove multiple associations with a given source.
      Parameters:
      source - source ID
      targets - target ID's
      Returns:
      true if any association was removed, false if none of the specified associations existed
      Throws:
      IllegalArgumentException - if source, targets, or any target in the iteration is null
    • removeSource

      public boolean removeSource(ObjId source)
      Remove all associations involving the specified source.
      Parameters:
      source - source ID
      Returns:
      true if any source association(s) were removed, false if source had no target associations
      Throws:
      IllegalArgumentException - if source is null
    • removeTarget

      public boolean removeTarget(ObjId target)
      Remove all associations involving the specified target.
      Parameters:
      target - target ID
      Returns:
      true if any target association(s) were removed, false if target had no source associations
      Throws:
      IllegalArgumentException - if target is null
    • clear

      public void clear()
      Clear this instance.
    • inverse

      public ObjIdBiMultiMap inverse()
      Get an inverse view backed by this instance.

      The returned ObjIdBiMultiMap is a view in which sources become targets and vice-versa; any changes are reflected back in this instance.

      This method is efficient, requiring only constant time.

      Returns:
      inverse view of this instance
    • hashCode

      public int hashCode()
      Calculate a hash code value for this instance.
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Compare for equality.
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Create a String representation.
      Overrides:
      toString in class Object
    • clone

      public ObjIdBiMultiMap clone()
      Clone this instance.
      Overrides:
      clone in class Object