org.pf.util
Class AssociationList<K,V>

java.lang.Object
  extended by org.pf.util.AssociationList<K,V>
Direct Known Subclasses:
NamedValueList

public class AssociationList<K,V>
extends java.lang.Object

A container that holds a collection of Association objects. No null values are allowed. Neither for association keys nor for association values.

Version:
2.0
Author:
Manfred Duchrow

Field Summary
static Association[] EMPTY_ASSOCIATION_ARRAY
           
 
Constructor Summary
AssociationList()
          Initialize the new instance with default values.
AssociationList(java.util.Map<K,V> map)
          Initialize the new instance with values from the given map.
 
Method Summary
 void add(Association<K,V> association)
          Adds the specified association.
 void add(K key, V value)
          Adds the specified key and value as new association.
 void addAll(Association<K,V>... associations)
          Adds all association elements of the given array to the list.
 void addAll(java.util.Map<K,V> map)
          Add all entries of the specified map as associations
 void addAll(java.util.Properties props)
          Add all entries of the specified properties as associations
 java.util.Map<K,V> addAllToMap(java.util.Map<K,V> map)
          Returns the given map object with all associations added to it.
 java.util.HashMap<K,V> asHashMap()
          Returns all associations transformed to a HashMap where the association keys became the map keys and the association values became the map values.
 java.util.Hashtable<K,V> asHashtable()
          Returns all associations transformed to a Hashtable where the association keys became the map keys and the association values became the map values.
 java.util.List<Association<K,V>> asList()
          Returns all associations as list
 Association<K,V>[] associationArray()
          Returns all associations as an array
 Association<K,V>[] associationArray(IObjectFilter filter)
          Returns an array of all those elements contained in this list that match the given filter.
 Association<K,V> associationAt(int index)
          Returns the association at the specified index.
 void clear()
          Removes all association from the list.
 boolean containsKey(K key)
          Returns true if the given key is in the list
 Association<K,V> findAssociation(K key)
          Returns the association with the given key or null if the key can't be found.
 int indexOf(K key)
          Returns the index of the given key or -1 if the key can't be found.
 boolean isEmpty()
          Returns true if this list has no elements
 K keyAt(int index)
          Returns the key of the association at the specified index.
 java.util.List<K> keys()
          Returns all values that are currently stored (might contain duplicates)
 void processEach(IObjectProcessor associationProcessor)
          Calls the given associationProcessor once for each association in this list.
 void put(Association<K,V> association)
          Adds the specified association.
 void put(K key, V value)
          Puts the given value under the specified key.
 void putAll(Association<K,V>[] associations)
          Adds all association elements of the given array to the list.
 void putAll(java.util.Map<K,V> map)
          Puts all key-value pairs of the given map to the list.
 boolean remove(Association<K,V> association)
          Removes the given association from this list.
 Association<K,V> remove(int index)
          Removes the association at the given index.
 void removeAll(java.util.Collection<Association<K,V>> assocCollection)
          Removes all association that are contained in the given collection.
 Association<K,V> removeKey(K key)
          Removes the association with the given key.
 void setAssociationAt(int index, Association<K,V> associaction)
          Puts the given association at the specified index.
 int size()
          Returns the number of associations in this list
 V valueAt(int index)
          Returns the value of the association at the specified index.
 V valueAt(K key)
          Returns the value associated with the specified key or null if the key cannot be found.
 java.util.List<V> values()
          Returns all values that are currently stored (might contain duplicates)
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_ASSOCIATION_ARRAY

public static final Association[] EMPTY_ASSOCIATION_ARRAY
Constructor Detail

AssociationList

public AssociationList()
Initialize the new instance with default values.


AssociationList

public AssociationList(java.util.Map<K,V> map)
Initialize the new instance with values from the given map.

Method Detail

clear

public void clear()
Removes all association from the list.


size

public int size()
Returns the number of associations in this list


isEmpty

public boolean isEmpty()
Returns true if this list has no elements


add

public void add(Association<K,V> association)
Adds the specified association.

Parameters:
association - The association to add (must not be null!)

add

public void add(K key,
                V value)
Adds the specified key and value as new association.

