com.bristle.javalib.util
Class MapUtil

java.lang.Object
  extended by com.bristle.javalib.util.MapUtil

public class MapUtil
extends Object

This class contains utility routines for creating and manipulating Java Maps.

Usage:
   - The typical scenario for using this class is:
       Map map = MapUtil.createMap(arr1, arr2);

   - See the source code of the inner Tester class for more examples.
  
Assumptions:
Effects:
       - None.
Anticipated Changes:
Notes:
Implementation Notes:
Portability Issues:
Revision History:
   $Log$


Nested Class Summary
static class MapUtil.DifferentNumberOfKeysAndValues
          This exception is thrown when the number of specified keys is not the same as the number of specified values.
static class MapUtil.Tester
          Each class contains a Tester inner class with a main() for easier unit testing.
 
Constructor Summary
MapUtil()
           
 
Method Summary
static Map createMap(Enumeration enumeration)
          Create a Map from the specified Enumeration, using Integer values 0, 1, 2, and so on as the key values.
static Map createMap(Enumeration enumKeys, Enumeration enumVals)
          Create a Map from the specified Enumerations of keys and values.
static Map createMap(Iterator iter)
          Create a Map from the specified Iterator, using Integer values 0, 1, 2, and so on as the key values.
static Map createMap(Iterator iterKeys, Iterator iterVals)
          Create a Map from the specified Iterators of keys and values.
static Map createMap(List list)
          Create a Map from the specified List, using Integer values 0, 1, 2, and so on as the key values.
static Map createMap(List listKeys, List listVals)
          Create a Map from the specified Lists of keys and values.
static Map createMap(Object[] arr)
          Create a Map from the specified array, using Integer values 0, 1, 2, and so on as the key values.
static Map createMap(Object[] arrKeys, Object[] arrVals)
          Create a Map from the specified arrays of keys and values.
static String mapToString(Map map)
          Return a string version of the Map.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MapUtil

public MapUtil()
Method Detail

mapToString

public static String mapToString(Map map)
Return a string version of the Map.

Parameters:
map - Map to convert to a String.
Returns:
String version of the Map, or null if map was null.

createMap

public static Map createMap(Iterator iter)
Create a Map from the specified Iterator, using Integer values 0, 1, 2, and so on as the key values. Note: This leaves the Iterator with hasNext() == false.

Parameters:
iter - Iterator of Objects to put in the Map.
Returns:
The new Map.

createMap

public static Map createMap(Enumeration enumeration)
Create a Map from the specified Enumeration, using Integer values 0, 1, 2, and so on as the key values. Note: This leaves the Enumeration with hasMoreElements() == false.

Parameters:
enumeration - Enumeration of Objects to put in the Map.
Returns:
The new Map.

createMap

public static Map createMap(List list)
Create a Map from the specified List, using Integer values 0, 1, 2, and so on as the key values.

Parameters:
list - List of Objects to put in the Map.
Returns:
The new Map.

createMap

public static Map createMap(Object[] arr)
Create a Map from the specified array, using Integer values 0, 1, 2, and so on as the key values.

Parameters:
arr - Array of Objects to put in the Map.
Returns:
The new Map.

createMap

public static Map createMap(Iterator iterKeys,
                            Iterator iterVals)
                     throws MapUtil.DifferentNumberOfKeysAndValues
Create a Map from the specified Iterators of keys and values. Returns null if the Iterator of keys is null and the Iterator of values is null or empty. Returns an empty Map if the Iterator of keys is empty and the Iterator of values is null or empty. Note: If the same key occurs more than once, the later value overwrites the earlier value in the Map. Note: This leaves the Iterators with hasNext() == false.

Parameters:
iterKeys - Iterator of Objects to use as keys in the Map.
iterVals - Iterator of Objects to use as values in the Map.
Returns:
The new Map.
Throws:
MapUtil.DifferentNumberOfKeysAndValues - When the Iterators do not contain the same number of elements.

createMap

public static Map createMap(Enumeration enumKeys,
                            Enumeration enumVals)
                     throws MapUtil.DifferentNumberOfKeysAndValues
Create a Map from the specified Enumerations of keys and values. Returns null if the Enumeration of keys is null and the Enumeration of values is null or empty. Returns an empty Map if the Enumeration of keys is empty and the Enumeration of values is null or empty. Note: If the same key occurs more than once, the later value overwrites the earlier value in the Map. Note: This leaves the Enumerations with hasMoreElements() == false.

Parameters:
enumKeys - Enumeration of Objects to use as keys in the Map.
enumVals - Enumeration of Objects to use as values in the Map.
Returns:
The new Map.
Throws:
MapUtil.DifferentNumberOfKeysAndValues - When the Enumerations are not the same size.

createMap

public static Map createMap(List listKeys,
                            List listVals)
                     throws MapUtil.DifferentNumberOfKeysAndValues
Create a Map from the specified Lists of keys and values. Returns null if the List of keys is null and the List of values is null or empty. Returns an empty Map if the List of keys is empty and the List of values is null or empty. Note: If the same key occurs more than once, the later value overwrites the earlier value in the Map.

Parameters:
listKeys - List of Objects to use as keys in the Map.
listVals - List of Objects to use as values in the Map.
Returns:
The new Map.
Throws:
MapUtil.DifferentNumberOfKeysAndValues - When the Lists are not the same size.

createMap

public static Map createMap(Object[] arrKeys,
                            Object[] arrVals)
                     throws MapUtil.DifferentNumberOfKeysAndValues
Create a Map from the specified arrays of keys and values. Returns null if the array of keys is null and the array of values is null or empty. Returns an empty Map if the array of keys is empty and the array of values is null or empty. Note: If the same key occurs more than once, the later value overwrites the earlier value in the Map.

Parameters:
arrKeys - Array of Objects to use as keys in the Map.
arrVals - Array of Objects to use as values in the Map.
Returns:
The new Map.
Throws:
MapUtil.DifferentNumberOfKeysAndValues - When the arrays are not the same size.