* Implemented as a map of LazyList values
*
* @see LazyList
* @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; iUsage
*
* Object lazylist =null;
* while(loopCondition)
* {
* Object item = getItem();
* if (item.isToBeAdded())
* lazylist = LazyList.add(lazylist,item);
* }
* return LazyList.getList(lazylist);
*
*
* An ArrayList of default size is used as the initial LazyList.
*
* @see java.util.List
* @author Greg Wilkins (gregw)
*/
class LazyList
implements Cloneable, Serializable
{
private static final String[] __EMTPY_STRING_ARRAY = new String[0];
/* ------------------------------------------------------------ */
private LazyList()
{}
/* ------------------------------------------------------------ */
/** Add an item to a LazyList
* @param list The list to add to or null if none yet created.
* @param item The item to add.
* @return The lazylist created or added to.
*/
public static Object add(Object list, Object item)
{
if (list==null)
{
if (item instanceof List || item==null)
{
List l = new ArrayList();
l.add(item);
return l;
}
return item;
}
if (list instanceof List)
{
((List)list).add(item);
return list;
}
List l=new ArrayList();
l.add(list);
l.add(item);
return l;
}
/* ------------------------------------------------------------ */
/** Add an item to a LazyList
* @param list The list to add to or null if none yet created.
* @param index The index to add the item at.
* @param item The item to add.
* @return The lazylist created or added to.
*/
public static Object add(Object list, int index, Object item)
{
if (list==null)
{
if (index>0 || item instanceof List || item==null)
{
List l = new ArrayList();
l.add(index,item);
return l;
}
return item;
}
if (list instanceof List)
{
((List)list).add(index,item);
return list;
}
List l=new ArrayList();
l.add(list);
l.add(index,item);
return l;
}
/* ------------------------------------------------------------ */
/** Add the contents of a Collection to a LazyList
* @param list The list to add to or null if none yet created.
* @param collection The Collection whose contents should be added.
* @return The lazylist created or added to.
*/
public static Object addCollection(Object list, Collection collection)
{
Iterator i=collection.iterator();
while(i.hasNext())
list=LazyList.add(list,i.next());
return list;
}
/* ------------------------------------------------------------ */
/** Add the contents of an array to a LazyList
* @param list The list to add to or null if none yet created.
* @param collection The Collection whose contents should be added.
* @return The lazylist created or added to.
*/
public static Object addArray(Object list, Object[] array)
{
for(int i=0;array!=null && iarray
.
*/
public static List array2List(Object[] array)
{
if (array==null || array.length==0)
return new ArrayList();
return new ArrayList(Arrays.asList(array));
}
/* ------------------------------------------------------------ */
/** Add element to an array
* @param array The array to add to (or null)
* @param item The item to add
* @param type The type of the array (in case of null array)
* @return new array with contents of array plus item
*/
public static Object[] addToArray(Object[] array, Object item, Class type)
{
if (array==null)
{
if (type==null && item!=null)
type= item.getClass();
Object[] na = (Object[])Array.newInstance(type, 1);
na[0]=item;
return na;
}
else
{
Class c = array.getClass().getComponentType();
Object[] na = (Object[])Array.newInstance(c, Array.getLength(array)+1);
System.arraycopy(array, 0, na, 0, array.length);
na[array.length]=item;
return na;
}
}
/* ------------------------------------------------------------ */
public static Object[] removeFromArray(Object[] array, Object item)
{
if (item==null || array==null)
return array;
for (int i=array.length;i-->0;)
{
if (item.equals(array[i]))
{
Class c = array==null?item.getClass():array.getClass().getComponentType();
Object[] na = (Object[])Array.newInstance(c, Array.getLength(array)-1);
if (i>0)
System.arraycopy(array, 0, na, 0, i);
if (i+1