[jboss-svn-commits] JBoss Common SVN: r2887 - in common-core/trunk/src: test/java/org/jboss/test/util/test/collection and 1 other directory.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Jul 24 09:57:35 EDT 2008
Author: alesj
Date: 2008-07-24 09:57:35 -0400 (Thu, 24 Jul 2008)
New Revision: 2887
Added:
common-core/trunk/src/main/java/org/jboss/util/collection/ConcurrentSet.java
common-core/trunk/src/test/java/org/jboss/test/util/test/collection/AbstractNullableSetUnitTest.java
common-core/trunk/src/test/java/org/jboss/test/util/test/collection/AbstractSetUnitTest.java
common-core/trunk/src/test/java/org/jboss/test/util/test/collection/ConcurrentSetUnitTestCase.java
Modified:
common-core/trunk/src/main/java/org/jboss/util/collection/CollectionsFactory.java
common-core/trunk/src/test/java/org/jboss/test/util/test/collection/WeakSetUnitTestCase.java
Log:
[JBCOMMON-61]; add concurrent set.
Modified: common-core/trunk/src/main/java/org/jboss/util/collection/CollectionsFactory.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/collection/CollectionsFactory.java 2008-07-23 17:09:45 UTC (rev 2886)
+++ common-core/trunk/src/main/java/org/jboss/util/collection/CollectionsFactory.java 2008-07-24 13:57:35 UTC (rev 2887)
@@ -28,10 +28,6 @@
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
-import org.jboss.util.collection.LazyList;
-import org.jboss.util.collection.LazyMap;
-import org.jboss.util.collection.LazySet;
-
/**
* Collections factory.
*
@@ -107,4 +103,15 @@
{
return new CopyOnWriteArraySet<T>();
}
+
+ /**
+ * Defines the concurrent set implementation
+ *
+ * @param <T> the type
+ * @return the set
+ */
+ public static final <T> Set<T> createConcurrentSet()
+ {
+ return new ConcurrentSet<T>();
+ }
}
Copied: common-core/trunk/src/main/java/org/jboss/util/collection/ConcurrentSet.java (from rev 2882, common-core/trunk/src/main/java/org/jboss/util/collection/LazySet.java)
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/collection/ConcurrentSet.java (rev 0)
+++ common-core/trunk/src/main/java/org/jboss/util/collection/ConcurrentSet.java 2008-07-24 13:57:35 UTC (rev 2887)
@@ -0,0 +1,179 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., 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.util.collection;
+
+import java.io.Serializable;
+import java.util.AbstractSet;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * ConcurrentSet based on top of ConcurrenthashMap.
+ * It's serializable if the elements are serializable.
+ *
+ * @param <E> the element type
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ */
+public class ConcurrentSet<E> extends AbstractSet<E> implements Serializable
+{
+ /** The serialVersionUID */
+ private static final long serialVersionUID = 1L;
+ /** The delegate set */
+ private ConcurrentHashMap<E, Object> map;
+ /** The dummy object */
+ private static final Object PRESENT = new Object();
+
+ /**
+ * Constructs a new, empty set; the backing <tt>ConcurrentHashMap</tt> instance has
+ * default initial capacity (16) and load factor (0.75).
+ */
+ public ConcurrentSet()
+ {
+ map = new ConcurrentHashMap<E, Object>();
+ }
+
+ /**
+ * Constructs a new set containing the elements in the specified
+ * collection. The <tt>ConcurrentHashMap</tt> is created with default load factor
+ * (0.75) and an initial capacity sufficient to contain the elements in
+ * the specified collection.
+ *
+ * @param c the collection whose elements are to be placed into this set.
+ * @throws NullPointerException if the specified collection is null.
+ */
+ public ConcurrentSet(Collection<? extends E> c)
+ {
+ map = new ConcurrentHashMap<E, Object>(Math.max((int)(c.size() / .75f) + 1, 16));
+ addAll(c);
+ }
+
+ /**
+ * Constructs a new, empty set; the backing <tt>ConcurrentHashMap</tt> instance has
+ * the specified initial capacity and the specified load factor.
+ *
+ * @param initialCapacity the initial capacity. The implementation
+ * performs internal sizing to accommodate this many elements.
+ * @param loadFactor the load factor threshold, used to control resizing.
+ * Resizing may be performed when the average number of elements per
+ * bin exceeds this threshold.
+ * @param concurrencyLevel the estimated number of concurrently
+ * updating threads. The implementation performs internal sizing
+ * to try to accommodate this many threads.
+ * @throws IllegalArgumentException if the initial capacity is less
+ * than zero, or if the load factor is nonpositive.
+ */
+ public ConcurrentSet(int initialCapacity, float loadFactor, int concurrencyLevel)
+ {
+ map = new ConcurrentHashMap<E, Object>(initialCapacity, loadFactor, concurrencyLevel);
+ }
+
+ /**
+ * Constructs a new, empty set; the backing <tt>ConcurrentHashMap</tt> instance has
+ * the specified initial capacity and default load factor, which is
+ * <tt>0.75</tt>.
+ *
+ * @param initialCapacity the initial capacity of the hash table.
+ * @throws IllegalArgumentException if the initial capacity is less
+ * than zero.
+ */
+ public ConcurrentSet(int initialCapacity)
+ {
+ map = new ConcurrentHashMap<E, Object>(initialCapacity);
+ }
+
+ /**
+ * Returns an iterator over the elements in this set. The elements
+ * are returned in no particular order.
+ *
+ * @return an Iterator over the elements in this set.
+ * @see java.util.ConcurrentModificationException
+ */
+ public Iterator<E> iterator()
+ {
+ return map.keySet().iterator();
+ }
+
+ /**
+ * Returns the number of elements in this set (its cardinality).
+ *
+ * @return the number of elements in this set (its cardinality).
+ */
+ public int size()
+ {
+ return map.size();
+ }
+
+ /**
+ * Returns <tt>true</tt> if this set contains no elements.
+ *
+ * @return <tt>true</tt> if this set contains no elements.
+ */
+ public boolean isEmpty()
+ {
+ return map.isEmpty();
+ }
+
+ /**
+ * Returns <tt>true</tt> if this set contains the specified element.
+ *
+ * @param o element whose presence in this set is to be tested.
+ * @return <tt>true</tt> if this set contains the specified element.
+ */
+ @SuppressWarnings({"SuspiciousMethodCalls"})
+ public boolean contains(Object o)
+ {
+ return map.containsKey(o);
+ }
+
+ /**
+ * Adds the specified element to this set if it is not already
+ * present.
+ *
+ * @param o element to be added to this set.
+ * @return <tt>true</tt> if the set did not already contain the specified
+ * element.
+ */
+ public boolean add(E o)
+ {
+ return map.put(o, PRESENT) == null;
+ }
+
+ /**
+ * Removes the specified element from this set if it is present.
+ *
+ * @param o object to be removed from this set, if present.
+ * @return <tt>true</tt> if the set contained the specified element.
+ */
+ public boolean remove(Object o)
+ {
+ return map.remove(o) == PRESENT;
+ }
+
+ /**
+ * Removes all of the elements from this set.
+ */
+ public void clear()
+ {
+ map.clear();
+ }
+}
\ No newline at end of file
Added: common-core/trunk/src/test/java/org/jboss/test/util/test/collection/AbstractNullableSetUnitTest.java
===================================================================
--- common-core/trunk/src/test/java/org/jboss/test/util/test/collection/AbstractNullableSetUnitTest.java (rev 0)
+++ common-core/trunk/src/test/java/org/jboss/test/util/test/collection/AbstractNullableSetUnitTest.java 2008-07-24 13:57:35 UTC (rev 2887)
@@ -0,0 +1,31 @@
+package org.jboss.test.util.test.collection;
+
+import java.util.Iterator;
+import java.util.Set;
+
+/**
+ * Unit tests for WeakSet
+ *
+ * @author <a href="mailto:sven at meiers.net">Sven Meier</a>
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ * @version $Revision: 43534 $
+ */
+ at SuppressWarnings("unchecked")
+public abstract class AbstractNullableSetUnitTest extends AbstractSetUnitTest
+{
+ protected abstract Set createSet();
+
+ public void testNullElement()
+ {
+ Set set = createSet();
+
+ set.add(null);
+
+ assertEquals(1, set.size());
+
+ Iterator iterator = set.iterator();
+ assertTrue(iterator.hasNext());
+ iterator.next();
+ assertFalse(iterator.hasNext());
+ }
+}
\ No newline at end of file
Copied: common-core/trunk/src/test/java/org/jboss/test/util/test/collection/AbstractSetUnitTest.java (from rev 2882, common-core/trunk/src/test/java/org/jboss/test/util/test/collection/WeakSetUnitTestCase.java)
===================================================================
--- common-core/trunk/src/test/java/org/jboss/test/util/test/collection/AbstractSetUnitTest.java (rev 0)
+++ common-core/trunk/src/test/java/org/jboss/test/util/test/collection/AbstractSetUnitTest.java 2008-07-24 13:57:35 UTC (rev 2887)
@@ -0,0 +1,30 @@
+package org.jboss.test.util.test.collection;
+
+import java.util.Iterator;
+import java.util.Set;
+
+import junit.framework.TestCase;
+
+/**
+ * Unit tests for WeakSet
+ *
+ * @author <a href="mailto:sven at meiers.net">Sven Meier</a>
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ * @version $Revision: 43534 $
+ */
+ at SuppressWarnings("unchecked")
+public abstract class AbstractSetUnitTest extends TestCase
+{
+ protected abstract Set createSet();
+
+ public void testMultipleHasNext()
+ {
+ Set set = createSet();
+
+ set.add(new Object());
+
+ Iterator iterator = set.iterator();
+ assertTrue(iterator.hasNext());
+ assertTrue(iterator.hasNext());
+ }
+}
\ No newline at end of file
Copied: common-core/trunk/src/test/java/org/jboss/test/util/test/collection/ConcurrentSetUnitTestCase.java (from rev 2882, common-core/trunk/src/test/java/org/jboss/test/util/test/collection/WeakSetUnitTestCase.java)
===================================================================
--- common-core/trunk/src/test/java/org/jboss/test/util/test/collection/ConcurrentSetUnitTestCase.java (rev 0)
+++ common-core/trunk/src/test/java/org/jboss/test/util/test/collection/ConcurrentSetUnitTestCase.java 2008-07-24 13:57:35 UTC (rev 2887)
@@ -0,0 +1,19 @@
+package org.jboss.test.util.test.collection;
+
+import java.util.Set;
+
+import org.jboss.util.collection.ConcurrentSet;
+
+/**
+ * Unit tests for ConcurrentSet
+ *
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+ at SuppressWarnings("unchecked")
+public class ConcurrentSetUnitTestCase extends AbstractSetUnitTest
+{
+ protected Set createSet()
+ {
+ return new ConcurrentSet();
+ }
+}
\ No newline at end of file
Modified: common-core/trunk/src/test/java/org/jboss/test/util/test/collection/WeakSetUnitTestCase.java
===================================================================
--- common-core/trunk/src/test/java/org/jboss/test/util/test/collection/WeakSetUnitTestCase.java 2008-07-23 17:09:45 UTC (rev 2886)
+++ common-core/trunk/src/test/java/org/jboss/test/util/test/collection/WeakSetUnitTestCase.java 2008-07-24 13:57:35 UTC (rev 2887)
@@ -1,42 +1,21 @@
package org.jboss.test.util.test.collection;
-import java.util.Iterator;
+import java.util.Set;
import org.jboss.util.collection.WeakSet;
-import junit.framework.TestCase;
-
/**
* Unit tests for WeakSet
*
* @author <a href="mailto:sven at meiers.net">Sven Meier</a>
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
* @version $Revision: 43534 $
*/
@SuppressWarnings("unchecked")
-public class WeakSetUnitTestCase extends TestCase
+public class WeakSetUnitTestCase extends AbstractNullableSetUnitTest
{
- public void testNullElement()
+ protected Set createSet()
{
- WeakSet set = new WeakSet();
-
- set.add(null);
-
- assertEquals(1, set.size());
-
- Iterator iterator = set.iterator();
- assertTrue(iterator.hasNext());
- iterator.next();
- assertFalse(iterator.hasNext());
+ return new WeakSet();
}
-
- public void testMultipleHasNext()
- {
- WeakSet set = new WeakSet();
-
- set.add(new Object());
-
- Iterator iterator = set.iterator();
- assertTrue(iterator.hasNext());
- assertTrue(iterator.hasNext());
- }
}
More information about the jboss-svn-commits
mailing list