[multimap-ng] step 2, make MultiMap a generic, and remove LazyList use
This commit is contained in:
parent
74490580da
commit
dfebb117b7
|
@ -17,8 +17,6 @@ import java.io.IOException;
|
|||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.DispatcherType;
|
||||
import javax.servlet.RequestDispatcher;
|
||||
|
@ -30,7 +28,6 @@ import javax.servlet.http.HttpServletResponse;
|
|||
|
||||
import org.eclipse.jetty.server.handler.ContextHandler;
|
||||
import org.eclipse.jetty.util.Attributes;
|
||||
import org.eclipse.jetty.util.LazyList;
|
||||
import org.eclipse.jetty.util.MultiMap;
|
||||
import org.eclipse.jetty.util.UrlEncoded;
|
||||
|
||||
|
@ -128,7 +125,7 @@ public class Dispatcher implements RequestDispatcher
|
|||
|
||||
final DispatcherType old_type = baseRequest.getDispatcherType();
|
||||
final Attributes old_attr=baseRequest.getAttributes();
|
||||
MultiMap old_params=baseRequest.getParameters();
|
||||
MultiMap<String> old_params=baseRequest.getParameters();
|
||||
try
|
||||
{
|
||||
baseRequest.setDispatcherType(DispatcherType.INCLUDE);
|
||||
|
@ -148,21 +145,12 @@ public class Dispatcher implements RequestDispatcher
|
|||
old_params=baseRequest.getParameters();
|
||||
}
|
||||
|
||||
MultiMap parameters=new MultiMap();
|
||||
MultiMap<String> parameters=new MultiMap<>();
|
||||
UrlEncoded.decodeTo(query,parameters,baseRequest.getCharacterEncoding());
|
||||
|
||||
if (old_params!=null && old_params.size()>0)
|
||||
{
|
||||
if(old_params != null) {
|
||||
// Merge parameters.
|
||||
Iterator iter = old_params.entrySet().iterator();
|
||||
while (iter.hasNext())
|
||||
{
|
||||
Map.Entry entry = (Map.Entry)iter.next();
|
||||
String name=(String)entry.getKey();
|
||||
Object values=entry.getValue();
|
||||
for (int i=0;i<LazyList.size(values);i++)
|
||||
parameters.add(name, LazyList.get(values, i));
|
||||
}
|
||||
parameters.addAllValues(old_params);
|
||||
}
|
||||
baseRequest.setParameters(parameters);
|
||||
}
|
||||
|
@ -215,7 +203,7 @@ public class Dispatcher implements RequestDispatcher
|
|||
final String old_query=baseRequest.getQueryString();
|
||||
final Attributes old_attr=baseRequest.getAttributes();
|
||||
final DispatcherType old_type=baseRequest.getDispatcherType();
|
||||
MultiMap old_params=baseRequest.getParameters();
|
||||
MultiMap<String> old_params=baseRequest.getParameters();
|
||||
|
||||
try
|
||||
{
|
||||
|
|
|
@ -30,11 +30,9 @@ import java.util.Collections;
|
|||
import java.util.Enumeration;
|
||||
import java.util.EventListener;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import javax.servlet.AsyncContext;
|
||||
import javax.servlet.AsyncListener;
|
||||
|
@ -68,7 +66,6 @@ import org.eclipse.jetty.server.handler.ContextHandler;
|
|||
import org.eclipse.jetty.server.handler.ContextHandler.Context;
|
||||
import org.eclipse.jetty.util.Attributes;
|
||||
import org.eclipse.jetty.util.AttributesMap;
|
||||
import org.eclipse.jetty.util.LazyList;
|
||||
import org.eclipse.jetty.util.MultiMap;
|
||||
import org.eclipse.jetty.util.MultiPartInputStream;
|
||||
import org.eclipse.jetty.util.StringUtil;
|
||||
|
@ -126,7 +123,7 @@ public class Request implements HttpServletRequest
|
|||
private boolean _asyncSupported = true;
|
||||
private volatile Attributes _attributes;
|
||||
private Authentication _authentication;
|
||||
private MultiMap _baseParameters;
|
||||
private MultiMap<String> _baseParameters;
|
||||
private String _characterEncoding;
|
||||
private ContextHandler.Context _context;
|
||||
private boolean _newContext;
|
||||
|
@ -138,7 +135,7 @@ public class Request implements HttpServletRequest
|
|||
private int _inputState = __NONE;
|
||||
private HttpMethod _httpMethod;
|
||||
private String _method;
|
||||
private MultiMap _parameters;
|
||||
private MultiMap<String> _parameters;
|
||||
private boolean _paramsExtracted;
|
||||
private String _pathInfo;
|
||||
private int _port;
|
||||
|
@ -192,7 +189,7 @@ public class Request implements HttpServletRequest
|
|||
public void extractParameters()
|
||||
{
|
||||
if (_baseParameters == null)
|
||||
_baseParameters = new MultiMap();
|
||||
_baseParameters = new MultiMap<>();
|
||||
|
||||
if (_paramsExtracted)
|
||||
{
|
||||
|
@ -283,15 +280,7 @@ public class Request implements HttpServletRequest
|
|||
else if (_parameters != _baseParameters)
|
||||
{
|
||||
// Merge parameters (needed if parameters extracted after a forward).
|
||||
Iterator<?> iter = _baseParameters.entrySet().iterator();
|
||||
while (iter.hasNext())
|
||||
{
|
||||
Map.Entry<?, ?> entry = (Map.Entry<?, ?>)iter.next();
|
||||
String name = (String)entry.getKey();
|
||||
Object values = entry.getValue();
|
||||
for (int i = 0; i < LazyList.size(values); i++)
|
||||
_parameters.add(name,LazyList.get(values,i));
|
||||
}
|
||||
_parameters.addAllValues(_baseParameters);
|
||||
}
|
||||
}
|
||||
finally
|
||||
|
@ -335,10 +324,10 @@ public class Request implements HttpServletRequest
|
|||
* @see javax.servlet.ServletRequest#getAttributeNames()
|
||||
*/
|
||||
@Override
|
||||
public Enumeration getAttributeNames()
|
||||
public Enumeration<String> getAttributeNames()
|
||||
{
|
||||
if (_attributes == null)
|
||||
return Collections.enumeration(Collections.EMPTY_LIST);
|
||||
return Collections.enumeration(Collections.<String>emptyList());
|
||||
|
||||
return AttributesMap.getAttributeNamesCopy(_attributes);
|
||||
}
|
||||
|
@ -499,7 +488,7 @@ public class Request implements HttpServletRequest
|
|||
* @see javax.servlet.http.HttpServletRequest#getHeaderNames()
|
||||
*/
|
||||
@Override
|
||||
public Enumeration getHeaderNames()
|
||||
public Enumeration<String> getHeaderNames()
|
||||
{
|
||||
return _fields.getFieldNames();
|
||||
}
|
||||
|
@ -509,11 +498,11 @@ public class Request implements HttpServletRequest
|
|||
* @see javax.servlet.http.HttpServletRequest#getHeaders(java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public Enumeration getHeaders(String name)
|
||||
public Enumeration<String> getHeaders(String name)
|
||||
{
|
||||
Enumeration<?> e = _fields.getValues(name);
|
||||
Enumeration<String> e = _fields.getValues(name);
|
||||
if (e == null)
|
||||
return Collections.enumeration(Collections.EMPTY_LIST);
|
||||
return Collections.enumeration(Collections.<String>emptyList());
|
||||
return e;
|
||||
}
|
||||
|
||||
|
@ -691,7 +680,7 @@ public class Request implements HttpServletRequest
|
|||
* @see javax.servlet.ServletRequest#getParameterMap()
|
||||
*/
|
||||
@Override
|
||||
public Map getParameterMap()
|
||||
public Map<String, String[]> getParameterMap()
|
||||
{
|
||||
if (!_paramsExtracted)
|
||||
extractParameters();
|
||||
|
@ -715,7 +704,7 @@ public class Request implements HttpServletRequest
|
|||
/**
|
||||
* @return Returns the parameters.
|
||||
*/
|
||||
public MultiMap getParameters()
|
||||
public MultiMap<String> getParameters()
|
||||
{
|
||||
return _parameters;
|
||||
}
|
||||
|
@ -1691,7 +1680,7 @@ public class Request implements HttpServletRequest
|
|||
* @param parameters
|
||||
* The parameters to set.
|
||||
*/
|
||||
public void setParameters(MultiMap parameters)
|
||||
public void setParameters(MultiMap<String> parameters)
|
||||
{
|
||||
_parameters = (parameters == null)?_baseParameters:parameters;
|
||||
if (_paramsExtracted && _parameters == null)
|
||||
|
@ -2017,7 +2006,7 @@ public class Request implements HttpServletRequest
|
|||
public void mergeQueryString(String query)
|
||||
{
|
||||
// extract parameters from dispatch query
|
||||
MultiMap parameters = new MultiMap();
|
||||
MultiMap<String> parameters = new MultiMap<>();
|
||||
UrlEncoded.decodeTo(query,parameters,getCharacterEncoding());
|
||||
|
||||
boolean merge_old_query = false;
|
||||
|
@ -2030,21 +2019,7 @@ public class Request implements HttpServletRequest
|
|||
if (_parameters != null && _parameters.size() > 0)
|
||||
{
|
||||
// Merge parameters; new parameters of the same name take precedence.
|
||||
Iterator<Entry<String, Object>> iter = _parameters.entrySet().iterator();
|
||||
while (iter.hasNext())
|
||||
{
|
||||
Map.Entry<String, Object> entry = iter.next();
|
||||
String name = entry.getKey();
|
||||
|
||||
// If the names match, we will need to remake the query string
|
||||
if (parameters.containsKey(name))
|
||||
merge_old_query = true;
|
||||
|
||||
// Add the old values to the new parameter map
|
||||
Object values = entry.getValue();
|
||||
for (int i = 0; i < LazyList.size(values); i++)
|
||||
parameters.add(name,LazyList.get(values,i));
|
||||
}
|
||||
merge_old_query = parameters.addAllValues(_parameters);
|
||||
}
|
||||
|
||||
if (_queryString != null && _queryString.length() > 0)
|
||||
|
@ -2052,23 +2027,20 @@ public class Request implements HttpServletRequest
|
|||
if (merge_old_query)
|
||||
{
|
||||
StringBuilder overridden_query_string = new StringBuilder();
|
||||
MultiMap overridden_old_query = new MultiMap();
|
||||
MultiMap<String> overridden_old_query = new MultiMap<>();
|
||||
UrlEncoded.decodeTo(_queryString,overridden_old_query,getCharacterEncoding());
|
||||
|
||||
MultiMap overridden_new_query = new MultiMap();
|
||||
MultiMap<String> overridden_new_query = new MultiMap<>();
|
||||
UrlEncoded.decodeTo(query,overridden_new_query,getCharacterEncoding());
|
||||
|
||||
Iterator<Entry<String, Object>> iter = overridden_old_query.entrySet().iterator();
|
||||
while (iter.hasNext())
|
||||
for(String name: overridden_old_query.keySet())
|
||||
{
|
||||
Map.Entry<String, Object> entry = iter.next();
|
||||
String name = entry.getKey();
|
||||
if (!overridden_new_query.containsKey(name))
|
||||
{
|
||||
Object values = entry.getValue();
|
||||
for (int i = 0; i < LazyList.size(values); i++)
|
||||
List<String> values = overridden_old_query.get(name);
|
||||
for(String v: values)
|
||||
{
|
||||
overridden_query_string.append("&").append(name).append("=").append(LazyList.get(values,i));
|
||||
overridden_query_string.append("&").append(name).append("=").append(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -99,7 +99,7 @@ public class ServletHandler extends ScopedHandler
|
|||
|
||||
private final Map<String,FilterHolder> _filterNameMap= new HashMap<>();
|
||||
private List<FilterMapping> _filterPathMappings;
|
||||
private MultiMap _filterNameMappings;
|
||||
private MultiMap<FilterMapping> _filterNameMappings;
|
||||
|
||||
private final Map<String,ServletHolder> _servletNameMap=new HashMap<>();
|
||||
private PathMap _servletPathMap;
|
||||
|
@ -1109,7 +1109,7 @@ public class ServletHandler extends ScopedHandler
|
|||
else
|
||||
{
|
||||
_filterPathMappings=new ArrayList<>();
|
||||
_filterNameMappings=new MultiMap();
|
||||
_filterNameMappings=new MultiMap<FilterMapping>();
|
||||
for (FilterMapping filtermapping : _filterMappings)
|
||||
{
|
||||
FilterHolder filter_holder = _filterNameMap.get(filtermapping.getFilterName());
|
||||
|
|
|
@ -13,28 +13,31 @@
|
|||
|
||||
package org.eclipse.jetty.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
/**
|
||||
* A multi valued Map.
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class MultiMap extends HashMap<String,Object>
|
||||
public class MultiMap<V> extends HashMap<String,List<V>>
|
||||
{
|
||||
public MultiMap()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public MultiMap(Map<String,Object> map)
|
||||
public MultiMap(Map<String,List<V>> map)
|
||||
{
|
||||
super(map);
|
||||
}
|
||||
|
||||
public MultiMap(MultiMap map)
|
||||
public MultiMap(MultiMap<V> map)
|
||||
{
|
||||
super(map);
|
||||
}
|
||||
|
@ -46,9 +49,13 @@ public class MultiMap extends HashMap<String,Object>
|
|||
* @param name The entry key.
|
||||
* @return Unmodifieable List of values.
|
||||
*/
|
||||
public List<String> getValues(String name)
|
||||
public List<V> getValues(String name)
|
||||
{
|
||||
return LazyList.getList(get(name),true);
|
||||
List<V> vals = super.get(name);
|
||||
if((vals == null) || vals.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return vals;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
@ -59,12 +66,16 @@ public class MultiMap extends HashMap<String,Object>
|
|||
* @param i Index of element to get.
|
||||
* @return Unmodifieable List of values.
|
||||
*/
|
||||
public Object getValue(String name,int i)
|
||||
public V getValue(String name,int i)
|
||||
{
|
||||
Object l=get(name);
|
||||
if (i==0 && LazyList.size(l)==0)
|
||||
List<V> vals = getValues(name);
|
||||
if(vals == null) {
|
||||
return null;
|
||||
return LazyList.get(l,i);
|
||||
}
|
||||
if (i==0 && vals.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return vals.get(i);
|
||||
}
|
||||
|
||||
|
||||
|
@ -78,46 +89,57 @@ public class MultiMap extends HashMap<String,Object>
|
|||
*/
|
||||
public String getString(String name)
|
||||
{
|
||||
Object l=get(name);
|
||||
switch(LazyList.size(l))
|
||||
List<V> vals =get(name);
|
||||
if ((vals == null) || (vals.isEmpty()))
|
||||
{
|
||||
case 0:
|
||||
return null;
|
||||
case 1:
|
||||
Object o=LazyList.get(l,0);
|
||||
return o==null?null:o.toString();
|
||||
default:
|
||||
{
|
||||
StringBuilder values=new StringBuilder(128);
|
||||
for (int i=0; i<LazyList.size(l); i++)
|
||||
{
|
||||
Object e=LazyList.get(l,i);
|
||||
if (e!=null)
|
||||
{
|
||||
if (values.length()>0)
|
||||
values.append(',');
|
||||
values.append(e.toString());
|
||||
}
|
||||
}
|
||||
return values.toString();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
if (vals.size() == 1)
|
||||
{
|
||||
// simple form.
|
||||
return vals.get(0).toString();
|
||||
}
|
||||
|
||||
// delimited form
|
||||
StringBuilder values=new StringBuilder(128);
|
||||
for (V e : vals)
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
if (values.length() > 0)
|
||||
values.append(',');
|
||||
values.append(e.toString());
|
||||
}
|
||||
}
|
||||
return values.toString();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
@Override
|
||||
public Object get(Object name)
|
||||
/**
|
||||
* Put multi valued entry.
|
||||
* @param name The entry key.
|
||||
* @param value The simple value
|
||||
* @return The previous value or null.
|
||||
*/
|
||||
public List<V> put(String name, V value)
|
||||
{
|
||||
Object l=super.get(name);
|
||||
switch(LazyList.size(l))
|
||||
if(value == null) {
|
||||
return super.put(name, null);
|
||||
}
|
||||
List<V> vals = new ArrayList<>();
|
||||
vals.add(value);
|
||||
return put(name,vals);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shorthand version of putAll
|
||||
* @param input the input map
|
||||
*/
|
||||
public void putAllValues(Map<String, V> input)
|
||||
{
|
||||
for(Map.Entry<String,V> entry: input.entrySet())
|
||||
{
|
||||
case 0:
|
||||
return null;
|
||||
case 1:
|
||||
Object o=LazyList.get(l,0);
|
||||
return o;
|
||||
default:
|
||||
return LazyList.getList(l,true);
|
||||
put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -127,23 +149,23 @@ public class MultiMap extends HashMap<String,Object>
|
|||
* @param values The List of multiple values.
|
||||
* @return The previous value or null.
|
||||
*/
|
||||
public Object putValues(String name, List<? extends Object> values)
|
||||
public List<V> putValues(String name, List<V> values)
|
||||
{
|
||||
return put(name,values);
|
||||
return super.put(name,values);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Put multi valued entry.
|
||||
* @param name The entry key.
|
||||
* @param values The String array of multiple values.
|
||||
* @param values The array of multiple values.
|
||||
* @return The previous value or null.
|
||||
*/
|
||||
public Object putValues(String name, String... values)
|
||||
@SafeVarargs
|
||||
public final List<V> putValues(String name, V... values)
|
||||
{
|
||||
Object list=null;
|
||||
for (int i=0;i<values.length;i++)
|
||||
list=LazyList.add(list,values[i]);
|
||||
return put(name,list);
|
||||
List<V> list = new ArrayList<>();
|
||||
list.addAll(Arrays.asList(values));
|
||||
return super.put(name,list);
|
||||
}
|
||||
|
||||
|
||||
|
@ -154,12 +176,14 @@ public class MultiMap extends HashMap<String,Object>
|
|||
* @param name The entry key.
|
||||
* @param value The entry value.
|
||||
*/
|
||||
public void add(String name, Object value)
|
||||
public void add(String name, V value)
|
||||
{
|
||||
Object lo = get(name);
|
||||
Object ln = LazyList.add(lo,value);
|
||||
if (lo!=ln)
|
||||
put(name,ln);
|
||||
List<V> lo = get(name);
|
||||
if(lo == null) {
|
||||
lo = new ArrayList<>();
|
||||
}
|
||||
lo.add(value);
|
||||
super.put(name,lo);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
@ -169,12 +193,14 @@ public class MultiMap extends HashMap<String,Object>
|
|||
* @param name The entry key.
|
||||
* @param values The List of multiple values.
|
||||
*/
|
||||
public void addValues(String name, List<? extends Object> values)
|
||||
public void addValues(String name, List<V> values)
|
||||
{
|
||||
Object lo = get(name);
|
||||
Object ln = LazyList.addCollection(lo,values);
|
||||
if (lo!=ln)
|
||||
put(name,ln);
|
||||
List<V> lo = get(name);
|
||||
if(lo == null) {
|
||||
lo = new ArrayList<>();
|
||||
}
|
||||
lo.addAll(values);
|
||||
put(name,lo);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
@ -184,12 +210,47 @@ public class MultiMap extends HashMap<String,Object>
|
|||
* @param name The entry key.
|
||||
* @param values The String array of multiple values.
|
||||
*/
|
||||
public void addValues(String name, String[] values)
|
||||
public void addValues(String name, V[] values)
|
||||
{
|
||||
Object lo = get(name);
|
||||
Object ln = LazyList.addCollection(lo,Arrays.asList(values));
|
||||
if (lo!=ln)
|
||||
put(name,ln);
|
||||
List<V> lo = get(name);
|
||||
if(lo == null) {
|
||||
lo = new ArrayList<>();
|
||||
}
|
||||
lo.addAll(Arrays.asList(values));
|
||||
put(name,lo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge values.
|
||||
*
|
||||
* @param the
|
||||
* map to overlay on top of this one, merging together values if needed.
|
||||
* @return true if an existing key was merged with potentially new values, false if either no change was made, or there were only new keys.
|
||||
*/
|
||||
public boolean addAllValues(MultiMap<V> map)
|
||||
{
|
||||
boolean merged = false;
|
||||
|
||||
if ((map == null) || (map.isEmpty()))
|
||||
{
|
||||
// done
|
||||
return merged;
|
||||
}
|
||||
|
||||
for (Map.Entry<String, List<V>> entry : map.entrySet())
|
||||
{
|
||||
String name = entry.getKey();
|
||||
List<V> values = entry.getValue();
|
||||
|
||||
if (this.containsKey(name))
|
||||
{
|
||||
merged = true;
|
||||
}
|
||||
|
||||
this.addValues(name,values);
|
||||
}
|
||||
|
||||
return merged;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
@ -198,20 +259,70 @@ public class MultiMap extends HashMap<String,Object>
|
|||
* @param value The entry value.
|
||||
* @return true if it was removed.
|
||||
*/
|
||||
public boolean removeValue(String name,Object value)
|
||||
public boolean removeValue(String name,V value)
|
||||
{
|
||||
Object lo = get(name);
|
||||
Object ln=lo;
|
||||
int s=LazyList.size(lo);
|
||||
if (s>0)
|
||||
{
|
||||
ln=LazyList.remove(lo,value);
|
||||
if (ln==null)
|
||||
remove(name);
|
||||
else
|
||||
put(name, ln);
|
||||
List<V> lo = get(name);
|
||||
if((lo == null)||(lo.isEmpty())) {
|
||||
return false;
|
||||
}
|
||||
return LazyList.size(ln)!=s;
|
||||
boolean ret = lo.remove(value);
|
||||
if(lo.isEmpty()) {
|
||||
remove(name);
|
||||
} else {
|
||||
put(name,lo);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for a specific single value in the map.
|
||||
* <p>
|
||||
* NOTE: This is a SLOW operation, and is actively discouraged.
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
public boolean containsSimpleValue(V value)
|
||||
{
|
||||
for (List<V> vals : values())
|
||||
{
|
||||
if ((vals.size() == 1) && vals.contains(value))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
Iterator<Entry<String, List<V>>> iter = entrySet().iterator();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append('{');
|
||||
boolean delim = false;
|
||||
while (iter.hasNext())
|
||||
{
|
||||
Entry<String, List<V>> e = iter.next();
|
||||
if (delim)
|
||||
{
|
||||
sb.append(", ");
|
||||
}
|
||||
String key = e.getKey();
|
||||
List<V> vals = e.getValue();
|
||||
sb.append(key);
|
||||
sb.append('=');
|
||||
if (vals.size() == 1)
|
||||
{
|
||||
sb.append(vals.get(0));
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.append(vals);
|
||||
}
|
||||
delim = true;
|
||||
}
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
@ -241,11 +352,17 @@ public class MultiMap extends HashMap<String,Object>
|
|||
}
|
||||
};
|
||||
|
||||
for(Map.Entry<String,Object> entry: entrySet())
|
||||
for(Map.Entry<String,List<V>> entry: entrySet())
|
||||
{
|
||||
String[] a = LazyList.toStringArray(entry.getValue());
|
||||
String[] a = null;
|
||||
if (entry.getValue() != null)
|
||||
{
|
||||
a = new String[entry.getValue().size()];
|
||||
a = entry.getValue().toArray(a);
|
||||
}
|
||||
map.put(entry.getKey(),a);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ public class MultiMapTest
|
|||
@Test
|
||||
public void testPut()
|
||||
{
|
||||
MultiMap mm = new MultiMap();
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
|
||||
String key = "formats";
|
||||
|
||||
|
@ -42,13 +42,30 @@ public class MultiMapTest
|
|||
* Tests {@link MultiMap#put(Object, Object)}
|
||||
*/
|
||||
@Test
|
||||
public void testPut_Null()
|
||||
public void testPut_Null_String()
|
||||
{
|
||||
MultiMap mm = new MultiMap();
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
|
||||
String key = "formats";
|
||||
String val = null;
|
||||
|
||||
mm.put(key,null);
|
||||
mm.put(key,val);
|
||||
assertMapSize(mm,1);
|
||||
assertNullValues(mm,key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link MultiMap#put(Object, Object)}
|
||||
*/
|
||||
@Test
|
||||
public void testPut_Null_List()
|
||||
{
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
|
||||
String key = "formats";
|
||||
List<String> vals = null;
|
||||
|
||||
mm.put(key,vals);
|
||||
assertMapSize(mm,1);
|
||||
assertNullValues(mm,key);
|
||||
}
|
||||
|
@ -59,7 +76,7 @@ public class MultiMapTest
|
|||
@Test
|
||||
public void testPut_Replace()
|
||||
{
|
||||
MultiMap mm = new MultiMap();
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
|
||||
String key = "formats";
|
||||
Object ret;
|
||||
|
@ -83,7 +100,7 @@ public class MultiMapTest
|
|||
@Test
|
||||
public void testPutValues_List()
|
||||
{
|
||||
MultiMap mm = new MultiMap();
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
|
||||
String key = "formats";
|
||||
|
||||
|
@ -103,7 +120,7 @@ public class MultiMapTest
|
|||
@Test
|
||||
public void testPutValues_StringArray()
|
||||
{
|
||||
MultiMap mm = new MultiMap();
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
|
||||
String key = "formats";
|
||||
|
||||
|
@ -119,7 +136,7 @@ public class MultiMapTest
|
|||
@Test
|
||||
public void testPutValues_VarArgs()
|
||||
{
|
||||
MultiMap mm = new MultiMap();
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
|
||||
String key = "formats";
|
||||
|
||||
|
@ -134,7 +151,7 @@ public class MultiMapTest
|
|||
@Test
|
||||
public void testAdd()
|
||||
{
|
||||
MultiMap mm = new MultiMap();
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
|
||||
String key = "formats";
|
||||
|
||||
|
@ -157,7 +174,7 @@ public class MultiMapTest
|
|||
@Test
|
||||
public void testAddValues_List()
|
||||
{
|
||||
MultiMap mm = new MultiMap();
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
|
||||
String key = "formats";
|
||||
|
||||
|
@ -183,7 +200,7 @@ public class MultiMapTest
|
|||
@Test
|
||||
public void testAddValues_List_Empty()
|
||||
{
|
||||
MultiMap mm = new MultiMap();
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
|
||||
String key = "formats";
|
||||
|
||||
|
@ -206,7 +223,7 @@ public class MultiMapTest
|
|||
@Test
|
||||
public void testAddValues_StringArray()
|
||||
{
|
||||
MultiMap mm = new MultiMap();
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
|
||||
String key = "formats";
|
||||
|
||||
|
@ -229,7 +246,7 @@ public class MultiMapTest
|
|||
@Test
|
||||
public void testAddValues_StringArray_Empty()
|
||||
{
|
||||
MultiMap mm = new MultiMap();
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
|
||||
String key = "formats";
|
||||
|
||||
|
@ -252,7 +269,7 @@ public class MultiMapTest
|
|||
@Test
|
||||
public void testRemoveValue()
|
||||
{
|
||||
MultiMap mm = new MultiMap();
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
|
||||
String key = "formats";
|
||||
|
||||
|
@ -274,7 +291,7 @@ public class MultiMapTest
|
|||
@Test
|
||||
public void testRemoveValue_InvalidItem()
|
||||
{
|
||||
MultiMap mm = new MultiMap();
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
|
||||
String key = "formats";
|
||||
|
||||
|
@ -295,7 +312,7 @@ public class MultiMapTest
|
|||
@Test
|
||||
public void testRemoveValue_AllItems()
|
||||
{
|
||||
MultiMap mm = new MultiMap();
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
|
||||
String key = "formats";
|
||||
|
||||
|
@ -325,7 +342,7 @@ public class MultiMapTest
|
|||
@Test
|
||||
public void testRemoveValue_FromEmpty()
|
||||
{
|
||||
MultiMap mm = new MultiMap();
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
|
||||
String key = "formats";
|
||||
|
||||
|
@ -346,7 +363,7 @@ public class MultiMapTest
|
|||
@Test
|
||||
public void testPutAll_Map()
|
||||
{
|
||||
MultiMap mm = new MultiMap();
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
|
||||
assertMapSize(mm,0); // Shouldn't have anything yet.
|
||||
|
||||
|
@ -355,7 +372,7 @@ public class MultiMapTest
|
|||
input.put("color","red");
|
||||
input.put("amount","bushel");
|
||||
|
||||
mm.putAll(input);
|
||||
mm.putAllValues(input);
|
||||
|
||||
assertMapSize(mm,3);
|
||||
assertValues(mm,"food","apple");
|
||||
|
@ -369,11 +386,11 @@ public class MultiMapTest
|
|||
@Test
|
||||
public void testPutAll_MultiMap_Simple()
|
||||
{
|
||||
MultiMap mm = new MultiMap();
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
|
||||
assertMapSize(mm,0); // Shouldn't have anything yet.
|
||||
|
||||
MultiMap input = new MultiMap();
|
||||
MultiMap<String> input = new MultiMap<>();
|
||||
input.put("food","apple");
|
||||
input.put("color","red");
|
||||
input.put("amount","bushel");
|
||||
|
@ -392,11 +409,11 @@ public class MultiMapTest
|
|||
@Test
|
||||
public void testPutAll_MultiMapComplex()
|
||||
{
|
||||
MultiMap mm = new MultiMap();
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
|
||||
assertMapSize(mm,0); // Shouldn't have anything yet.
|
||||
|
||||
MultiMap input = new MultiMap();
|
||||
MultiMap<String> input = new MultiMap<>();
|
||||
input.putValues("food","apple","cherry","raspberry");
|
||||
input.put("color","red");
|
||||
input.putValues("amount","bushel","pint");
|
||||
|
@ -415,7 +432,7 @@ public class MultiMapTest
|
|||
@Test
|
||||
public void testToStringArrayMap()
|
||||
{
|
||||
MultiMap mm = new MultiMap();
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
mm.putValues("food","apple","cherry","raspberry");
|
||||
mm.put("color","red");
|
||||
mm.putValues("amount","bushel","pint");
|
||||
|
@ -436,7 +453,7 @@ public class MultiMapTest
|
|||
@Test
|
||||
public void testToString()
|
||||
{
|
||||
MultiMap mm = new MultiMap();
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
mm.put("color","red");
|
||||
|
||||
Assert.assertEquals("{color=red}", mm.toString());
|
||||
|
@ -452,7 +469,7 @@ public class MultiMapTest
|
|||
@Test
|
||||
public void testClear()
|
||||
{
|
||||
MultiMap mm = new MultiMap();
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
mm.putValues("food","apple","cherry","raspberry");
|
||||
mm.put("color","red");
|
||||
mm.putValues("amount","bushel","pint");
|
||||
|
@ -470,7 +487,7 @@ public class MultiMapTest
|
|||
@Test
|
||||
public void testContainsKey()
|
||||
{
|
||||
MultiMap mm = new MultiMap();
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
mm.putValues("food","apple","cherry","raspberry");
|
||||
mm.put("color","red");
|
||||
mm.putValues("amount","bushel","pint");
|
||||
|
@ -479,18 +496,37 @@ public class MultiMapTest
|
|||
Assert.assertFalse("Contains Key [nutrition]", mm.containsKey("nutrition"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link MultiMap#containsSimpleValue(Object)}
|
||||
*/
|
||||
@Test
|
||||
public void testContainsSimpleValue()
|
||||
{
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
mm.putValues("food","apple","cherry","raspberry");
|
||||
mm.put("color","red");
|
||||
mm.putValues("amount","bushel","pint");
|
||||
|
||||
Assert.assertTrue("Contains Value [red]", mm.containsSimpleValue("red"));
|
||||
Assert.assertFalse("Contains Value [nutrition]", mm.containsValue("nutrition"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link MultiMap#containsValue(Object)}
|
||||
*/
|
||||
@Test
|
||||
public void testContainsValue()
|
||||
{
|
||||
MultiMap mm = new MultiMap();
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
mm.putValues("food","apple","cherry","raspberry");
|
||||
mm.put("color","red");
|
||||
mm.putValues("amount","bushel","pint");
|
||||
|
||||
Assert.assertTrue("Contains Value [red]", mm.containsValue("red"));
|
||||
List<String> acr = new ArrayList<>();
|
||||
acr.add("apple");
|
||||
acr.add("cherry");
|
||||
acr.add("raspberry");
|
||||
Assert.assertTrue("Contains Value [apple,cherry,raspberry]", mm.containsValue(acr));
|
||||
Assert.assertFalse("Contains Value [nutrition]", mm.containsValue("nutrition"));
|
||||
}
|
||||
|
||||
|
@ -500,7 +536,7 @@ public class MultiMapTest
|
|||
@Test
|
||||
public void testContainsValue_LazyList()
|
||||
{
|
||||
MultiMap mm = new MultiMap();
|
||||
MultiMap<String> mm = new MultiMap<>();
|
||||
mm.putValues("food","apple","cherry","raspberry");
|
||||
mm.put("color","red");
|
||||
mm.putValues("amount","bushel","pint");
|
||||
|
@ -521,7 +557,7 @@ public class MultiMapTest
|
|||
}
|
||||
}
|
||||
|
||||
private void assertValues(MultiMap mm, String key, Object... expectedValues)
|
||||
private void assertValues(MultiMap<String> mm, String key, Object... expectedValues)
|
||||
{
|
||||
List<String> values = mm.getValues(key);
|
||||
|
||||
|
@ -539,7 +575,7 @@ public class MultiMapTest
|
|||
}
|
||||
}
|
||||
|
||||
private void assertNullValues(MultiMap mm, String key)
|
||||
private void assertNullValues(MultiMap<String> mm, String key)
|
||||
{
|
||||
List<String> values = mm.getValues(key);
|
||||
|
||||
|
@ -548,7 +584,7 @@ public class MultiMapTest
|
|||
Assert.assertThat(prefix + ".size",values,nullValue());
|
||||
}
|
||||
|
||||
private void assertEmptyValues(MultiMap mm, String key)
|
||||
private void assertEmptyValues(MultiMap<String> mm, String key)
|
||||
{
|
||||
List<String> values = mm.getValues(key);
|
||||
|
||||
|
@ -557,7 +593,7 @@ public class MultiMapTest
|
|||
Assert.assertEquals(prefix + ".size",0,LazyList.size(values));
|
||||
}
|
||||
|
||||
private void assertMapSize(MultiMap mm, int expectedSize)
|
||||
private void assertMapSize(MultiMap<String> mm, int expectedSize)
|
||||
{
|
||||
Assert.assertEquals("MultiMap.size",expectedSize,mm.size());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue