Class ClassToInstancesMap<T>

java.lang.Object
org.faktorips.util.ClassToInstancesMap<T>

public class ClassToInstancesMap<T> extends Object
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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Remove every object in the map.
    boolean
    Check if the map contains the value.
    boolean
    containsValuesOf(Class<? extends T> type)
    Check if the map contains a list of values for the specified type.
    <K extends T>
    List<K>
    get(Class<K> key)
    Getting the list of instances stored of the type given by the key.
    boolean
    Return true if this map is empty
    <K extends T>
    List<K>
    put(Class<K> key, K value)
    Putting the value to the classes of key.
    <K extends T>
    List<K>
    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.
    boolean
    remove(Class<? extends T> key, T object)
    Remove a single element from the list and returns true if it was removed successfully.
    <K extends T>
    List<K>
    removeAll(Class<K> key)
    Remove all objects for a given key and returns the list of removed objects.
    int
    The whole size of the map that means how many instances are stored in this map.
    <K extends T>
    int
    size(Class<K> key)
    Getting the number of objects stored for the specified key.
    This method returns all values in one collection.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ClassToInstancesMap

      public ClassToInstancesMap()
  • Method Details

    • get

      public <K extends T> List<K> get(Class<K> key)
      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

      public <K extends T> List<K> put(K value)
      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. Use put(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

      public <K extends T> List<K> put(Class<K> key, K value)
      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 value
      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.
    • putWithRuntimeCheck

      public List<T> putWithRuntimeCheck(Class<? extends T> key, T value)
      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, use put(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 a RuntimeException in case of type mismatch.
      Parameters:
      key - The class that identifies the list of values
      value - 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 use size(Class).
      Returns:
      The number of discrete values stored in this map.
    • size

      public <K extends T> int size(Class<K> key)
      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

      public boolean containsValue(Object value)
      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

      public boolean containsValuesOf(Class<? extends T> type)
      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

      public boolean remove(Class<? extends T> key, T object)
      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 object
      object - the object you want to remove
      Returns:
      true if the object was found in the list.
    • removeAll

      public <K extends T> List<K> removeAll(Class<K> key)
      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

      public List<T> 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.