Class IdentifiableMap<T extends Identifiable>
- Type Parameters:
T- the identifiable type
- All Implemented Interfaces:
Serializable
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 Summary
ConstructorsConstructorDescriptionCreates anIdentifiablemap.IdentifiableMap(T object) Creates an object map with a single object. -
Method Summary
Modifier and TypeMethodDescriptionintadd(Collection<? extends T> objects) Adds a collection to the map.Adds an object to the map.voidclear()Clears the map.
Also drops the sorted map.<P extends T>
PRetrieves an object from the map.getAll()Gets all objects in the map.Gets all objects sorted by class name and ID.<P extends T>
Collection<P> getAllSorted(Class<P> clazz) Gets all objects for a given class.
Objects of subclasses are not included.<P extends T>
PRemoves an object from the map.intremove(Collection<? extends T> objects) Removes a collection from the map.Removes an object from the map.
-
Constructor Details
-
IdentifiableMap
public IdentifiableMap()Creates anIdentifiablemap. -
IdentifiableMap
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
-
add
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
-
remove
-
remove
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
-
getAll
Gets all objects in the map.The returned objects are in the order they were added.
- Returns:
- all objects (a live view)
-
getAllSorted
Gets all objects sorted by class name and ID.- Returns:
- all objects (a live view)
-
getAllSorted
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
-