Author: julien(a)jboss.com
Date: 2008-01-15 17:57:36 -0500 (Tue, 15 Jan 2008)
New Revision: 9518
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/ParameterMap.java
Log:
moving methods from PortletParameters to ParameterMap as they are generic enough.
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/ParameterMap.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/ParameterMap.java 2008-01-15
22:45:05 UTC (rev 9517)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/ParameterMap.java 2008-01-15
22:57:36 UTC (rev 9518)
@@ -25,6 +25,8 @@
import java.util.Arrays;
import java.util.Map;
import java.util.HashMap;
+import java.util.Iterator;
+import java.io.Serializable;
/**
* A decorator that enforce the map content to be <String,String[]>
@@ -32,7 +34,7 @@
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 6671 $
*/
-public class ParameterMap extends TypedMap<String, String[], String, String[]>
+public class ParameterMap extends TypedMap<String, String[], String, String[]>
implements Serializable
{
/** . */
@@ -76,6 +78,53 @@
}
}
+ /**
+ * Copy the parameter map.
+ *
+ * @param parameterMap the parameter map to copy
+ * @return a parameter map initialized from the argument map
+ * @throws NullPointerException if the map contains a null key or a null value
+ * @throws IllegalArgumentException if the map is null or it contains a value with a
zero length array or a null
+ * element in the array
+ * @throws ClassCastException if the map contains a key that is not a string or
a value that is not a string
+ * array
+ */
+ public static ParameterMap clone(Map<String,String[]> parameterMap) throws
NullPointerException, ClassCastException, IllegalArgumentException
+ {
+ if (parameterMap == null)
+ {
+ throw new IllegalArgumentException("No null map accepted");
+ }
+
+ //
+ ParameterMap pm = new ParameterMap();
+
+ //
+ pm.replace(parameterMap);
+
+ //
+ return pm;
+ }
+
+ /**
+ * Safely wrap the map as a portlet parameters object. If the map is already a
parameter map object, just return
+ * that object otherwise return a wrapper around the map.
+ *
+ * @param map the map
+ * @return the portlet parameters
+ */
+ public static ParameterMap wrap(Map<String, String[]> map)
+ {
+ if (map instanceof ParameterMap)
+ {
+ return (ParameterMap)map;
+ }
+ else
+ {
+ return new ParameterMap(map);
+ }
+ }
+
/** . */
private final boolean cloneInternalValue;
@@ -271,4 +320,63 @@
{
put(name, values);
}
+
+ /**
+ * Append the content of the argument map to that map. If both maps contains an entry
sharing the same key, then the
+ * string arrays or the two entries will be concatenated into a single array. Each
entry present on the argument map
+ * and not in the current map will be kept as is. The argument validation is performed
before the state is updated.
+ *
+ * @param params the parameters to appends
+ * @throws NullPointerException if the map contains a null key or a null value
+ * @throws IllegalArgumentException if the map is null or it contains a value with a
zero length array or a null
+ * element in the array
+ * @throws ClassCastException if the map contains a key that is not a string or
a value that is not a string
+ * array
+ */
+ public void append(Map<String, String[]> params) throws ClassCastException,
NullPointerException, IllegalArgumentException
+ {
+ // Clone
+ params = new HashMap<String, String[]>(params);
+
+ //
+ for (Map.Entry<String, String[]> entry : params.entrySet())
+ {
+ String[] existingValue = get(entry.getKey());
+
+ // Perform the appending operation if the entry exist
+ if (existingValue != null)
+ {
+ String[] appendedValue = entry.getValue();
+ String[] newValue = new String[existingValue.length + appendedValue.length];
+ System.arraycopy(existingValue, 0, newValue, 0, existingValue.length);
+ System.arraycopy(appendedValue, 0, newValue, existingValue.length,
appendedValue.length);
+ entry.setValue(newValue);
+ }
+ }
+
+ //
+ putAll(params);
+ }
+
+ public String toString()
+ {
+ StringBuffer buffer = new StringBuffer("ParameterMap[");
+ for (Iterator i = entrySet().iterator(); i.hasNext();)
+ {
+ Map.Entry entry = (Map.Entry)i.next();
+ String name = (String)entry.getKey();
+ String[] values = (String[])entry.getValue();
+ buffer.append(name);
+ for (int j = 0; j < values.length; j++)
+ {
+ buffer.append(j > 0 ? ',' : '=').append(values[j]);
+ }
+ if (i.hasNext())
+ {
+ buffer.append(" | ");
+ }
+ }
+ buffer.append(']');
+ return buffer.toString();
+ }
}
Show replies by date