[jboss-cvs] jboss-portal/server/src/main/org/jboss/portal/server/util ...
Julien Viet
julien at jboss.com
Sun Jul 30 20:05:40 EDT 2006
User: julien
Date: 06/07/30 20:05:40
Modified: server/src/main/org/jboss/portal/server/util
Parameters.java
Log:
- Make Parameters extend ParameterMap
Revision Changes Path
1.13 +76 -177 jboss-portal/server/src/main/org/jboss/portal/server/util/Parameters.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: Parameters.java
===================================================================
RCS file: /cvsroot/jboss/jboss-portal/server/src/main/org/jboss/portal/server/util/Parameters.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -b -r1.12 -r1.13
--- Parameters.java 30 Jul 2006 22:35:17 -0000 1.12
+++ Parameters.java 31 Jul 2006 00:05:40 -0000 1.13
@@ -21,11 +21,9 @@
*/
package org.jboss.portal.server.util;
-import org.jboss.portal.common.util.Tools;
+import org.jboss.portal.common.util.ParameterMap;
import java.io.Serializable;
-import java.util.Collections;
-import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@@ -36,9 +34,9 @@
* A set of parameters.
*
* @author <a href="mailto:julien at jboss.org">Julien Viet</a>
- * @version $Revision: 1.12 $
+ * @version $Revision: 1.13 $
*/
-public class Parameters implements Serializable
+public class Parameters extends ParameterMap implements Serializable
{
/** The serialVersionUID */
@@ -50,6 +48,7 @@
/** Creates an empty parameter set. */
public Parameters()
{
+ this.map = new HashMap();
}
/**
@@ -61,13 +60,9 @@
{
if (parameters == null)
{
- throw new IllegalArgumentException("parameters must not be null");
- }
- Map originalMap = parameters.getMap();
- if (originalMap != null && originalMap.size() > 0)
- {
- map = new HashMap(originalMap); // The danger here is that one String[] is modified, but will someone really do that ?
+ throw new IllegalArgumentException();
}
+ this.map = new HashMap(parameters.map);
}
/**
@@ -77,11 +72,13 @@
*/
public Parameters(Map parameterMap)
{
- validateMap(parameterMap);
- if (parameterMap.size() > 0)
- {
- this.map = new HashMap(parameterMap);
+ this.map = new HashMap();
+ replace(parameterMap);
}
+
+ protected Map getDelegate()
+ {
+ return map;
}
/**
@@ -97,29 +94,11 @@
{
throw new IllegalArgumentException("name cannot be null");
}
- if (map == null)
- {
- return null;
- }
String[] value = (String[])map.get(name);
return value == null ? null : value[0];
}
/**
- * Return all the parameter names.
- *
- * @return the parameter names
- */
- public Enumeration getNames()
- {
- if (map == null)
- {
- return Tools.EMPTY_ENUMERATION;
- }
- return Collections.enumeration(map.keySet());
- }
-
- /**
* Return the parameter values or null if it does not exist.
*
* @param name the value to get
@@ -132,35 +111,9 @@
{
throw new IllegalArgumentException("name cannot be null");
}
- if (map == null)
- {
- return null;
- }
return (String[])map.get(name);
}
- /** Return the a map containing the values. */
- public Map getMap()
- {
- if (map == null)
- {
- return Collections.EMPTY_MAP;
- }
- else
- {
- return map;
- }
- }
-
- /** Clear all the parameters. */
- public void clear()
- {
- if (map != null)
- {
- map.clear();
- }
- }
-
/**
* Set the a parameter value.
*
@@ -181,10 +134,6 @@
}
else
{
- if (map == null)
- {
- map = new HashMap();
- }
map.put(name, new String[]{value});
}
}
@@ -202,13 +151,7 @@
{
throw new IllegalArgumentException("name must not be null");
}
- validateStringArray(values);
- // julien : we don't copy the array, the client is not supposed to modify the array
- // the test before are just defensive against bad arguments
- if (map == null)
- {
- map = new HashMap();
- }
+ assertValue(values);
map.put(name, values);
}
@@ -243,16 +186,25 @@
{
throw new IllegalArgumentException("Parameters to merge must be null");
}
+ internalAppend(params.map);
+ }
- Map other = params.getMap();
-
- if (map == null)
+ /**
+ * @param params the parameters to appends
+ * @throws IllegalArgumentException if the params argument is not valid
+ */
+ public void append(Map params) throws IllegalArgumentException
{
- map = new HashMap(other);
+ assertMap(params);
+ internalAppend(params);
}
- else
+
+ /**
+ * Append actual implementation.
+ */
+ private void internalAppend(Map params)
{
- for (Iterator entries = other.entrySet().iterator();entries.hasNext();)
+ for (Iterator entries = params.entrySet().iterator();entries.hasNext();)
{
Map.Entry entry = (Map.Entry)entries.next();
String name = (String)entry.getKey();
@@ -276,56 +228,45 @@
map.put(name, values);
}
}
- }
/**
- * Replace all the parameters.
+ * Replace all the exisint parameters with the new ones.
*
* @throws IllegalArgumentException if the map is not valid
*/
- public void replace(Map map)
- {
- validateMap(map);
- if (this.map != null)
- {
- this.map.clear();
- if (map.isEmpty() == false)
- {
- this.map.putAll(map);
- }
- }
- else
- {
- if (map.isEmpty() == false)
+ public void replace(Map params)
{
- this.map = new HashMap(map);
- }
- }
+ assertMap(params);
+ internalReplace(params);
}
/**
- * Replace all the parameters.
+ * Replace all the parameters with the new ones.
*
* @throws IllegalArgumentException if the parameters is not valid
*/
- public void replace(Parameters parameters)
+ public void replace(Parameters params)
{
- if (parameters == null)
+ if (params == null)
{
throw new IllegalArgumentException("parameters must not be null");
}
- Map map = parameters.getMap();
- if (map == null)
- {
- map = Collections.EMPTY_MAP;
- }
- replace(map);
+ internalReplace(params.map);
}
- /** Return the size. */
- public int getSize()
+ /**
+ * Replace actual implementation.
+ */
+ private void internalReplace(Map params)
{
- return map == null ? 0 : map.size();
+ map.clear();
+ for (Iterator i = params.entrySet().iterator(); i.hasNext();)
+ {
+ Map.Entry entry = (Entry)i.next();
+ String name = (String)entry.getKey();
+ String values[] = (String[])entry.getValue();
+ map.put(name, values.clone());
+ }
}
/**
@@ -396,59 +337,17 @@
return buffer.toString();
}
- /**
- * Validate the parameter map. The map is considered valid if <ul> <li>it is not null</li> <li>the key set contains
- * only strings</li> <li>the value set contains valid string arrays</li> </ul>
- *
- * @param map the map to validate
- * @throws IllegalArgumentException if the map is not valid
- */
- protected void validateMap(Map map) throws IllegalArgumentException
- {
- if (map == null)
- {
- throw new IllegalArgumentException("parameter map must not be null");
- }
- for (Iterator i = map.entrySet().iterator(); i.hasNext();)
- {
- Map.Entry entry = (Map.Entry)i.next();
- Object key = entry.getKey();
- if (!(key instanceof String))
- {
- throw new IllegalArgumentException("parameter map keys must all be string");
- }
- Object value = entry.getValue();
- if (!(value instanceof String[]))
- {
- throw new IllegalArgumentException("parameter map values must all be string arrays");
- }
- validateStringArray((String[])value);
- }
- }
-
- /**
- * Validate the string array. The array is considered valid if <ul> <li>it is not null</li> <li>the size is greater
- * than zero</li> <li>all the parameters are not null</li> </ul>
- *
- * @param values the string array to validate
- * @throws IllegalArgumentException if the array is not valid
- */
- protected void validateStringArray(String[] values) throws IllegalArgumentException
- {
- if (values == null)
+ protected void assertMap(Map that) throws IllegalArgumentException
{
- throw new IllegalArgumentException("Array must not be null");
- }
- if (values.length == 0)
+ if (that == null)
{
- throw new IllegalArgumentException("Array must not be zero length");
+ throw new IllegalArgumentException("Map cannot be null");
}
- for (int i = values.length - 1; i >= 0; i--)
+ for (Iterator i = that.entrySet().iterator(); i.hasNext();)
{
- if (values[i] == null)
- {
- throw new IllegalArgumentException("Array must not contain null values");
- }
+ Entry entry = (Entry)i.next();
+ assertKey(entry.getKey());
+ assertValue(entry.getValue());
}
}
}
More information about the jboss-cvs-commits
mailing list