Author: julien(a)jboss.com
Date: 2007-11-11 17:20:21 -0500 (Sun, 11 Nov 2007)
New Revision: 8868
Added:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/CollectionMapTestCase.java
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/CollectionMap.java
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/ListMap.java
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/SetMap.java
Log:
generified CollectionMap + added test cases
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/CollectionMap.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/CollectionMap.java 2007-11-11
21:19:01 UTC (rev 8867)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/CollectionMap.java 2007-11-11
22:20:21 UTC (rev 8868)
@@ -36,32 +36,40 @@
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 1.1 $
*/
-public abstract class CollectionMap implements Serializable
+public abstract class CollectionMap<K, V> implements Serializable
{
/** The underlying map. */
- protected Map map;
+ private final Map<K, Collection<V>> map;
public CollectionMap()
{
- init(null);
+ map = init(null);
}
- public CollectionMap(SetMap other) throws IllegalArgumentException
+ public CollectionMap(SetMap<K, V> other) throws IllegalArgumentException
{
if (other == null)
{
throw new IllegalArgumentException("Cannot copy null argument");
}
- init(other);
+ map = init(other);
}
/**
* Add an object in the set keyed under the specified key.
+ *
+ * @throws NullPointerException if the key is null
*/
- public void put(Object key, Object o)
+ public final void put(K key, V o) throws NullPointerException
{
- Collection collection = (Collection)map.get(key);
+ if (key == null)
+ {
+ throw new NullPointerException("No null key");
+ }
+
+ //
+ Collection<V> collection = map.get(key);
if (collection == null)
{
collection = newCollection();
@@ -73,28 +81,48 @@
/**
* Return the set of keys.
*/
- public Set keySet()
+ public final Set<K> keySet()
{
return map.keySet();
}
/**
* Remove the entire set of objects specified by the key.
+ *
+ * @throws NullPointerException if the key is null
*/
- public void remove(Object key)
+ public final void remove(K key) throws NullPointerException
{
+ if (key == null)
+ {
+ throw new NullPointerException("No null key");
+ }
+
+ //
map.remove(key);
}
/**
* Remove an object in the set keyed under the specified key.
+ *
+ * @throws NullPointerException if the key is null
*/
- public void remove(Object key, Object o)
+ public final void remove(K key, Object o) throws NullPointerException
{
- Collection collection = (Collection)map.get(key);
+ if (key == null)
+ {
+ throw new NullPointerException("No null key");
+ }
+
+ //
+ Collection<V> collection = map.get(key);
+
+ //
if (collection != null)
{
remove(collection, o);
+
+ //
if (collection.isEmpty())
{
map.remove(key);
@@ -104,10 +132,20 @@
/**
* Return true if the specified set contains the object o.
+ *
+ * @throws NullPointerException if the key is null
*/
- public boolean contains(Object key, Object o)
+ public final boolean contains(K key, Object o) throws NullPointerException
{
- Collection collection = (Collection)map.get(key);
+ if (key == null)
+ {
+ throw new NullPointerException("No null key");
+ }
+
+ //
+ Collection<V> collection = map.get(key);
+
+ //
if (collection == null)
{
return false;
@@ -119,25 +157,44 @@
}
/**
+ * Return the collection specified by the key.
+ */
+ public Collection<V> get(K key)
+ {
+ return map.get(key);
+ }
+
+ /**
* Return an iterator over the values in the set specified by the key.
+ *
+ * @throws NullPointerException if the key is null
*/
- public Iterator iterator(final Object key)
+ public final Iterator<V> iterator(final K key)
{
- Collection set = (Collection)map.get(key);
+ if (key == null)
+ {
+ throw new NullPointerException("No null key");
+ }
+
+ //
+ Collection<V> set = map.get(key);
+
+ //
if (set == null)
{
- return Collections.EMPTY_SET.iterator();
+ Set<V> tmp = Collections.emptySet();
+ return tmp.iterator();
}
else
{
- final Iterator iterator = set.iterator();
- return new Iterator()
+ final Iterator<V> iterator = set.iterator();
+ return new Iterator<V>()
{
public boolean hasNext()
{
return iterator.hasNext();
}
- public Object next()
+ public V next()
{
return iterator.next();
}
@@ -153,29 +210,38 @@
}
}
- private void init(CollectionMap other)
+ private Map<K, Collection<V>> init(CollectionMap<K, V> other)
{
- //
- this.map = new HashMap();
+ Map<K, Collection<V>> map = new HashMap<K,
Collection<V>>();
//
if (other != null)
{
- for (Iterator i = other.map.entrySet().iterator(); i.hasNext();)
+ for (Map.Entry<K, Collection<V>> entry : other.map.entrySet())
{
- Map.Entry entry = (Map.Entry)i.next();
- Object key = entry.getKey();
- Collection value = (Collection)entry.getValue();
+ K key = entry.getKey();
+ Collection<V> value = entry.getValue();
map.put(key, newCollection(value));
}
}
+
+ //
+ return map;
}
- protected abstract void add(Collection c, Object o);
+ protected abstract void add(Collection<V> c, V o);
- protected abstract void remove(Collection c, Object o);
+ /**
+ * Removes an object from the collection. The type of the object to remove is
intentionnally
+ * <code>Object</code> and not the parameterized type
<code><V></code> because the Collection<V>
+ * interface is designed that way.
+ *
+ * @param c the collection to remove from
+ * @param o the object to remove
+ */
+ protected abstract void remove(Collection<V> c, Object o);
- protected abstract Collection newCollection();
+ protected abstract Collection<V> newCollection();
- protected abstract Collection newCollection(Collection other);
+ protected abstract Collection<V> newCollection(Collection<V> other);
}
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/ListMap.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/ListMap.java 2007-11-11
21:19:01 UTC (rev 8867)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/ListMap.java 2007-11-11
22:20:21 UTC (rev 8868)
@@ -27,27 +27,28 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
+import java.util.Set;
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 1.1 $
*/
-public class ListMap extends CollectionMap
+public class ListMap<K, V> extends CollectionMap<K, V>
{
/** An optional comparator. */
- protected Comparator comparator;
+ protected Comparator<V> comparator;
public ListMap()
{
}
- public ListMap(SetMap other) throws IllegalArgumentException
+ public ListMap(SetMap<K, V> other) throws IllegalArgumentException
{
super(other);
}
- public ListMap(SetMap other, Comparator comparator) throws IllegalArgumentException
+ public ListMap(SetMap<K, V> other, Comparator<V> comparator) throws
IllegalArgumentException
{
super(other);
@@ -61,7 +62,7 @@
this.comparator = comparator;
}
- public ListMap(Comparator comparator)
+ public ListMap(Comparator<V> comparator)
{
super();
@@ -75,35 +76,43 @@
this.comparator = comparator;
}
- protected void add(Collection c, Object o)
+ /**
+ * Return the list specified by the key.
+ */
+ public List<V> get(K key)
{
+ return (List<V>)super.get(key);
+ }
+
+ protected void add(Collection<V> c, V o)
+ {
c.add(o);
//
if (comparator != null)
{
- Collections.sort((List)c, comparator);
+ Collections.sort((List<V>)c, comparator);
}
}
- protected void remove(Collection c, Object o)
+ protected void remove(Collection<V> c, Object o)
{
c.remove(o);
//
if (comparator != null)
{
- Collections.sort((List)c, comparator);
+ Collections.sort((List<V>)c, comparator);
}
}
- protected Collection newCollection()
+ protected Collection<V> newCollection()
{
- return new ArrayList();
+ return new ArrayList<V>();
}
- protected Collection newCollection(Collection other)
+ protected Collection<V> newCollection(Collection<V> other)
{
- return new ArrayList(other);
+ return new ArrayList<V>(other);
}
}
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/SetMap.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/SetMap.java 2007-11-11
21:19:01 UTC (rev 8867)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/SetMap.java 2007-11-11
22:20:21 UTC (rev 8868)
@@ -28,6 +28,7 @@
import java.util.TreeSet;
import java.util.SortedSet;
import java.util.Collection;
+import java.util.Map;
/**
* A map of set. This object does not handle synchronization and use HashMap and HashSet
@@ -36,25 +37,25 @@
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 7322 $
*/
-public class SetMap extends CollectionMap
+public class SetMap<K, V> extends CollectionMap<K, V>
{
/** Version. */
static final long serialVersionUID = -7239767000556095977L;
/** An optional comparator. */
- protected Comparator comparator;
+ protected Comparator<V> comparator;
public SetMap()
{
}
- public SetMap(SetMap other) throws IllegalArgumentException
+ public SetMap(SetMap<K, V> other) throws IllegalArgumentException
{
super(other);
}
- public SetMap(SetMap other, Comparator comparator) throws IllegalArgumentException
+ public SetMap(SetMap<K, V> other, Comparator<V> comparator) throws
IllegalArgumentException
{
super(other);
@@ -68,7 +69,7 @@
this.comparator = comparator;
}
- public SetMap(Comparator comparator)
+ public SetMap(Comparator<V> comparator)
{
super();
@@ -85,42 +86,42 @@
/**
* Return the set specified by the key.
*/
- public Set get(Object key)
+ public Set<V> get(K key)
{
- return (Set)map.get(key);
+ return (Set<V>)super.get(key);
}
- protected void add(Collection c, Object o)
+ protected void add(Collection<V> c, V o)
{
c.add(o);
}
- protected void remove(Collection c, Object o)
+ protected void remove(Collection<V> c, Object o)
{
c.remove(o);
}
- protected Collection newCollection()
+ protected Collection<V> newCollection()
{
if (comparator == null)
{
- return new HashSet();
+ return new HashSet<V>();
}
else
{
- return new TreeSet(comparator);
+ return new TreeSet<V>(comparator);
}
}
- protected Collection newCollection(Collection other)
+ protected Collection<V> newCollection(Collection<V> other)
{
if (comparator == null)
{
- return new HashSet(other);
+ return new HashSet<V>(other);
}
else
{
- SortedSet set = new TreeSet(comparator);
+ SortedSet<V> set = new TreeSet<V>(comparator);
set.addAll(other);
return set;
}
Added:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/CollectionMapTestCase.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/CollectionMapTestCase.java
(rev 0)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/CollectionMapTestCase.java 2007-11-11
22:20:21 UTC (rev 8868)
@@ -0,0 +1,220 @@
+/******************************************************************************
+ * 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.CollectionMap;
+import org.jboss.portal.common.util.ListMap;
+import org.jboss.portal.common.util.SetMap;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class CollectionMapTestCase extends TestCase
+{
+
+ private Key k1 = new Key();
+ private Value v1 = new Value();
+ private Value v2 = new Value();
+ private ValueExt ve1 = new ValueExt();
+ private ValueExt ve2 = new ValueExt();
+
+ public void testNormal()
+ {
+ testNormal(new SetMap<Key, Value>());
+ testNormal(new ListMap<Key, Value>());
+ }
+
+ public void testRemoveAbsent()
+ {
+ testRemoveAbsent(new SetMap<Key, Value>());
+ testRemoveAbsent(new ListMap<Key, Value>());
+ }
+
+ public void testRemoveNull()
+ {
+ testRemoveNull(new SetMap<Key, Value>());
+ testRemoveNull(new ListMap<Key, Value>());
+ }
+
+ public void testWithNullValue()
+ {
+ testWithNullValue(new SetMap<Key, Value>());
+ testWithNullValue(new ListMap<Key, Value>());
+ }
+
+ public void testClassCastException()
+ {
+ testClassCastException(new SetMap<Key, Value>());
+ testClassCastException(new ListMap<Key, Value>());
+ }
+
+ public void testThrowNPE()
+ {
+ testThrowNPE(new SetMap<Key, Value>());
+ testThrowNPE(new ListMap<Key, Value>());
+ }
+
+ private void testNormal(CollectionMap<Key, Value> map)
+ {
+ assertFalse(map.contains(k1, v1));
+ assertFalse(map.contains(k1, ve1));
+ map.put(k1, v1);
+ assertTrue(map.contains(k1, v1));
+ assertFalse(map.contains(k1, ve1));
+ map.put(k1, ve1);
+ assertTrue(map.contains(k1, v1));
+ assertTrue(map.contains(k1, ve1));
+ map.remove(k1, v1);
+ assertFalse(map.contains(k1, v1));
+ assertTrue(map.contains(k1, ve1));
+ map.remove(k1, ve1);
+ assertFalse(map.contains(k1, v1));
+ assertFalse(map.contains(k1, ve1));
+ }
+
+ private void testRemoveAbsent(CollectionMap<Key, Value> map)
+ {
+ map.put(k1, v1);
+ assertNotNull(map.get(k1));
+ assertEquals(1, map.get(k1).size());
+ assertTrue(map.get(k1).contains(v1));
+ map.remove(k1, v2);
+ assertNotNull(map.get(k1));
+ assertEquals(1, map.get(k1).size());
+ assertTrue(map.get(k1).contains(v1));
+ }
+
+ private void testRemoveNull(CollectionMap<Key, Value> map)
+ {
+ map.put(k1, v1);
+ assertNotNull(map.get(k1));
+ assertEquals(1, map.get(k1).size());
+ assertTrue(map.get(k1).contains(v1));
+ map.remove(k1, null);
+ assertNotNull(map.get(k1));
+ assertEquals(1, map.get(k1).size());
+ assertTrue(map.get(k1).contains(v1));
+ }
+
+ private void testWithNullValue(CollectionMap<Key, Value> map)
+ {
+ assertFalse(map.contains(k1, null));
+ map.put(k1, null);
+ assertTrue(map.contains(k1, null));
+ assertNotNull(map.get(k1));
+ assertEquals(1, map.get(k1).size());
+ assertTrue(map.get(k1).contains(null));
+ map.remove(k1, null);
+ assertFalse(map.contains(k1, null));
+ assertEquals(null, map.get(k1));
+ }
+
+ private void testClassCastException(CollectionMap<Key, Value> map)
+ {
+ CollectionMap sm2 = map;
+ sm2.put(k1, new Object());
+ Iterator<Value> i = map.iterator(k1);
+ List<Value> lst = get(i);
+ try
+ {
+ Value v = lst.get(0);
+ fail();
+ }
+ catch (ClassCastException expected)
+ {
+ }
+ }
+
+ private void testThrowNPE(CollectionMap<Key, Value> map)
+ {
+ try
+ {
+ map.put(null, v1);
+ fail();
+ }
+ catch (NullPointerException expected)
+ {
+ }
+ try
+ {
+ map.remove(null);
+ fail();
+ }
+ catch (NullPointerException expected)
+ {
+ }
+ try
+ {
+ map.remove(null, v1);
+ fail();
+ }
+ catch (NullPointerException expected)
+ {
+ }
+ try
+ {
+ map.contains(null, v1);
+ fail();
+ }
+ catch (NullPointerException expected)
+ {
+ }
+ try
+ {
+ map.iterator(null);
+ fail();
+ }
+ catch (NullPointerException expected)
+ {
+ }
+ }
+
+ private <V> List<V> get(Iterator<V> i)
+ {
+ List<V> list = new ArrayList<V>();
+ while (i.hasNext())
+ {
+ V v = i.next();
+ list.add(v);
+ }
+ return list;
+ }
+
+ private static final class Key
+ {
+ }
+
+ private static class Value
+ {
+ }
+
+ private static class ValueExt extends Value
+ {
+ }
+}