de.enough.polish.util
Class KeyValueList<K,V>

java.lang.Object
  extended by de.enough.polish.util.KeyValueList<K,V>
Type Parameters:
K - type of keys; when you use the enough-polish-client-java5.jar you can parameterize the KeyValueList, e.g. KeyValueList<Integer, String> = new KeyValueList<Integer, String>(10);
V - type of values; when you use the enough-polish-client-java5.jar you can parameterize the KeyValueList, e.g. KeyValueList<Integer, String> = new KeyValueList<Integer, String>(10);
All Implemented Interfaces:
Externalizable, Serializable

public class KeyValueList<K,V>
extends Object
implements Externalizable

Provides a list that may contain several duplicate keys

Copyright Enough Software 2008

Author:
Robert Virkus, j2mepolish@enough.de

Field Summary
 ArrayList<K> keys
           
 ArrayList<V> values
           
 
Constructor Summary
KeyValueList()
          Creates an KeyValueList with the initial capacity of 10 and a growth factor of 75%
KeyValueList(int initialCapacity)
          Creates an KeyValueList with the given initial capacity and a growth factor of 75%
KeyValueList(int initialCapacity, int growthFactor)
          Creates a new KeyValueList
 
Method Summary
 void add(int index, K key, V value)
          Inserts the given element at the defined position.
 void add(K key, V value)
          Stores the given key-value pair in this list.
 void clear()
          Removes all of the elements from this list.
 boolean containsKey(K element)
          Determines whether the given key is stored in this list.
 boolean containsValue(V element)
          Determines whether the given value is stored in this list.
 Object[] getInternalKeyArray()
          Retrieves the internal key array - use with care! This method allows to access stored objects without creating an intermediate array.
 Object[] getInternalValueArray()
          Retrieves the internal value array - use with care! This method allows to access stored objects without creating an intermediate array.
 K getKey(int index)
          Returns the key at the specified position in this list.
 V getValue(int index)
          Returns the value at the specified position in this list.
 int indexOfKey(K element)
          Retrieves the (first) index of the given key.
 int indexOfValue(V element)
          Retrieves the (first) index of the given value.
 void read(DataInputStream in)
          Restores the internal instance fields from the given input stream.
 K remove(int index)
          Removes the key-value pair at the specified position in this list.
 boolean remove(K element)
          Removes the given key and the corresponding value.
 K set(int index, K key, V value)
          Replaces the key-value pair at the specified position in this list with the specified element.
 int size()
          Retrieves the current size of this array list.
 void write(DataOutputStream out)
          Stores the internal instance fields to the output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

keys

public final ArrayList<K> keys

values

public final ArrayList<V> values
Constructor Detail

KeyValueList

public KeyValueList()
Creates an KeyValueList with the initial capacity of 10 and a growth factor of 75%


KeyValueList

public KeyValueList(int initialCapacity)
Creates an KeyValueList with the given initial capacity and a growth factor of 75%

Parameters:
initialCapacity - the capacity of this list.

KeyValueList

public KeyValueList(int initialCapacity,
                    int growthFactor)
Creates a new KeyValueList

Parameters:
initialCapacity - the capacity of this list.
growthFactor - the factor in % for increasing the capacity when there's not enough room in this list anymore
Method Detail

size

public int size()
Retrieves the current size of this array list.

Returns:
the number of stored elements in this list.

containsKey

public boolean containsKey(K element)
Determines whether the given key is stored in this list.

Parameters:
element - the key which might be stored in this list
Returns:
true when the given element is stored in this list
Throws:
IllegalArgumentException - when the given element is null
See Also:
remove(Object)

containsValue

public boolean containsValue(V element)
Determines whether the given value is stored in this list.

Parameters:
element - the value which might be stored in this list
Returns:
true when the given element is stored in this list
Throws:
IllegalArgumentException - when the given element is null
See Also:
remove(Object)

indexOfKey

public int indexOfKey(K element)
Retrieves the (first) index of the given key.

Parameters:
element - the key which is part of this list.
Returns:
the index of the key or -1 when the object is not part of this list.
Throws:
IllegalArgumentException - when the given element is null

indexOfValue

public int indexOfValue(V element)
Retrieves the (first) index of the given value.

Parameters:
element - the value which is part of this list.
Returns:
the index of the value or -1 when the object is not part of this list.
Throws:
IllegalArgumentException - when the given element is null

getKey

public K getKey(int index)
Returns the key at the specified position in this list.

Parameters:
index - the position of the desired element.
Returns:
the key stored at the given position
Throws:
IndexOutOfBoundsException - when the index < 0 || index >= size()

getValue

public V getValue(int index)
Returns the value at the specified position in this list.

Parameters:
index - the position of the desired element.
Returns:
the value stored at the given position
Throws:
IndexOutOfBoundsException - when the index < 0 || index >= size()

remove

public K remove(int index)
Removes the key-value pair at the specified position in this list.

Parameters:
index - the position of the desired element.
Returns:
the key stored at the given position
Throws:
IndexOutOfBoundsException - when the index < 0 || index >= size()

remove

public boolean remove(K element)
Removes the given key and the corresponding value.

Parameters:
element - the element which should be removed.
Returns:
true when the element was found in this list.
Throws:
IllegalArgumentException - when the given element is null
See Also:
containsKey(Object)

clear

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


add

public void add(K key,
                V value)
Stores the given key-value pair in this list.

Parameters:
key - the key
value - the value
Throws:
IllegalArgumentException - when the given key or value is null
See Also:
add( int, Object, Object )

add

public void add(int index,
                K key,
                V value)
Inserts the given element at the defined position. Any following elements are shifted one position to the back.

Parameters:
index - the position at which the element should be inserted, use 0 when the element should be inserted in the front of this list.
key - the key
value - the value
Throws:
IllegalArgumentException - when the given element is null
IndexOutOfBoundsException - when the index < 0 || index >= size()

set

public K set(int index,
             K key,
             V value)
Replaces the key-value pair at the specified position in this list with the specified element.

Parameters:
index - the position of the element, the first element has the index 0.
key - the key
value - the value
Returns:
the replaced key element
Throws:
IndexOutOfBoundsException - when the index < 0 || index >= size()

getInternalKeyArray

public Object[] getInternalKeyArray()
Retrieves the internal key array - use with care! This method allows to access stored objects without creating an intermediate array. You really should refrain from changing any elements in the returned array unless you are 110% sure about what you are doing. It is safe to cycle through this array to access it's elements, though. Note that some array positions might contain null. Also note that the internal array is changed whenever this list has to be increased.

Returns:
the internal array

getInternalValueArray

public Object[] getInternalValueArray()
Retrieves the internal value array - use with care! This method allows to access stored objects without creating an intermediate array. You really should refrain from changing any elements in the returned array unless you are 110% sure about what you are doing. It is safe to cycle through this array to access it's elements, though. Note that some array positions might contain null. Also note that the internal array is changed whenever this list has to be increased.

Returns:
the internal array

read

public void read(DataInputStream in)
          throws IOException
Description copied from interface: Externalizable
Restores the internal instance fields from the given input stream.

Specified by:
read in interface Externalizable
Parameters:
in - the input stream from which the data is loaded
Throws:
IOException - when reading fails

write

public void write(DataOutputStream out)
           throws IOException
Description copied from interface: Externalizable
Stores the internal instance fields to the output stream.

Specified by:
write in interface Externalizable
Parameters:
out - the output stream to which instance fields should be written
Throws:
IOException - when writing fails