Author: chris.laprun(a)jboss.com
Date: 2008-02-19 19:40:46 -0500 (Tue, 19 Feb 2008)
New Revision: 10037
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
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/CollectionMapTestCase.java
Log:
- JBPORTAL-1833: SetMap now properly uses comparator, added test case.
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 2008-02-20
00:03:59 UTC (rev 10036)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/CollectionMap.java 2008-02-20
00:40:46 UTC (rev 10037)
@@ -22,13 +22,14 @@
******************************************************************************/
package org.jboss.portal.common.util;
-import java.util.Map;
+import java.io.Serializable;
import java.util.Collection;
-import java.util.Iterator;
import java.util.Collections;
+import java.util.Comparator;
import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
import java.util.Set;
-import java.io.Serializable;
/**
* A map of collections.
@@ -41,13 +42,15 @@
/** The underlying map. */
private final Map<K, Collection<V>> map;
+ /** An optional comparator. */
+ protected Comparator<V> comparator;
public CollectionMap()
{
map = init(null);
}
- public CollectionMap(SetMap<K, V> other) throws IllegalArgumentException
+ public CollectionMap(CollectionMap<K, V> other) throws IllegalArgumentException
{
if (other == null)
{
@@ -56,6 +59,31 @@
map = init(other);
}
+ public CollectionMap(CollectionMap<K, V> other, Comparator<V> comparator)
throws IllegalArgumentException
+ {
+ this(other);
+ initComparator(comparator);
+ }
+
+ public CollectionMap(Comparator<V> comparator)
+ {
+ this();
+
+ initComparator(comparator);
+ }
+
+ private void initComparator(Comparator<V> comparator)
+ {
+ //
+ if (comparator == null)
+ {
+ throw new IllegalArgumentException("No null comparator allowed");
+ }
+
+ //
+ this.comparator = comparator;
+ }
+
/**
* Add an object in the set keyed under the specified key.
*
@@ -78,9 +106,7 @@
add(collection, o);
}
- /**
- * Return the set of keys.
- */
+ /** Return the set of keys. */
public final Set<K> keySet()
{
return map.keySet();
@@ -156,9 +182,7 @@
}
}
- /**
- * Return the collection specified by the key.
- */
+ /** Return the collection specified by the key. */
public Collection<V> get(K key)
{
return map.get(key);
@@ -194,10 +218,12 @@
{
return iterator.hasNext();
}
+
public V next()
{
return iterator.next();
}
+
public void remove()
{
iterator.remove();
@@ -232,9 +258,8 @@
protected abstract void add(Collection<V> c, V 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.
+ * 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
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 2008-02-20
00:03:59 UTC (rev 10036)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/ListMap.java 2008-02-20
00:40:46 UTC (rev 10037)
@@ -22,12 +22,11 @@
******************************************************************************/
package org.jboss.portal.common.util;
-import java.util.Collection;
-import java.util.Comparator;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Collections;
+import java.util.Comparator;
import java.util.List;
-import java.util.Set;
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
@@ -36,9 +35,6 @@
public class ListMap<K, V> extends CollectionMap<K, V>
{
- /** An optional comparator. */
- protected Comparator<V> comparator;
-
public ListMap()
{
}
@@ -50,35 +46,15 @@
public ListMap(SetMap<K, V> other, Comparator<V> comparator) throws
IllegalArgumentException
{
- super(other);
-
- //
- if (comparator == null)
- {
- throw new IllegalArgumentException("No null comparator allowed");
- }
-
- //
- this.comparator = comparator;
+ super(other, comparator);
}
public ListMap(Comparator<V> comparator)
{
- super();
-
- //
- if (comparator == null)
- {
- throw new IllegalArgumentException("No null comparator allowed");
- }
-
- //
- this.comparator = comparator;
+ super(comparator);
}
- /**
- * Return the list specified by the key.
- */
+ /** Return the list specified by the key. */
public List<V> get(K key)
{
return (List<V>)super.get(key);
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 2008-02-20
00:03:59 UTC (rev 10036)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/SetMap.java 2008-02-20
00:40:46 UTC (rev 10037)
@@ -22,17 +22,15 @@
******************************************************************************/
package org.jboss.portal.common.util;
+import java.util.Collection;
+import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
-import java.util.Comparator;
-import java.util.TreeSet;
import java.util.SortedSet;
-import java.util.Collection;
-import java.util.Map;
+import java.util.TreeSet;
/**
- * A map of set. This object does not handle synchronization and use HashMap and HashSet
- * as underlying data structures;
+ * A map of set. This object does not handle synchronization and use HashMap and HashSet
as underlying data structures;
*
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 7322 $
@@ -43,9 +41,6 @@
/** Version. */
static final long serialVersionUID = -7239767000556095977L;
- /** An optional comparator. */
- protected Comparator<V> comparator;
-
public SetMap()
{
}
@@ -83,9 +78,7 @@
this.comparator = comparator;
}
- /**
- * Return the set specified by the key.
- */
+ /** Return the set specified by the key. */
public Set<V> get(K key)
{
return (Set<V>)super.get(key);
Modified:
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 2008-02-20
00:03:59 UTC (rev 10036)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/CollectionMapTestCase.java 2008-02-20
00:40:46 UTC (rev 10037)
@@ -28,6 +28,7 @@
import org.jboss.portal.common.util.SetMap;
import java.util.ArrayList;
+import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
@@ -80,6 +81,54 @@
testThrowNPE(new ListMap<Key, Value>());
}
+ public void testSorting()
+ {
+ Comparator<Value> comp = new Comparator<Value>()
+ {
+ public int compare(Value o1, Value o2)
+ {
+ return o1.i - o2.i;
+ }
+ };
+
+ testComparatorSorting(new ListMap<Key, Value>(comp));
+ testComparatorSorting(new SetMap<Key, Value>(comp));
+ }
+
+ private void testComparatorSorting(CollectionMap<Key, Value> map)
+ {
+ Value v1 = new Value(1);
+ Value v2 = new Value(2);
+ Value v3 = new Value(3);
+ Value v4 = new Value(4);
+
+ map.put(k1, v2);
+ map.put(k1, v4);
+ map.put(k1, v3);
+ map.put(k1, v1);
+
+ Iterator<Value> iterator = map.iterator(k1);
+ for (int i = 0; iterator.hasNext(); i++)
+ {
+ Value value = iterator.next();
+ switch (i)
+ {
+ case 0:
+ assertEquals(v1, value);
+ break;
+ case 1:
+ assertEquals(v2, value);
+ break;
+ case 2:
+ assertEquals(v3, value);
+ break;
+ case 3:
+ assertEquals(v4, value);
+ break;
+ }
+ }
+ }
+
private void testNormal(CollectionMap<Key, Value> map)
{
assertFalse(map.contains(k1, v1));
@@ -212,6 +261,22 @@
private static class Value
{
+ int i;
+
+ private Value()
+ {
+ }
+
+ private Value(int i)
+ {
+ this.i = i;
+ }
+
+ @Override
+ public String toString()
+ {
+ return "Value " + i;
+ }
}
private static class ValueExt extends Value