org.abstracthorizon.danube.http.util
Class MultiStringHashMap

java.lang.Object
  extended by org.abstracthorizon.danube.http.util.MultiStringHashMap
All Implemented Interfaces:
MultiStringMap

public class MultiStringHashMap
extends Object
implements MultiStringMap

MultiStringMap implementation using HashMap This implementation uses map and is storing multiple elements in a list. If only one element remains in the list then it is replaced with that element only.

Author:
Daniel Sendula

Nested Class Summary
protected  class MultiStringHashMap.InternalMapEntry<K,V>
          Map entry for internal use
 
Field Summary
protected  Map<String,Object> map
          Backing storage.
 
Constructor Summary
MultiStringHashMap()
          Constructor.
MultiStringHashMap(int initialCapacity)
          Constructor.
MultiStringHashMap(int initialCapacity, float loadFactor)
          Constructor.
 
Method Summary
 void add(String id, String value)
           Adds new element to the map.
 void addAll(String id, Collection<String> values)
          Adds all elements from the given collection
 void addAll(String id, String[] values)
          Adds all elements from the given array.
 void clear()
          Clears the map
 boolean containsKey(String id)
          Returns true if there is at least one entry
 Collection<Map.Entry<String,String>> getAllEntries()
          Returns list of all entries.
 String[] getAsArray(String id)
          Returns array of all elements under asked key.
 List<String> getAsList(String id)
          Returns list of all elements under asked key.
 Map<String,Object> getAsMap()
          Returns a map that contains all elements.
 int getEntrySize(String id)
          Returns number of entries for given key
 String getFirst(String id)
          Retrieves element from the given key.
 String getOnly(String id)
          Retrieves element from the given key.
 Set<String> keySet()
          Returns key set
 void putAll(String id, Collection<String> values)
           Replaces existing element(s), if there are any under the given key, with the given values.
 void putAll(String id, String[] values)
          Replaces existing element(s), if there are any under the given key, with the given values.
 void putOnly(String id, String value)
          Replaces existing element(s), if there are any under the given key, with the given value.
 String remove(String id, int index)
           Removes n-th element from the given key.
 Collection<String> removeAll(String id)
          Removes all elements from the given key
 String removeFirst(String id)
           Removes first element from the given key.
 int size()
          Number of keys in the map.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

map

protected Map<String,Object> map
Backing storage.

Constructor Detail

MultiStringHashMap

public MultiStringHashMap()
Constructor.


MultiStringHashMap

public MultiStringHashMap(int initialCapacity)
Constructor.

Parameters:
initialCapacity - initial capacity of a backing map

MultiStringHashMap

public MultiStringHashMap(int initialCapacity,
                          float loadFactor)
Constructor.

Parameters:
initialCapacity - initial capacity of a backing map
loadFactor - load factory of a backing map
Method Detail

add

public void add(String id,
                String value)

Adds new element to the map. If no entries with given id exist than this is going to be the first. If there are already values under given id then this is going to be added as a new one.

This implementation if there is already one value in the backing map converts it to a list and adds both (old and new values) to it.

Specified by:
add in interface MultiStringMap
Parameters:
id - key
value - value to be added
Throws:
IllegalStateException - if existing element of the backing storage is not a string or a list

addAll

public void addAll(String id,
                   String[] values)

Adds all elements from the given array.

This implementation is using addAll(String, Collection) to add values

Specified by:
addAll in interface MultiStringMap
Parameters:
id - key
values - array of values
See Also:
MultiStringMap.add(String, String)

addAll

public void addAll(String id,
                   Collection<String> values)

Adds all elements from the given collection

This implementation if there is already one value in the backing map converts it to a list and adds both (old and new values) to it.

Specified by:
addAll in interface MultiStringMap
Parameters:
id - key
values - collection which elements are to be added
Throws:
IllegalStateException - if existing element of the backing storage is not a string or a list
See Also:
MultiStringMap.add(String, String)

putOnly

public void putOnly(String id,
                    String value)
Replaces existing element(s), if there are any under the given key, with the given value.

Specified by:
putOnly in interface MultiStringMap
Parameters:
id - key
value - value to be put to the map

putAll

public void putAll(String id,
                   String[] values)

Replaces existing element(s), if there are any under the given key, with the given values.

If array's length is greater then one it stores given array of strings as a list, if arrayls. If array's length is exactly one then value of it is stored as a string while if array's length is zero then it is removed from the backing storage.

