at.spardat.xma.mdl.util
Class KeyedList

java.lang.Object
  extended byat.spardat.xma.mdl.util.KeyedList
All Implemented Interfaces:
java.lang.Cloneable

public class KeyedList
extends java.lang.Object
implements java.lang.Cloneable

This class realizes a ordered collection consisting of entries (key, value). The keys are Strings. The collection has set semantics, i.e., at most one entry with a particular key is allowed. Nevertheless, the collection is ordered. The order is controlled via positional insertion operations (method add providing an index).

You may view this class as a mix of HashMap and ArrayList, gaining the fast access by key of the HashMap and retaining the order and the ease of indexed access of the ArrayList.


Constructor Summary
KeyedList()
           
 
Method Summary
 boolean add(int index, java.lang.String key, java.lang.Object value)
          Inserts the specified element at the specified position in this list.
 boolean add(java.lang.String key, java.lang.Object value)
          Appends the specified element to the end of this list.
 void clear()
          Removes all of the elements from this list.
 java.lang.Object clone()
          Clones shallow.
 boolean containsKey(java.lang.String key)
          Returns true if this list contains a element with the specified key.
 java.lang.String getKey(int index)
          Returns the key for the entry at the provided index.
 java.lang.Object getValue(int index)
          Returns the value of the entry at the provided index.
 java.lang.Object getValue(java.lang.String key)
          Returns the value for the provided key or null if this list does not contain an entry with the specified key.
 int indexOf(java.lang.String key)
          Returns the index of the entry whose key equals the one provided.
 java.lang.String remove(int index)
          Removes the element at the specified position in this list.
 boolean remove(java.lang.String key)
          Removes the entry with the provided key.
 boolean replace(java.lang.String key, java.lang.Object value)
          Replaces the old value associated with the provided key with a new one.
 int size()
          Returns the size of this list.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

KeyedList

public KeyedList()
Method Detail

add

public boolean add(java.lang.String key,
                   java.lang.Object value)
Appends the specified element to the end of this list.

Parameters:
key - the key of the element; must not be null
value - the value associated with the key
Returns:
true if successfully added, false if entry with key already in the list.

add

public boolean add(int index,
                   java.lang.String key,
                   java.lang.Object value)
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Parameters:
index - index at which the specified element is to be inserted.
key - the key of the element; must not be null
value - the value associated with the key
Returns:
true if entry has been successfully added to the list, false if this list already contained an entry with the provided key.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if index is out of range (index < 0 || index > size()).

getKey

public java.lang.String getKey(int index)
Returns the key for the entry at the provided index.

Parameters:
index - index at which to return the key.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if index out of range.

getValue

public java.lang.Object getValue(int index)
Returns the value of the entry at the provided index.

Parameters:
index - index at which to return the value.
Throws:
java.lang.IndexOutOfBoundsException - if index out of range.

getValue

public java.lang.Object getValue(java.lang.String key)
Returns the value for the provided key or null if this list does not contain an entry with the specified key.

Parameters:
key - the key whose value is to be found.
Returns:
the found value or null if this list does not contain an entry with the provided key.

indexOf

public int indexOf(java.lang.String key)
Returns the index of the entry whose key equals the one provided.

This is a time consuming operation of O(n).

Parameters:
key - the key of the entry whose index is wanted.
Returns:
the index of the found entry or -1 if the key is not in the list.

clear

public void clear()
Removes all of the elements from this list. The list will be empty after this call returns.


containsKey

public boolean containsKey(java.lang.String key)
Returns true if this list contains a element with the specified key.

Parameters:
key - the key to check for existence

remove

public boolean remove(java.lang.String key)
Removes the entry with the provided key.

Removing is a time consuming operation and requires O(n).

Parameters:
key - identifies the entry to be removed
Returns:
true if the entry was found and has been removed, false otherwise.

remove

public java.lang.String 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).

Removing is a time consuming operation requiring O(n). Removing from the end of the list requires O(1).

Parameters:
index - the index of the element to removed.
Returns:
the key of the element that was removed from the list.
Throws:
java.lang.IndexOutOfBoundsException - if index out of range (index < 0 || index >= size()).

replace

public boolean replace(java.lang.String key,
                       java.lang.Object value)
Replaces the old value associated with the provided key with a new one.

Parameters:
key - the key whose value is to be replaced
value - the new value
Returns:
true if this list contained the provided key and the replace has been done, false otherwise.

size

public int size()
Returns the size of this list.

Returns:
the size

clone

public java.lang.Object clone()
Clones shallow. The contained keys and values are not cloned. With keys it does not matter, since they are immutable. If the values are also immutable, you need not worry about shallow and deep semantics at all.

See Also:
Object.clone()

toString

public java.lang.String toString()
See Also:
Object.toString()