Parameters:
key - The key of the association to add
value - The value of the association to add

addAll

public void addAll(java.util.Map<K,V> map)
Add all entries of the specified map as associations

Parameters:
map - The map that contains all the key/value pairs to add

addAll

public void addAll(java.util.Properties props)
Add all entries of the specified properties as associations

Parameters:
props - The map that contains all the key/value pairs to add

addAll

public void addAll(Association<K,V>... associations)
Adds all association elements of the given array to the list.

Parameters:
associations - The associations to add

associationArray

public Association<K,V>[] associationArray()
Returns all associations as an array


associationArray

public Association<K,V>[] associationArray(IObjectFilter filter)
Returns an array of all those elements contained in this list that match the given filter. Each Association element of this list gets passed to the matches() method of the filter. If the filter is null, all elements will be returned.

Parameters:
filter - The filter that determines which elements to return in the result array
Returns:
Always an array, never null

asList

public java.util.List<Association<K,V>> asList()
Returns all associations as list

Returns:
List

asHashtable

public java.util.Hashtable<K,V> asHashtable()
Returns all associations transformed to a Hashtable where the association keys became the map keys and the association values became the map values.


asHashMap

public java.util.HashMap<K,V> asHashMap()
Returns all associations transformed to a HashMap where the association keys became the map keys and the association values became the map values.


addAllToMap

public java.util.Map<K,V> addAllToMap(java.util.Map<K,V> map)
Returns the given map object with all associations added to it.

Parameters:
map - A valid map object

processEach

public void processEach(IObjectProcessor associationProcessor)
Calls the given associationProcessor once for each association in this list. Each object that gets passed to processObject() is of type Association.

Parameters:
associationProcessor - The processor to be called for each association

associationAt

public Association<K,V> associationAt(int index)
Returns the association at the specified index.

Parameters:
index - The index of the Association

setAssociationAt

public void setAssociationAt(int index,
                             Association<K,V> associaction)
Puts the given association at the specified index.

Parameters:
index - The index of the association to set
associaction - The association to put at the given index
Throws:
java.lang.IndexOutOfBoundsException - if the index is out of range

keyAt

public K keyAt(int index)
Returns the key of the association at the specified index.

Parameters:
index - The index of the Association

valueAt

public V valueAt(int index)
Returns the value of the association at the specified index.

Parameters:
index - The index of the Association

valueAt

public V valueAt(K key)
Returns the value associated with the specified key or null if the key cannot be found.

Parameters:
key - The identifier for the desired value

findAssociation

public Association<K,V> findAssociation(K key)
Returns the association with the given key or null if the key can't be found.

Parameters:
key - The key to identify the association

remove

public boolean remove(Association<K,V> association)
Removes the given association from this list. Returns true, if the association was found in the list and now removed from it.


remove

public Association<K,V> remove(int index)
Removes the association at the given index. Returns the removed association.


removeKey

public Association<K,V> removeKey(K key)
Removes the association with the given key. Returns the removed association or null if not found.


removeAll

public void removeAll(java.util.Collection<Association<K,V>> assocCollection)
Removes all association that are contained in the given collection.


values

public java.util.List<V> values()
Returns all values that are currently stored (might contain duplicates)


keys

public java.util.List<K> keys()
Returns all values that are currently stored (might contain duplicates)


containsKey

public boolean containsKey(K key)
Returns true if the given key is in the list

Parameters:
key - The key to look for

put

public void put(K key,
                V value)
Puts the given value under the specified key. If the key already exists, its associated value will be replaced. Otherwise the new key/value pair will be added at the end of the list.


put

public void put(Association<K,V> association)
Adds the specified association.

Parameters:
association - The association to add (must not be null!)

putAll

public void putAll(Association<K,V>[] associations)
Adds all association elements of the given array to the list.

Parameters:
associations - The associations to add

putAll

public void putAll(java.util.Map<K,V> map)
Puts all key-value pairs of the given map to the list. If elements with the same key already exist then their value will be replaced.

Parameters:
map - The associations to add

indexOf

public int indexOf(K key)
Returns the index of the given key or -1 if the key can't be found.

Parameters:
key - The key to look for