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 by the class and object id. The first invocation of one of the methods getAllSorted() or getAllSorted(Class) changes the map to treemap (which is a bit slower).

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.
    • 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 appended.

      Returns:
      all objects
    • getAllSorted

      public Collection<T> getAllSorted()
      Gets all objects sorted by class + ID.
      Returns:
      all objects
    • getAllSorted

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