Class IndexMap<K,V>

java.lang.Object
jadex.collection.IndexMap<K,V>
All Implemented Interfaces:
Serializable, Cloneable
Direct Known Subclasses:
IndexMap.ListIndexMap, IndexMap.MapIndexMap

public class IndexMap<K,V> extends Object implements Serializable, Cloneable
This class combines the list and map interface. So it provides fast lookup (via map) and ordering (via list). Because the remove(Object) method has different return types in map and list, index map cannot implement both interfaces. Methods are provided to get a map instance and a list instance of an index map.
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Provide access to the index map via list interface.
    static class 
    Provide access to the index map via map interface.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected List<V>
    The index map as java.util.List.
    protected Map<K,V>
    The index map as java.util.Map.
    protected List<K>
    The key list.
    protected Map<K,V>
    The key/value map.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new index map.
    Create a new index map.
    IndexMap(List<K> list, Map<K,V> map)
    Create a new index map using the specified collections as backup.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(int index, Object element)
    Unsupported method, due to missing key parameter.
    void
    add(int index, K key, V o)
    Add an object to the collection.
    boolean
    Unsupported method, due to missing key parameter.
    void
    add(K key, V o)
    Add a new object with key and value.
    boolean
    addAll(int index, Collection c)
    Unsupported method, due to missing key parameter.
    boolean
    Unsupported method, due to missing key parameter.
    void
    Removes all mappings from this map.
    Clone an index map.
    boolean
    Returns true if this list contains the specified element.
    boolean
    Returns true if this list contains all of the elements of the specified collection.
    boolean
    Returns true if this map contains a mapping for the specified key.
    boolean
    Returns true if this map maps one or more keys to the specified value.
    Returns a set view of the mappings contained in this map.
    boolean
    Compares the specified object with this map for equality.
    get(int index)
    Returns the element at the specified position in this list.
    get(Object key)
    Returns the value to which this map maps the specified key.
    Return an instance of this index map accessible via list interface.
    Return an instance of this index map accessible via map interface.
    getKey(int index)
    Get an indexed key.
    Get the keys as array.
    getKeys(Class<?> type)
    Get the keys as array.
    Get the values as array.
    Get the values as array.
    int
    Returns the hash code value for this map.
    int
    Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element.
    boolean
    Returns true if this map contains no key-value mappings.
    Returns an iterator over the elements in this list in proper sequence.
    Returns a set view of the keys contained in this map.
    int
    Returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element.
    Returns a list iterator of the elements in this list (in proper sequence).
    listIterator(int index)
    Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list.
    put(K key, V value)
    Associates the specified value with the specified key in this map (optional operation).
    void
    putAll(Map<? extends K,? extends V> t)
    Copies all of the mappings from the specified map to this map (optional operation).
    remove(int index)
    Removes the element at the specified position in this list.
    boolean
    Removes from this list all the elements that are contained in the specified collection.
    removeKey(K key)
    Removes the mapping for this key from this map if it is present.
    boolean
    Removes the first occurrence in this list of the specified element.
    replace(K key, V o)
    Replace an object for the given key.
    boolean
    Retains only the elements in this list that are contained in the specified collection.
    set(int index, V element)
    Replaces the element at the specified position in this list with the specified element.
    int
    Returns the number of key-value mappings in this map.
    subList(int fromIndex, int toIndex)
    Unsupported method.
    Returns an array containing all of the elements in this list in proper sequence.
    <T> T[]
    toArray(T[] array)
    Returns an array containing all of the elements in this list in proper sequence; the runtime type of the returned array is that of the specified array.
    Create a string representation of this map.
    Returns a collection view of the values contained in this map.

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • list

      protected List<K> list
      The key list.
    • map

      protected Map<K,V> map
      The key/value map.
    • asmap

      protected Map<K,V> asmap
      The index map as java.util.Map.
    • aslist

      protected List<V> aslist
      The index map as java.util.List.
  • Constructor Details

    • IndexMap

      public IndexMap()
      Create a new index map.
    • IndexMap

      public IndexMap(IndexMap<K,V> imap)
      Create a new index map.
    • IndexMap

      public IndexMap(List<K> list, Map<K,V> map)
      Create a new index map using the specified collections as backup.
      Parameters:
      list - The key list.
      map - The key/value map.
  • Method Details

    • clone

      public Object clone() throws CloneNotSupportedException
      Clone an index map.
      Overrides:
      clone in class Object
      Throws:
      CloneNotSupportedException
    • size

      public int size()
      Returns the number of key-value mappings in this map. If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
      Returns:
      the number of key-value mappings in this map.
    • isEmpty

      public boolean isEmpty()
      Returns true if this map contains no key-value mappings.
      Returns:
      true if this map contains no key-value mappings.
    • containsKey

      public boolean containsKey(Object key)
      Returns true if this map contains a mapping for the specified key. More formally, returns true if and only if this map contains at a mapping for a key k such that (key==null ? k==null : key.equals(k)). (There can be at most one such mapping.)
      Parameters:
      key - key whose presence in this map is to be tested.
      Returns:
      true if this map contains a mapping for the specified key.
      Throws:
      ClassCastException - if the key is of an inappropriate type for this map (optional).
      NullPointerException - if the key is null and this map does not not permit null keys (optional).
    • containsValue

      public boolean containsValue(Object value)
      Returns true if this map maps one or more keys to the specified value. More formally, returns true if and only if this map contains at least one mapping to a value v such that (value==null ? v==null : value.equals(v)). This operation will probably require time linear in the map size for most implementations of the Map interface.
      Parameters:
      value - value whose presence in this map is to be tested.
      Returns:
      true if this map maps one or more keys to the specified value.
      Throws:
      ClassCastException - if the value is of an inappropriate type for this map (optional).
      NullPointerException - if the value is null and this map does not not permit null values (optional).
    • get

      public V get(Object key)
      Returns the value to which this map maps the specified key. Returns null if the map contains no mapping for this key. A return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.

      More formally, if this map contains a mapping from a key k to a value v such that (key==null ? k==null : key.equals(k)), then this method returns v; otherwise it returns null. (There can be at most one such mapping.)

      Parameters:
      key - key whose associated value is to be returned.
      Returns:
      the value to which this map maps the specified key, or null if the map contains no mapping for this key.
      Throws:
      ClassCastException - if the key is of an inappropriate type for this map (optional).
      NullPointerException - key is null and this map does not not permit null keys (optional).
      See Also:
    • put

      public V put(K key, V value)
      Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for this key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.))
      Parameters:
      key - key with which the specified value is to be associated.
      value - value to be associated with the specified key.
      Returns:
      previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the map previously associated null with the specified key, if the implementation supports null values.
      Throws:
      UnsupportedOperationException - if the put operation is not supported by this map.
      ClassCastException - if the class of the specified key or value prevents it from being stored in this map.
      IllegalArgumentException - if some aspect of this key or value prevents it from being stored in this map.
      NullPointerException - this map does not permit null keys or values, and the specified key or value is null.
    • putAll

      public void putAll(Map<? extends K,? extends V> t)
      Copies all of the mappings from the specified map to this map (optional operation). The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map. The behavior of this operation is unspecified if the specified map is modified while the operation is in progress.
      Parameters:
      t - Mappings to be stored in this map.
      Throws:
      UnsupportedOperationException - if the putAll method is not supported by this map.
      ClassCastException - if the class of a key or value in the specified map prevents it from being stored in this map.
      IllegalArgumentException - some aspect of a key or value in the specified map prevents it from being stored in this map.
      NullPointerException - the specified map is null, or if this map does not permit null keys or values, and the specified map contains null keys or values.
    • clear

      public void clear()
      Removes all mappings from this map.
    • keySet

      public Set<K> keySet()
      Returns a set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined. No key order is reflected in the set!
      Returns:
      a set view of the keys contained in this map.
    • values

      public Collection<V> values()
      Returns a collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress, the results of the iteration are undefined.
      Returns:
      a collection view of the values contained in this map.
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Returns a set view of the mappings contained in this map. Each element in the returned set is a Map.Entry. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined. No key order is reflected in the set!
      Returns:
      a set view of the mappings contained in this map.
    • equals

      public boolean equals(Object o)
      Compares the specified object with this map for equality. Returns true if the given object is also a map and the two Maps represent the same mappings. More formally, two maps t1 and t2 represent the same mappings if t1.entrySet().equals(t2.entrySet()). This ensures that the equals method works properly across different implementations of the Map interface.
      Overrides:
      equals in class Object
      Parameters:
      o - object to be compared for equality with this map.
      Returns:
      true if the specified object is equal to this map.
    • hashCode

      public int hashCode()
      Returns the hash code value for this map.
      Overrides:
      hashCode in class Object
      Returns:
      the hash code value for this map.
      See Also:
    • toString

      public String toString()
      Create a string representation of this map.
      Overrides:
      toString in class Object
    • contains

      public boolean contains(Object o)
      Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).
      Parameters:
      o - element whose presence in this list is to be tested.
      Returns:
      true if this list contains the specified element.
      Throws:
      ClassCastException - if the type of the specified element is incompatible with this list (optional).
      NullPointerException - if the specified element is null and this list does not support null elements (optional).
    • iterator

      public Iterator<V> iterator()
      Returns an iterator over the elements in this list in proper sequence.
      Returns:
      an iterator over the elements in this list in proper sequence.
    • toArray

      public Object[] toArray()
      Returns an array containing all of the elements in this list in proper sequence. Obeys the general contract of the Collection.toArray method.
      Returns:
      an array containing all of the elements in this list in proper sequence.
      See Also:
    • toArray

      public <T> T[] toArray(T[] array)
      Returns an array containing all of the elements in this list in proper sequence; the runtime type of the returned array is that of the specified array. Obeys the general contract of the Collection.toArray(Object[]) method.
      Parameters:
      array - the array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
      Returns:
      an array containing the elements of this list.
      Throws:
      ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of every element in this list.
      NullPointerException - if the specified array is null.
    • containsAll

      public boolean containsAll(Collection<?> c)
      Returns true if this list contains all of the elements of the specified collection.
      Parameters:
      c - collection to be checked for containment in this list.
      Returns:
      true if this list contains all of the elements of the specified collection.
      Throws:
      ClassCastException - if the types of one or more elements in the specified collection are incompatible with this list (optional).
      NullPointerException - if the specified collection contains one or more null elements and this list does not support null elements (optional).
      NullPointerException - if the specified collection is null.
      See Also:
    • removeAll

      public boolean removeAll(Collection<?> c)
      Removes from this list all the elements that are contained in the specified collection.
      Parameters:
      c - collection that defines which elements will be removed from this list.
      Returns:
      true if this list changed as a result of the call.
      Throws:
      ClassCastException - if the types of one or more elements in this list are incompatible with the specified collection (optional).
      NullPointerException - if this list contains one or more null elements and the specified collection does not support null elements (optional).
      NullPointerException - if the specified collection is null.
      See Also:
    • retainAll

      public boolean retainAll(Collection<?> c)
      Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all the elements that are not contained in the specified collection.
      Parameters:
      c - collection that defines which elements this set will retain.
      Returns:
      true if this list changed as a result of the call.
      Throws:
      ClassCastException - if the types of one or more elements in this list are incompatible with the specified collection (optional).
      NullPointerException - if this list contains one or more null elements and the specified collection does not support null elements (optional).
      NullPointerException - if the specified collection is null.
      See Also:
    • get

      public V get(int index)
      Returns the element at the specified position in this list.
      Parameters:
      index - index of element to return.
      Returns:
      the element at the specified position in this list.
      Throws:
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).
    • set

      public V set(int index, V element)
      Replaces the element at the specified position in this list with the specified element.
      Parameters:
      index - index of element to replace.
      element - element to be stored at the specified position.
      Returns:
      the element previously at the specified position.
      Throws:
      ClassCastException - if the class of the specified element prevents it from being added to this list.
      NullPointerException - if the specified element is null and this list does not support null elements.
      IllegalArgumentException - if some aspect of the specified element prevents it from being added to this list.
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).
    • remove

      public V remove(int index)
      Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.
      Parameters:
      index - the index of the element to removed.
      Returns:
      the element previously at the specified position.
      Throws:
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).
    • indexOf

      public int indexOf(Object o)
      Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.
      Parameters:
      o - element to search for.
      Returns:
      the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element.
      Throws:
      ClassCastException - if the type of the specified element is incompatible with this list (optional).
      NullPointerException - if the specified element is null and this list does not support null elements (optional).
    • lastIndexOf

      public int lastIndexOf(Object o)
      Returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element. More formally, returns the highest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.
      Parameters:
      o - element to search for.
      Returns:
      the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element.
      Throws:
      ClassCastException - if the type of the specified element is incompatible with this list (optional).
      NullPointerException - if the specified element is null and this list does not support null elements (optional).
    • listIterator

      public ListIterator<V> listIterator()
      Returns a list iterator of the elements in this list (in proper sequence).
      Returns:
      a list iterator of the elements in this list (in proper sequence).
    • listIterator

      public ListIterator<V> listIterator(int index)
      Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list. The specified index indicates the first element that would be returned by an initial call to the next method. An initial call to the previous method would return the element with the specified index minus one.
      Parameters:
      index - index of first element to be returned from the list iterator (by a call to the next method).
      Returns:
      a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list.
      Throws:
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size()).
    • subList

      public List subList(int fromIndex, int toIndex)
      Unsupported method.
      Throws:
      UnsupportedOperationException
    • add

      public boolean add(Object o)
      Unsupported method, due to missing key parameter.
      Throws:
      UnsupportedOperationException
    • addAll

      public boolean addAll(Collection c)
      Unsupported method, due to missing key parameter.
      Throws:
      UnsupportedOperationException
    • addAll

      public boolean addAll(int index, Collection c)
      Unsupported method, due to missing key parameter.
      Throws:
      UnsupportedOperationException
    • add

      public void add(int index, Object element)
      Unsupported method, due to missing key parameter.
      Throws:
      UnsupportedOperationException
    • removeKey

      public V removeKey(K key)
      Removes the mapping for this key from this map if it is present. More formally, if this map contains a mapping from key k to value v such that (key==null ? k==null : key.equals(k)), that mapping is removed. (The map can contain at most one such mapping.)

      Returns the value to which the map previously associated the key, or null if the map contained no mapping for this key. (A null return can also indicate that the map previously associated null with the specified key if the implementation supports null values.) The map will not contain a mapping for the specified key once the call returns.

      Parameters:
      key - key whose mapping is to be removed from the map.
      Returns:
      previous value associated with specified key, or null if there was no mapping for key.
      Throws:
      ClassCastException - if the key is of an inappropriate type for this map (optional).
      NullPointerException - if the key is null and this map does not not permit null keys (optional).
    • removeValue

      public boolean removeValue(Object o)
      Removes the first occurrence in this list of the specified element. If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists).
      Parameters:
      o - element to be removed from this list, if present.
      Returns:
      true if this list contained the specified element.
      Throws:
      ClassCastException - if the type of the specified element is incompatible with this list (optional).
      NullPointerException - if the specified element is null and this list does not support null elements (optional).
    • add

      public void add(K key, V o)
      Add a new object with key and value. The key must not exist.
      Parameters:
      key - The key.
      o - The object.
    • replace

      public V replace(K key, V o)
      Replace an object for the given key. The key has to exist.
      Parameters:
      key - The key.
      o - The object.
      Returns:
      The old value for the key.
    • getKey

      public K getKey(int index)
      Get an indexed key.
      Parameters:
      index - The index.
      Returns:
      The key.
    • add

      public void add(int index, K key, V o)
      Add an object to the collection.
      Parameters:
      index - The index.
      key - The key.
      o - The object.
    • getAsMap

      public Map<K,V> getAsMap()
      Return an instance of this index map accessible via map interface.
    • getAsList

      public List<V> getAsList()
      Return an instance of this index map accessible via list interface.
    • getObjects

      public Object[] getObjects()
      Get the values as array.
      Returns:
      The array of values.
    • getObjects

      public Object[] getObjects(Class type)
      Get the values as array.
      Parameters:
      type - The component type of the array.
      Returns:
      The array of values.
    • getKeys

      public Object[] getKeys()
      Get the keys as array.
      Returns:
      The array of keys.
    • getKeys

      public Object[] getKeys(Class<?> type)
      Get the keys as array.
      Parameters:
      type - The component type of the array.
      Returns:
      The array of keys.