From portal-commits at lists.jboss.org Sun Nov 11 17:20:22 2007 Content-Type: multipart/mixed; boundary="===============9198694648006256551==" MIME-Version: 1.0 From: portal-commits at lists.jboss.org To: portal-commits at lists.jboss.org Subject: [portal-commits] JBoss Portal SVN: r8868 - in modules/common/trunk/common/src: test/java/org/jboss/portal/test/common and 1 other directories. Date: Sun, 11 Nov 2007 17:20:21 -0500 Message-ID: --===============9198694648006256551== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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/u= til/ modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/u= til/CollectionMapTestCase.java Modified: modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/C= ollectionMap.java modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/L= istMap.java modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/S= etMap.java Log: generified CollectionMap + added test cases Modified: modules/common/trunk/common/src/main/java/org/jboss/portal/common= /util/CollectionMap.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 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 Julien Viet * @version $Revision: 1.1 $ */ -public abstract class CollectionMap implements Serializable +public abstract class CollectionMap implements Serializable { = /** The underlying map. */ - protected Map map; + private final Map> map; = public CollectionMap() { - init(null); + map =3D init(null); } = - public CollectionMap(SetMap other) throws IllegalArgumentException + public CollectionMap(SetMap other) throws IllegalArgumentException { if (other =3D=3D null) { throw new IllegalArgumentException("Cannot copy null argument"); } - init(other); + map =3D 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 =3D (Collection)map.get(key); + if (key =3D=3D null) + { + throw new NullPointerException("No null key"); + } + + // + Collection collection =3D map.get(key); if (collection =3D=3D null) { collection =3D newCollection(); @@ -73,28 +81,48 @@ /** * Return the set of keys. */ - public Set keySet() + public final Set 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 =3D=3D 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 =3D (Collection)map.get(key); + if (key =3D=3D null) + { + throw new NullPointerException("No null key"); + } + + // + Collection collection =3D map.get(key); + + // if (collection !=3D 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 NullPointerExcept= ion { - Collection collection =3D (Collection)map.get(key); + if (key =3D=3D null) + { + throw new NullPointerException("No null key"); + } + + // + Collection collection =3D map.get(key); + + // if (collection =3D=3D null) { return false; @@ -119,25 +157,44 @@ } = /** + * Return the collection specified by the key. + */ + public Collection 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 iterator(final K key) { - Collection set =3D (Collection)map.get(key); + if (key =3D=3D null) + { + throw new NullPointerException("No null key"); + } + + // + Collection set =3D map.get(key); + + // if (set =3D=3D null) { - return Collections.EMPTY_SET.iterator(); + Set tmp =3D Collections.emptySet(); + return tmp.iterator(); } else { - final Iterator iterator =3D set.iterator(); - return new Iterator() + final Iterator iterator =3D set.iterator(); + return new Iterator() { 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> init(CollectionMap other) { - // - this.map =3D new HashMap(); + Map> map =3D new HashMap>(); = // if (other !=3D null) { - for (Iterator i =3D other.map.entrySet().iterator(); i.hasNext();) + for (Map.Entry> entry : other.map.entrySet()) { - Map.Entry entry =3D (Map.Entry)i.next(); - Object key =3D entry.getKey(); - Collection value =3D (Collection)entry.getValue(); + K key =3D entry.getKey(); + Collection value =3D entry.getValue(); map.put(key, newCollection(value)); } } + + // + return map; } = - protected abstract void add(Collection c, Object o); + protected abstract void add(Collection c, V o); = - protected abstract void remove(Collection c, Object o); + /** + * Removes an object from the collection. The type of the object to rem= ove is intentionnally + * Object and not the parameterized type = because the Collection + * interface is designed that way. + * + * @param c the collection to remove from + * @param o the object to remove + */ + protected abstract void remove(Collection c, Object o); = - protected abstract Collection newCollection(); + protected abstract Collection newCollection(); = - protected abstract Collection newCollection(Collection other); + protected abstract Collection newCollection(Collection other); } Modified: modules/common/trunk/common/src/main/java/org/jboss/portal/common= /util/ListMap.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 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 Julien Viet * @version $Revision: 1.1 $ */ -public class ListMap extends CollectionMap +public class ListMap extends CollectionMap { = /** An optional comparator. */ - protected Comparator comparator; + protected Comparator comparator; = public ListMap() { } = - public ListMap(SetMap other) throws IllegalArgumentException + public ListMap(SetMap other) throws IllegalArgumentException { super(other); } = - public ListMap(SetMap other, Comparator comparator) throws IllegalArgum= entException + public ListMap(SetMap other, Comparator comparator) throws Ill= egalArgumentException { super(other); = @@ -61,7 +62,7 @@ this.comparator =3D comparator; } = - public ListMap(Comparator comparator) + public ListMap(Comparator comparator) { super(); = @@ -75,35 +76,43 @@ this.comparator =3D comparator; } = - protected void add(Collection c, Object o) + /** + * Return the list specified by the key. + */ + public List get(K key) { + return (List)super.get(key); + } + + protected void add(Collection c, V o) + { c.add(o); = // if (comparator !=3D null) { - Collections.sort((List)c, comparator); + Collections.sort((List)c, comparator); } } = - protected void remove(Collection c, Object o) + protected void remove(Collection c, Object o) { c.remove(o); = // if (comparator !=3D null) { - Collections.sort((List)c, comparator); + Collections.sort((List)c, comparator); } } = - protected Collection newCollection() + protected Collection newCollection() { - return new ArrayList(); + return new ArrayList(); } = - protected Collection newCollection(Collection other) + protected Collection newCollection(Collection other) { - return new ArrayList(other); + return new ArrayList(other); } } Modified: modules/common/trunk/common/src/main/java/org/jboss/portal/common= /util/SetMap.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 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 HashM= ap and HashSet @@ -36,25 +37,25 @@ * @author Julien Viet * @version $Revision: 7322 $ */ -public class SetMap extends CollectionMap +public class SetMap extends CollectionMap { = /** Version. */ static final long serialVersionUID =3D -7239767000556095977L; = /** An optional comparator. */ - protected Comparator comparator; + protected Comparator comparator; = public SetMap() { } = - public SetMap(SetMap other) throws IllegalArgumentException + public SetMap(SetMap other) throws IllegalArgumentException { super(other); } = - public SetMap(SetMap other, Comparator comparator) throws IllegalArgume= ntException + public SetMap(SetMap other, Comparator comparator) throws Ille= galArgumentException { super(other); = @@ -68,7 +69,7 @@ this.comparator =3D comparator; } = - public SetMap(Comparator comparator) + public SetMap(Comparator comparator) { super(); = @@ -85,42 +86,42 @@ /** * Return the set specified by the key. */ - public Set get(Object key) + public Set get(K key) { - return (Set)map.get(key); + return (Set)super.get(key); } = - protected void add(Collection c, Object o) + protected void add(Collection c, V o) { c.add(o); } = - protected void remove(Collection c, Object o) + protected void remove(Collection c, Object o) { c.remove(o); } = - protected Collection newCollection() + protected Collection newCollection() { if (comparator =3D=3D null) { - return new HashSet(); + return new HashSet(); } else { - return new TreeSet(comparator); + return new TreeSet(comparator); } } = - protected Collection newCollection(Collection other) + protected Collection newCollection(Collection other) { if (comparator =3D=3D null) { - return new HashSet(other); + return new HashSet(other); } else { - SortedSet set =3D new TreeSet(comparator); + SortedSet set =3D new TreeSet(comparator); set.addAll(other); return set; } Added: modules/common/trunk/common/src/test/java/org/jboss/portal/test/comm= on/util/CollectionMapTestCase.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 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 Julien Viet + * @version $Revision: 1.1 $ + */ +public class CollectionMapTestCase extends TestCase +{ + + private Key k1 =3D new Key(); + private Value v1 =3D new Value(); + private Value v2 =3D new Value(); + private ValueExt ve1 =3D new ValueExt(); + private ValueExt ve2 =3D new ValueExt(); + + public void testNormal() + { + testNormal(new SetMap()); + testNormal(new ListMap()); + } + + public void testRemoveAbsent() + { + testRemoveAbsent(new SetMap()); + testRemoveAbsent(new ListMap()); + } + + public void testRemoveNull() + { + testRemoveNull(new SetMap()); + testRemoveNull(new ListMap()); + } + + public void testWithNullValue() + { + testWithNullValue(new SetMap()); + testWithNullValue(new ListMap()); + } + + public void testClassCastException() + { + testClassCastException(new SetMap()); + testClassCastException(new ListMap()); + } + + public void testThrowNPE() + { + testThrowNPE(new SetMap()); + testThrowNPE(new ListMap()); + } + + private void testNormal(CollectionMap 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 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 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 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 map) + { + CollectionMap sm2 =3D map; + sm2.put(k1, new Object()); + Iterator i =3D map.iterator(k1); + List lst =3D get(i); + try + { + Value v =3D lst.get(0); + fail(); + } + catch (ClassCastException expected) + { + } + } + + private void testThrowNPE(CollectionMap 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 List get(Iterator i) + { + List list =3D new ArrayList(); + while (i.hasNext()) + { + V v =3D i.next(); + list.add(v); + } + return list; + } + + private static final class Key + { + } + + private static class Value + { + } + + private static class ValueExt extends Value + { + } +} --===============9198694648006256551==--