Package jadex.collection
Class IndexMap<K,V>
java.lang.Object
jadex.collection.IndexMap<K,V>
- All Implemented Interfaces:
Serializable
,Cloneable
- Direct Known Subclasses:
IndexMap.ListIndexMap
,IndexMap.MapIndexMap
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 ClassesModifier and TypeClassDescriptionstatic class
Provide access to the index map via list interface.static class
Provide access to the index map via map interface. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Unsupported method, due to missing key parameter.void
Add an object to the collection.boolean
Unsupported method, due to missing key parameter.void
Add a new object with key and value.boolean
addAll
(int index, Collection c) Unsupported method, due to missing key parameter.boolean
addAll
(Collection c) Unsupported method, due to missing key parameter.void
clear()
Removes all mappings from this map.clone()
Clone an index map.boolean
Returns true if this list contains the specified element.boolean
containsAll
(Collection<?> c) Returns true if this list contains all of the elements of the specified collection.boolean
containsKey
(Object key) Returns true if this map contains a mapping for the specified key.boolean
containsValue
(Object value) Returns true if this map maps one or more keys to the specified value.entrySet()
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.Returns the value to which this map maps the specified key.Return an instance of this index map accessible via list interface.getAsMap()
Return an instance of this index map accessible via map interface.getKey
(int index) Get an indexed key.Object[]
getKeys()
Get the keys as array.Object[]
Get the keys as array.Object[]
Get the values as array.Object[]
getObjects
(Class type) Get the values as array.int
hashCode()
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
isEmpty()
Returns true if this map contains no key-value mappings.iterator()
Returns an iterator over the elements in this list in proper sequence.keySet()
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.Associates the specified value with the specified key in this map (optional operation).void
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
removeAll
(Collection<?> c) Removes from this list all the elements that are contained in the specified collection.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 an object for the given key.boolean
retainAll
(Collection<?> c) Retains only the elements in this list that are contained in the specified collection.Replaces the element at the specified position in this list with the specified element.int
size()
Returns the number of key-value mappings in this map.subList
(int fromIndex, int toIndex) Unsupported method.Object[]
toArray()
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.toString()
Create a string representation of this map.values()
Returns a collection view of the values contained in this map.
-
Field Details
-
list
The key list. -
map
The key/value map. -
asmap
The index map as java.util.Map. -
aslist
The index map as java.util.List.
-
-
Constructor Details
-
IndexMap
public IndexMap()Create a new index map. -
IndexMap
Create a new index map. -
IndexMap
Create a new index map using the specified collections as backup.- Parameters:
list
- The key list.map
- The key/value map.
-
-
Method Details
-
clone
Clone an index map.- Overrides:
clone
in classObject
- 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
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
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
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
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 ifm.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
Copies all of the mappings from the specified map to this map (optional operation). The effect of this call is equivalent to that of callingput(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
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
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
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
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. -
hashCode
public int hashCode()Returns the hash code value for this map. -
toString
Create a string representation of this map. -
contains
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
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
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
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
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
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
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
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
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
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
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
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
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
Unsupported method.- Throws:
UnsupportedOperationException
-
add
Unsupported method, due to missing key parameter.- Throws:
UnsupportedOperationException
-
addAll
Unsupported method, due to missing key parameter.- Throws:
UnsupportedOperationException
-
addAll
Unsupported method, due to missing key parameter.- Throws:
UnsupportedOperationException
-
add
Unsupported method, due to missing key parameter.- Throws:
UnsupportedOperationException
-
removeKey
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
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
Add a new object with key and value. The key must not exist.- Parameters:
key
- The key.o
- The object.
-
replace
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
Get an indexed key.- Parameters:
index
- The index.- Returns:
- The key.
-
add
Add an object to the collection.- Parameters:
index
- The index.key
- The key.o
- The object.
-
getAsMap
Return an instance of this index map accessible via map interface. -
getAsList
Return an instance of this index map accessible via list interface. -
getObjects
Get the values as array.- Returns:
- The array of values.
-
getObjects
Get the values as array.- Parameters:
type
- The component type of the array.- Returns:
- The array of values.
-
getKeys
Get the keys as array.- Returns:
- The array of keys.
-
getKeys
Get the keys as array.- Parameters:
type
- The component type of the array.- Returns:
- The array of keys.
-