Specified by:
putAll in interface MultiStringMap
Parameters:
id - key
values - values to be put to the map
See Also:
MultiStringMap.putOnly(String, String)

putAll

public void putAll(String id,
                   Collection<String> values)

Replaces existing element(s), if there are any under the given key, with the given values.

If array's length is greater then one it stores given array of strings as a list, if arrayls. If array's length is exactly one then value of it is stored as a string while if array's length is zero then it is removed from the backing storage.

Note: This implementation is not ensuring that all collection elements are of String type.

Specified by:
putAll in interface MultiStringMap
Parameters:
id - key
values - collection of values to be put to the map
See Also:
MultiStringMap.putOnly(String, String)

removeAll

public Collection<String> removeAll(String id)
Removes all elements from the given key

Specified by:
removeAll in interface MultiStringMap
Parameters:
id - key

removeFirst

public String removeFirst(String id)

Removes first element from the given key. If there was only one element then there will be no more elements under the given key

Implementation ensures that if list is reduced to only one element then that element is stored instead of the list itself.

Specified by:
removeFirst in interface MultiStringMap
Parameters:
id - key
Throws:
IllegalStateException - if the element of the backing storage is not string nor list or there are no elements to be removed

remove

public String remove(String id,
                     int index)

Removes n-th element from the given key.

Implementation ensures that if list is reduced to only one element then that element is stored instead of the list itself.

Specified by:
remove in interface MultiStringMap
Parameters:
id - key
index - index of the element to be removed
Throws:
IndexOutOfBoundsException - if there are no elements under the given key
IllegalStateException - if the element of the backing storage is not string nor list or there are no elements to be removed

containsKey

public boolean containsKey(String id)
Returns true if there is at least one entry

Specified by:
containsKey in interface MultiStringMap
Parameters:
id - key
Returns:
true if there is at least one entry

getEntrySize

public int getEntrySize(String id)
Returns number of entries for given key

Specified by:
getEntrySize in interface MultiStringMap
Parameters:
id - key
Returns:
number of entries for given key

getOnly

public String getOnly(String id)
Retrieves element from the given key. Key must have only one element (or none) for this method to work.

Specified by:
getOnly in interface MultiStringMap
Parameters:
id - key of the asked element
Returns:
element
Throws:
IllegalStateException - if there are more then one element under this key or nor string or list is stored in the backing storage

getFirst

public String getFirst(String id)
Retrieves element from the given key. Key must have only one element (or none) for this method to work.

Specified by:
getFirst in interface MultiStringMap
Parameters:
id - key of the asked element
Returns:
element
Throws:
IllegalStateException - if there are more then one element under this key or nor string or list is stored in the backing storage

getAsArray

public String[] getAsArray(String id)
Returns array of all elements under asked key. If there are no elements then it returns an empty array.

Specified by:
getAsArray in interface MultiStringMap
Parameters:
id - key of the asked elements
Returns:
an array of strings
Throws:
IllegalStateException - if the element of the backing storage is not string nor list or there are no elements to be removed

getAsList

public List<String> getAsList(String id)
Returns list of all elements under asked key. If there are no elements then it returns an empty list.

Specified by:
getAsList in interface MultiStringMap
Parameters:
id - key of the asked elements
Returns:
a list of strings
Throws:
IllegalStateException - if the element of the backing storage is not string nor list or there are no elements to be removed

clear

public void clear()
Clears the map

Specified by:
clear in interface MultiStringMap

keySet

public Set<String> keySet()
Returns key set

Specified by:
keySet in interface MultiStringMap
Returns:
key set

getAsMap

public Map<String,Object> getAsMap()
Returns a map that contains all elements. Where there are more elements per key then implementation is responsiple of returning something sensible like an array of strings or a collection (or a list) whose elements are strings

Specified by:
getAsMap in interface MultiStringMap
Returns:
a map

getAllEntries

public Collection<Map.Entry<String,String>> getAllEntries()
Returns list of all entries. Entries with the same key will repeat if there are more elements stored under the same key.

Specified by:
getAllEntries in interface MultiStringMap
Returns:
list of all entries
Throws:
IllegalStateException - if the element of the backing storage is not string nor list or there are no elements to be removed

size

public int size()
Number of keys in the map. It won't return total number of elements but just number of differnet keys in the map.

Specified by:
size in interface MultiStringMap
Returns:
number of keys in the map


Copyright © 2005-2009 Abstract Horizon. All Rights Reserved.