edu.harvard.seas.iis.util
Class MultiValueMap

java.lang.Object
  extended by edu.harvard.seas.iis.util.MultiValueMap

public class MultiValueMap
extends java.lang.Object

This class contains a bunch of static methods that make it easier to deal with Maps (e.g. Hashtables, PersistentMaps) that contain more than one value per key.

Author:
Krzysztof Gajos

Constructor Summary
MultiValueMap()
          Class constructor
 
Method Summary
static boolean addValue(java.util.Map map, java.lang.Object key, java.lang.Object val)
          Adds a value to a collection of values under a specific key.
static boolean addValues(java.util.Map map, java.lang.Object key, java.util.Collection vals)
          Adds a whole collection of values to a collection of values under a specific key.
static boolean containsValue(java.util.Map map, java.lang.Object key, java.lang.Object val)
          Returns true if the list of values for a key contains the value; false otherwise
static int getNumValuesFor(java.util.Map map, java.lang.Object key)
          Returns the number of values associated with a key
static java.util.Vector getValues(java.util.Map map)
          Returns a collection of all values contained in the map
static boolean isEmpty(java.util.Map map)
          Test if any keys in the map have values
static boolean removeValue(java.util.Map map, java.lang.Object key, java.lang.Object val)
          Remove the value from the list of values associated with a given key (there can be many values under the same key); if this is the last value under the key, the key will be removed as well.
static boolean replaceValue(java.util.Map map, java.lang.Object key, java.lang.Object oldVal, java.lang.Object newVal)
          Replaces one value in the list of values with another one.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MultiValueMap

public MultiValueMap()
Class constructor

Method Detail

addValue

public static boolean addValue(java.util.Map map,
                               java.lang.Object key,
                               java.lang.Object val)
Adds a value to a collection of values under a specific key. If there already is a value under the key and it is not a collection, this method does nothing and returns false; otherwise it returns true. (if the value is already there, the new one does not get added)

Parameters:
map - object holding the key-val(s) binding
key - identifier for a specific binding
val - value to be referenced by key
Returns:
True if successful insert or val already present, False otherwise

addValues

public static boolean addValues(java.util.Map map,
                                java.lang.Object key,
                                java.util.Collection vals)
Adds a whole collection of values to a collection of values under a specific key. If there already is a value under the key and it is not a collection, this method does nothing and returns false; otherwise it returns true. (if the any of the values is already there, the new one does not get added)

Parameters:
map - object holding the key-val(s) binding
key - identifier for a specific binding
vals - a collection of values to be referenced by key
Returns:
True if successful insert or vals already present, False otherwise

containsValue

public static boolean containsValue(java.util.Map map,
                                    java.lang.Object key,
                                    java.lang.Object val)
Returns true if the list of values for a key contains the value; false otherwise

Parameters:
map - object holding the key-val(s) binding
key - identifier for a specific binding
val - value to be referenced by key
Returns:
true if the list of values for a key contains the value; false otherwise

removeValue

public static boolean removeValue(java.util.Map map,
                                  java.lang.Object key,
                                  java.lang.Object val)
Remove the value from the list of values associated with a given key (there can be many values under the same key); if this is the last value under the key, the key will be removed as well.

Parameters:
map - object holding the key-val(s) binding
key - identifier for a specific binding
val - value to be referenced by key
Returns:
true if there was something there, false otherwise

replaceValue

public static boolean replaceValue(java.util.Map map,
                                   java.lang.Object key,
                                   java.lang.Object oldVal,
                                   java.lang.Object newVal)
Replaces one value in the list of values with another one. Returns true if the old value was there and the new one was added successfully.

Parameters:
map - object holding the key-val(s) binding
key - identifier for a specific binding
oldVal - previous value to remove
newVal - value to insert
Returns:
true if remove and insert are successful, false otherwise

getValues

public static java.util.Vector getValues(java.util.Map map)
Returns a collection of all values contained in the map

Parameters:
map - input
Returns:
a collection of all values inside this map

isEmpty

public static boolean isEmpty(java.util.Map map)
Test if any keys in the map have values

Parameters:
map -
Returns:

getNumValuesFor

public static int getNumValuesFor(java.util.Map map,
                                  java.lang.Object key)
Returns the number of values associated with a key

Parameters:
map - a particular map to be used
key - the key, whose values we want to count
Returns:
the number of values for the key