Author: julien(a)jboss.com
Date: 2008-01-24 09:25:15 -0500 (Thu, 24 Jan 2008)
New Revision: 9596
Added:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/MultiValuedPropertyMap.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/MultiValuedPropertyMapTestCase.java
Log:
added MultiValuedPropertyMap class (migration from Portlet module) + its test case
Added:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/MultiValuedPropertyMap.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/MultiValuedPropertyMap.java
(rev 0)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/MultiValuedPropertyMap.java 2008-01-24
14:25:15 UTC (rev 9596)
@@ -0,0 +1,221 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.common.util;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Set;
+import java.util.Collections;
+import java.util.LinkedList;
+
+/**
+ * A map of multi valued properties
+ *
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+public class MultiValuedPropertyMap<T>
+{
+
+ /** . */
+ private Map<String, LinkedList<T>> content;
+
+ /**
+ * @param name the property name
+ * @param value the property value
+ * @throws IllegalArgumentException if name or value is null
+ */
+ public void addProperty(String name, T value)
+ {
+ if (name == null)
+ {
+ throw new IllegalArgumentException("Name cannot be null");
+ }
+ if (value == null)
+ {
+ throw new IllegalArgumentException("Value cannot be null");
+ }
+ if (content == null)
+ {
+ content = new HashMap<String, LinkedList<T>>();
+ }
+
+ //
+ LinkedList<T> values = content.get(name);
+ if (values == null)
+ {
+ values = new LinkedList<T>();
+ content.put(name, values);
+ }
+
+ //
+ values.add(value);
+ }
+
+ /**
+ * @param name the property name
+ * @param value the property value
+ * @throws IllegalArgumentException if name or value is null
+ */
+ public void setProperty(String name, T value)
+ {
+ if (name == null)
+ {
+ throw new IllegalArgumentException("Name cannot be null");
+ }
+ if (value == null)
+ {
+ throw new IllegalArgumentException("Value cannot be null");
+ }
+ if (content == null)
+ {
+ content = new HashMap<String, LinkedList<T>>();
+ }
+
+ //
+ LinkedList<T> values = content.get(name);
+ if (values == null)
+ {
+ values = new LinkedList<T>();
+ content.put(name, values);
+ }
+ else
+ {
+ values.clear();
+ }
+
+ //
+ values.add(value);
+ }
+
+ /**
+ * Clear the properties.
+ */
+ public void clear()
+ {
+ if (content != null)
+ {
+ content.clear();
+ }
+ }
+
+ /**
+ * Returns the first property value or null if it cannot be found.
+ *
+ * @param name the property name
+ * @return the property value
+ * @throws IllegalArgumentException if the name argument is null
+ */
+ public T getPropertyValue(String name) throws IllegalArgumentException
+ {
+ if (name == null)
+ {
+ throw new IllegalArgumentException("Name cannot be null");
+ }
+ if (content == null)
+ {
+ return null;
+ }
+
+ //
+ LinkedList<T> values = content.get(name);
+ if (values == null)
+ {
+ return null;
+ }
+ else
+ {
+ return values.get(0);
+ }
+ }
+
+ /**
+ * Returns the list of values for a specified property.
+ *
+ * @param name the property name
+ * @return the list of properties for the specified name or null if it does not exist
+ * @throws IllegalArgumentException if name argument is null
+ */
+ public List<T> getPropertyValues(String name)
+ {
+ if (name == null)
+ {
+ throw new IllegalArgumentException("Name cannot be null");
+ }
+ if (content == null)
+ {
+ return null;
+ }
+ return content.get(name);
+ }
+
+ /**
+ * Returns the set of property names.
+ *
+ * @return the set of property names
+ */
+ public Set<String> getPropertyNames()
+ {
+ if (content == null)
+ {
+ return Collections.emptySet();
+ }
+ return content.keySet();
+ }
+
+ /**
+ * Append the multi valued property map to this one. Entries from the provided map
that do not exist
+ * in the current map are created, entries that exist in both maps are concatenated in
the existing map
+ * with the values of the existing map being before the values of the provided map.
+ *
+ * @param appended the property map to append
+ * @throws IllegalArgumentException if the provided map is null
+ */
+ public void append(MultiValuedPropertyMap<T> appended) throws
IllegalArgumentException
+ {
+ if (appended == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ for (Map.Entry<String, LinkedList<T>> entry :
appended.content.entrySet())
+ {
+ String name = entry.getKey();
+
+ //
+ LinkedList<T> values = content.get(name);
+
+ //
+ if (values != null)
+ {
+ values.addAll(entry.getValue());
+ }
+ else
+ {
+ content.put(name, new LinkedList<T>(entry.getValue()));
+ }
+ }
+ }
+}
Added:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/MultiValuedPropertyMapTestCase.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/MultiValuedPropertyMapTestCase.java
(rev 0)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/MultiValuedPropertyMapTestCase.java 2008-01-24
14:25:15 UTC (rev 9596)
@@ -0,0 +1,155 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.test.common.util;
+
+import junit.framework.TestCase;
+import org.jboss.portal.common.util.MultiValuedPropertyMap;
+import org.jboss.portal.common.util.Tools;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+public class MultiValuedPropertyMapTestCase extends TestCase
+{
+
+ public MultiValuedPropertyMapTestCase()
+ {
+ }
+
+ public MultiValuedPropertyMapTestCase(String name)
+ {
+ super(name);
+ }
+
+ public void testA()
+ {
+ MultiValuedPropertyMap<String> props = new
MultiValuedPropertyMap<String>();
+
+ props.setProperty("name1", "value1");
+
+ props.addProperty("name2", "value2");
+
+ props.setProperty("name3", "value3-1");
+ props.addProperty("name3", "value3-2");
+
+ props.addProperty("name4", "value4-1");
+ props.addProperty("name4", "value4-2");
+
+ props.setProperty("name5", "value5-1");
+ props.setProperty("name5", "value5-2");
+
+ props.addProperty("name6", "value6-1");
+ props.setProperty("name6", "value6-2");
+
+ assertEquals("value1", props.getPropertyValue("name1"));
+ assertEquals(1, props.getPropertyValues("name1").size());
+ assertEquals("value1",
props.getPropertyValues("name1").get(0));
+
+ assertEquals("value2", props.getPropertyValue("name2"));
+ assertEquals(1, props.getPropertyValues("name2").size());
+ assertEquals("value2",
props.getPropertyValues("name2").get(0));
+
+ assertEquals("value3-1", props.getPropertyValue("name3"));
+ assertEquals(2, props.getPropertyValues("name3").size());
+ assertEquals("value3-1",
props.getPropertyValues("name3").get(0));
+ assertEquals("value3-2",
props.getPropertyValues("name3").get(1));
+
+ assertEquals("value4-1", props.getPropertyValue("name4"));
+ assertEquals(2, props.getPropertyValues("name4").size());
+ assertEquals("value4-1",
props.getPropertyValues("name4").get(0));
+ assertEquals("value4-2",
props.getPropertyValues("name4").get(1));
+
+ assertEquals(1, props.getPropertyValues("name5").size());
+ assertEquals("value5-2",
props.getPropertyValues("name5").get(0));
+
+ assertEquals("value6-2", props.getPropertyValue("name6"));
+ assertEquals(1, props.getPropertyValues("name6").size());
+ assertEquals("value6-2",
props.getPropertyValues("name6").get(0));
+
+ assertEquals(null, props.getPropertyValues("not here"));
+ assertNull(props.getPropertyValue("not here"));
+ assertEquals(Tools.toSet("name1", "name2", "name3",
"name4", "name5", "name6"), props.getPropertyNames());
+
+ try
+ {
+ props.setProperty(null, "not null");
+ fail("Expected IllegalArgumentException");
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+
+ try
+ {
+ props.setProperty("not null", null);
+ fail("Expected IllegalArgumentException");
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+
+ try
+ {
+ props.setProperty(null, null);
+ fail("Expected IllegalArgumentException");
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ }
+
+ public void testAppend()
+ {
+ MultiValuedPropertyMap<String> props = new
MultiValuedPropertyMap<String>();
+ props.setProperty("foo", "foo1");
+ props.setProperty("bar", "bar1");
+ props.addProperty("bar", "bar2");
+ props.setProperty("juu", "juu1");
+ props.setProperty("daa", "daa1");
+ props.addProperty("daa", "daa2");
+
+ //
+ MultiValuedPropertyMap<String> appended = new
MultiValuedPropertyMap<String>();
+ appended.setProperty("juu", "juu2");
+ appended.addProperty("juu", "juu3");
+ appended.setProperty("daa", "daa3");
+ appended.setProperty("zoo", "zoo1");
+ appended.setProperty("tee", "tee1");
+ appended.addProperty("tee", "tee2");
+
+
+ //
+ props.append(appended);
+
+ //
+
assertEquals(Tools.toSet("foo","bar","juu","daa","zoo","tee"),
props.getPropertyNames());
+ assertEquals(Tools.toList("foo1"),
props.getPropertyValues("foo"));
+ assertEquals(Tools.toList("bar1", "bar2"),
props.getPropertyValues("bar"));
+ assertEquals(Tools.toList("juu1", "juu2","juu3"),
props.getPropertyValues("juu"));
+ assertEquals(Tools.toList("daa1", "daa2","daa3"),
props.getPropertyValues("daa"));
+ assertEquals(Tools.toList("zoo1"),
props.getPropertyValues("zoo"));
+ assertEquals(Tools.toList("tee1","tee2"),
props.getPropertyValues("tee"));
+ }
+
+}