Class IdentifiableMap<T extends Identifiable>

java.lang.Object
org.tentackle.misc.IdentifiableMap<T>
Type Parameters:
T - the identifiable type
All Implemented Interfaces:
Serializable

public class IdentifiableMap<T extends Identifiable> extends Object implements Serializable
A map of Identifiables.

By default, the map uses a hashing key composed of the class and object id. The first invocation of one of the methods getAllSorted() or getAllSorted(Class) creates an additional sorted map, which is maintained from then on (and makes modifications a bit slower).

The map is not thread-safe and is meant to be built and consumed by a single thread, typically to collect the components of an aggregate. Sharing it between threads requires external synchronization: already a single add updates two maps non-atomically, getAllSorted lazily creates the sorted one, and getAll() hands out a live view.

Author:
harald
See Also:
  • Constructor Details

    • IdentifiableMap

      public IdentifiableMap()
      Creates an Identifiable map.
    • IdentifiableMap

      public IdentifiableMap(T object)
      Creates an object map with a single object.
      Parameters:
      object - the object to add
  • Method Details

    • clear

      public void clear()
      Clears the map.
      Also drops the sorted map.
    • add

      public T add(T object)
      Adds an object to the map.
      Parameters:
      object - the object to add
      Returns:
      the replaced object, null if added
    • add

      public int add(Collection<? extends T> objects)
      Adds a collection to the map.

      Objects already in the map are replaced but not counted.

      Parameters:
      objects - the collection
      Returns:
      the number of objects added
    • remove

      public <P extends T> P remove(Class<P> clazz, long id)
      Removes an object from the map.
      Type Parameters:
      P - the class type
      Parameters:
      clazz - the object class
      id - the object ID
      Returns:
      the removed object, null if not in the map
    • remove

      public T remove(T object)
      Removes an object from the map.
      Parameters:
      object - the object to remove
      Returns:
      the removed object, null if not in the map
    • remove

      public int remove(Collection<? extends T> objects)
      Removes a collection from the map.

      Objects not in the map are ignored and not counted.

      Parameters:
      objects - the object collection to remove
      Returns:
      the number of objects removed
    • get

      public <P extends T> P get(Class<P> clazz, long id)
      Retrieves an object from the map.
      Type Parameters:
      P - the class type
      Parameters:
      clazz - the object class
      id - the object ID
      Returns:
      the object, null if not in map
    • getAll

      public Collection<T> getAll()
      Gets all objects in the map.

      The returned objects are in the order they were added.

      Returns:
      all objects (a live view)
    • getAllSorted

      public Collection<T> getAllSorted()
      Gets all objects sorted by class name and ID.
      Returns:
      all objects (a live view)
    • getAllSorted

      public <P extends T> Collection<P> getAllSorted(Class<P> clazz)
      Gets all objects for a given class.
      Objects of subclasses are not included.
      Type Parameters:
      P - the given class
      Parameters:
      clazz - the object class
      Returns:
      the objects sorted by ID, empty collection if none