Package org.faktorips.util
Class ClassToInstancesMap<T>
java.lang.Object
org.faktorips.util.ClassToInstancesMap<T>
This class is a map with a class as key and a collection of instances of this class as value. The
class does NOT implements the
Map interface because this interface does not set the
correct generics. But this class does implements the same methods like Map except of the
generics.
The generic Type T represents the base class used in this map. All keys have to be a subclass or the same class.
Remember that this map does only handles a bunch of list with the same superclass. Changing any
of these lists does also change the list in the map. Only the method values() does
create a new List containing every object of the values. All other methods never create list
copies.
- Author:
- dirmeier
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Remove every object in the map.booleancontainsValue(Object value) Check if the map contains the value.booleancontainsValuesOf(Class<? extends T> type) Check if the map contains a list of values for the specified type.Getting the list of instances stored of the type given by the key.booleanisEmpty()Return true if this map is emptyPutting the value to the classes of key.put(K value) Putting the value to the classes of its type.putWithRuntimeCheck(Class<? extends T> key, T value) This method puts the value into the list specified by the key class.booleanRemove a single element from the list and returns true if it was removed successfully.Remove all objects for a given key and returns the list of removed objects.intsize()The whole size of the map that means how many instances are stored in this map.<K extends T>
intGetting the number of objects stored for the specified key.values()This method returns all values in one collection.
-
Constructor Details
-
ClassToInstancesMap
public ClassToInstancesMap()
-
-
Method Details
-
get
Getting the list of instances stored of the type given by the key. If there is no element for this key this method would return an empty list. Never return null.- Parameters:
key- The class of the instances you want to get- Returns:
- A list of instances of the given class
-
put
Putting the value to the classes of its type. Use this method only if you want to put the instance to exactly its implementation class used as key. Useput(Class, Object)if you want to store the instance for example with its interface used as the key. Calling this method multiple times with the same key value pair would add a new value every time. That means you could have the same instance multiple time.- Parameters:
value- the value you want to add- Returns:
- the list of all values already added to the map for the given key including the new one.
-
put
Putting the value to the classes of key. Calling this method multiple times with the same key value pair would add a new value every time. That means you could have the same instance multiple time.- Parameters:
key- The key specifying the class of the valuevalue- the value you want to add- Returns:
- the list of all values already added to the map for the given key including the new one.
-
putWithRuntimeCheck
This method puts the value into the list specified by the key class. Use this method only if you do not know the concrete class at compile time, useput(Class, Object)instead. If you do not have the information about key and value you could use this put method. The type check is done at runtime, this method would throw aRuntimeExceptionin case of type mismatch.- Parameters:
key- The class that identifies the list of valuesvalue- the value you want to add to this map- Returns:
- the list of values of type key after adding the value
-
size
public int size()The whole size of the map that means how many instances are stored in this map. If you want to know how many objects are stored of one type usesize(Class).- Returns:
- The number of discrete values stored in this map.
-
size
Getting the number of objects stored for the specified key.- Parameters:
key- The class you want to get the number of instances for- Returns:
- the number of values stored for the specified class.
-
isEmpty
public boolean isEmpty()Return true if this map is empty- Returns:
- true if the map is empty or false if there is at least one element.
-
containsValue
Check if the map contains the value. This method running over all value lists searching for the specified value.- Parameters:
value- The value you want to know if it is contained- Returns:
- true if the value is in the list or false if not
-
containsValuesOf
Check if the map contains a list of values for the specified type.- Parameters:
type- The type for which we check if there is a list of values for- Returns:
- true if there is a list of values of the specified type
-
remove
Remove a single element from the list and returns true if it was removed successfully. If the list containing the object multiple times only the fist one will be removed!- Parameters:
key- The key of the list that should contain the objectobject- the object you want to remove- Returns:
- true if the object was found in the list.
-
removeAll
Remove all objects for a given key and returns the list of removed objects. The returned list is a copy of the original included list, changing this list does not matter fot this map.- Parameters:
key- The class of the objects you want to remove- Returns:
- the list of removed objects
-
clear
public void clear()Remove every object in the map. -
values
This method returns all values in one collection. The order of the lists of different classes is always the same.- Returns:
- All values stored in this map in one collection.
-