// ======================================================================== // Copyright (c) 1999 Mort Bay Consulting (Australia) Pty. Ltd. // $Id: MultiMap.java,v 1.15.2.5 2003/06/04 04:47:58 starksm Exp $ // ======================================================================== package org.mortbay.util; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; /* ------------------------------------------------------------ */ /** A multi valued Map. * This Map specializes HashMap and provides methods * that operate on multi valued items. *
* Implemented as a map of LazyList values
*
* @see LazyList
* @version $Id: MultiMap.java,v 1.15.2.5 2003/06/04 04:47:58 starksm Exp $
* @author Greg Wilkins (gregw)
*/
public class MultiMap extends HashMap
implements Cloneable
{
/* ------------------------------------------------------------ */
/** Constructor.
*/
public MultiMap()
{}
/* ------------------------------------------------------------ */
/** Constructor.
* @param size Capacity of the map
*/
public MultiMap(int size)
{
super(size);
}
/* ------------------------------------------------------------ */
/** Constructor.
* @param map
*/
public MultiMap(Map map)
{
super((map.size()*3)/2);
putAll(map);
}
/* ------------------------------------------------------------ */
/** Get multiple values.
* Single valued entries are converted to singleton lists.
* @param name The entry key.
* @return Unmodifieable List of values.
*/
public List getValues(Object name)
{
return LazyList.getList(super.get(name),true);
}
/* ------------------------------------------------------------ */
/** Get a value from a multiple value.
* If the value is not a multivalue, then index 0 retrieves the
* value or null.
* @param name The entry key.
* @param i Index of element to get.
* @return Unmodifieable List of values.
*/
public Object getValue(Object name,int i)
{
Object l=super.get(name);
if (i==0 && LazyList.size(l)==0)
return null;
return LazyList.get(l,i);
}
/* ------------------------------------------------------------ */
/** Get value as String.
* Single valued items are converted to a String with the toString()
* Object method. Multi valued entries are converted to a comma separated
* List. No quoting of commas within values is performed.
* @param name The entry key.
* @return String value.
*/
public String getString(Object name)
{
Object l=super.get(name);
switch(LazyList.size(l))
{
case 0:
return null;
case 1:
Object o=LazyList.get(l,0);
return o==null?null:o.toString();
default:
StringBuffer values=new StringBuffer(128);
synchronized(values)
{
for (int i=0; i