Jadex 0.96-beta1

jadex.util.collection
Class IndexMap

java.lang.Object
  extended by jadex.util.collection.IndexMap
All Implemented Interfaces:
Serializable, Cloneable
Direct Known Subclasses:
IndexMap.ListIndexMap, IndexMap.MapIndexMap

public class IndexMap
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:
Serialized Form

Nested Class Summary
static class IndexMap.ListIndexMap
          Provide access to the index map via list interface.
static class IndexMap.MapIndexMap
          Provide access to the index map via map interface.
 
Constructor Summary
IndexMap()
          Create a new index map.
IndexMap(List list, Map map)
          Create a new index map using the specified collections as backup.
 
Method Summary
 void add(int index, Object element)
          Unsupported method, due to missing key parameter.
 void add(int index, Object key, Object o)
          Add an object to the collection.
 boolean add(Object o)
          Unsupported method, due to missing key parameter.
 void add(Object key, Object o)
          Add a new object with key and value.
 boolean addAll(Collection c)
          Unsupported method, due to missing key parameter.
 boolean addAll(int index, Collection c)
          Unsupported method, due to missing key parameter.
 void clear()
          Removes all mappings from this map.
 Object clone()
          Clone an index map.
 boolean contains(Object o)
          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.
 Set entrySet()
          Returns a set view of the mappings contained in this map.
 boolean equals(Object o)
          Compares the specified object with this map for equality.
 Object get(int index)
          Returns the element at the specified position in this list.
 Object get(Object key)
          Returns the value to which this map maps the specified key.
 List getAsList()
          Return an instance of this index map accessible via list interface.
 Map getAsMap()
          Return an instance of this index map accessible via map interface.
 Object getKey(int index)
          Get an indexed key.
 Object[] getKeys()
          Get the keys as array.
 Object[] getKeys(Class type)
          Get the keys as array.
 Object[] getObjects()
          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 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.
 boolean isEmpty()
          Returns true if this map contains no key-value mappings.
 Iterator iterator()
          Returns an iterator over the elements in this list in proper sequence.
 Set keySet()
          Returns a set view of the keys contained in this map.
 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.
 ListIterator listIterator()
          Returns a list iterator of the elements in this list (in proper sequence).
 ListIterator listIterator(int index)
          Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list.
 Object put(Object key, Object value)
          Associates the specified value with the specified key in this map (optional operation).
 void putAll(Map t)
          Copies all of the mappings from the specified map to this map (optional operation).
 Object 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.
 Object removeKey(Object key)
          Removes the mapping for this key from this map if it is present.
 boolean removeValue(Object o)
          Removes the first occurrence in this list of the specified element.
 Object replace(Object key, Object o)
          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.
 Object set(int index, Object element)
          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.
 List subList(int fromIndex, int toIndex)
          Unsupported method.
 Object[] toArray()
          Returns an array containing all of the elements in this list in proper sequence.
 Object[] toArray(Object[] 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.
 String toString()
          Create a string representation of this map.
 Collection values()
          Returns a collection view of the values contained in this map.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

IndexMap

public IndexMap()
Create a new index map.


IndexMap

public IndexMap(List list,
                Map map)
Create a new index map using the specified collections as backup.

Parameters:
list - The key list.
map - The key/value map.
Method Detail

clone

public Object clone()
Clone an index map.

Overrides:
clone in class Object

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 Object 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:
containsKey(Object)

put

public Object put(Object key,
                  Object 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 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 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 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 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:
Object.hashCode(), Object.equals(Object), equals(Object)

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 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:
Arrays.asList(Object[])

toArray

public Object[] toArray(Object[] 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:
contains(Object)

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:
removeValue(Object), contains(Object)

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:
removeValue(Object), contains(Object)

get

public Object 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 Object set(int index,
                  Object 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 Object 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 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 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 Object removeKey(Object 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(Object key,
                Object o)
Add a new object with key and value. The key must not exist.

Parameters:
key - The key.
o - The object.

replace

public Object replace(Object key,
                      Object 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 Object getKey(int index)
Get an indexed key.

Parameters:
index - The index.
Returns:
The key.

add

public void add(int index,
                Object key,
                Object o)
Add an object to the collection.

Parameters:
index - The index.
key - The key.
o - The object.

getAsMap

public Map getAsMap()
Return an instance of this index map accessible via map interface.


getAsList

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

Jadex 0.96-beta1

Submit a bug or feature
For further API reference and developer documentation, see the Jadex User Guide and the Jadex Tutorial. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, and working code examples.

Copyright (C) 2002-2007 Lars Braubach, Alexander Pokahr - University of Hamburg. Use is subject to license terms.