[jboss-svn-commits] JBoss Common SVN: r3950 - 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
Fri Jan 22 11:01:41 EST 2010


Author: alesj
Date: 2010-01-22 11:01:40 -0500 (Fri, 22 Jan 2010)
New Revision: 3950

Added:
   common-core/trunk/src/main/java/org/jboss/util/collection/ConcurrentReferenceHashSet.java
   common-core/trunk/src/main/java/org/jboss/util/collection/MapDelegateSet.java
   common-core/trunk/src/test/java/org/jboss/test/util/test/collection/ConcurrentRefSetUnitTestCase.java
Modified:
   common-core/trunk/src/main/java/org/jboss/util/collection/ConcurrentReferenceHashMap.java
   common-core/trunk/src/main/java/org/jboss/util/collection/ConcurrentSet.java
Log:
[JBCOMMON-104]; concurrent reference set based on CRHMap.

Modified: common-core/trunk/src/main/java/org/jboss/util/collection/ConcurrentReferenceHashMap.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/collection/ConcurrentReferenceHashMap.java	2010-01-22 14:30:18 UTC (rev 3949)
+++ common-core/trunk/src/main/java/org/jboss/util/collection/ConcurrentReferenceHashMap.java	2010-01-22 16:01:40 UTC (rev 3950)
@@ -971,6 +971,7 @@
      *
      * @param keyType the reference type to use for keys
      * @param valueType the reference type to use for values
+     * @param options the options
      * @throws IllegalArgumentException if the initial capacity of
      * elements is negative.
      */

Copied: common-core/trunk/src/main/java/org/jboss/util/collection/ConcurrentReferenceHashSet.java (from rev 3949, common-core/trunk/src/main/java/org/jboss/util/collection/ConcurrentSet.java)
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/collection/ConcurrentReferenceHashSet.java	                        (rev 0)
+++ common-core/trunk/src/main/java/org/jboss/util/collection/ConcurrentReferenceHashSet.java	2010-01-22 16:01:40 UTC (rev 3950)
@@ -0,0 +1,158 @@
+/*
+* 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.util.EnumSet;
+import java.util.Set;
+
+/**
+ * ConcurrentSet based on top of ConcurrentReferenceHashMap.
+ * It's serializable if the elements are serializable.
+ *
+ * @author <a href="ales.justin at jboss.org">Ales Justin</a>
+ * @param <E> the element type
+ */
+public class ConcurrentReferenceHashSet<E> extends MapDelegateSet<E>
+{
+   /**
+    * The serialVersionUID
+    */
+   private static final long serialVersionUID = 1L;
+
+   /**
+    * Creates a new, empty set with the specified initial
+    * capacity, load factor and concurrency level.
+    *
+    * @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
+    *                                  negative or the load factor or concurrencyLevel are
+    *                                  nonpositive.
+    */
+   public ConcurrentReferenceHashSet(int initialCapacity, float loadFactor, int concurrencyLevel)
+   {
+      super(new ConcurrentReferenceHashMap<E, Object>(initialCapacity, loadFactor, concurrencyLevel));
+   }
+
+   /**
+    * Creates a new, empty set with the specified initial capacity
+    * and load factor and with the default reference types (weak keys,
+    * strong values), and concurrencyLevel (16).
+    *
+    * @param initialCapacity 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.
+    * @throws IllegalArgumentException if the initial capacity of
+    *                                  elements is negative or the load factor is nonpositive
+    * @since 1.6
+    */
+   public ConcurrentReferenceHashSet(int initialCapacity, float loadFactor)
+   {
+      super(new ConcurrentReferenceHashMap<E, Object>(initialCapacity, loadFactor));
+   }
+
+   /**
+    * Creates a new, empty set with the specified initial capacity,
+    * reference type and with default load factor (0.75) and concurrencyLevel (16).
+    *
+    * @param initialCapacity the initial capacity. The implementation
+    *                        performs internal sizing to accommodate this many elements.
+    * @param type         the reference type to use
+    * @throws IllegalArgumentException if the initial capacity of
+    *                                  elements is negative.
+    */
+   public ConcurrentReferenceHashSet(int initialCapacity, ConcurrentReferenceHashMap.ReferenceType type)
+   {
+      super(new ConcurrentReferenceHashMap<E, Object>(initialCapacity, type, ConcurrentReferenceHashMap.ReferenceType.STRONG));
+   }
+
+   /**
+    * Creates a new, empty reference set with the specified key
+    * and value reference types.
+    *
+    * @param type         the reference type to use
+    * @throws IllegalArgumentException if the initial capacity of
+    *                                  elements is negative.
+    */
+   public ConcurrentReferenceHashSet(ConcurrentReferenceHashMap.ReferenceType type)
+   {
+      super(new ConcurrentReferenceHashMap<E, Object>(type, ConcurrentReferenceHashMap.ReferenceType.STRONG));
+   }
+
+   /**
+    * Creates a new, empty reference set with the specified reference types
+    * and behavioral options.
+    *
+    * @param type         the reference type to use
+    * @param options   the options
+    * @throws IllegalArgumentException if the initial capacity of
+    *                                  elements is negative.
+    */
+   public ConcurrentReferenceHashSet(ConcurrentReferenceHashMap.ReferenceType type, EnumSet<ConcurrentReferenceHashMap.Option> options)
+   {
+      super(new ConcurrentReferenceHashMap<E, Object>(type, ConcurrentReferenceHashMap.ReferenceType.STRONG, options));
+   }
+
+   /**
+    * Creates a new, empty set with the specified initial capacity,
+    * and with default reference types (weak keys, strong values),
+    * load factor (0.75) and concurrencyLevel (16).
+    *
+    * @param initialCapacity the initial capacity. The implementation
+    *                        performs internal sizing to accommodate this many elements.
+    * @throws IllegalArgumentException if the initial capacity of
+    *                                  elements is negative.
+    */
+   public ConcurrentReferenceHashSet(int initialCapacity)
+   {
+      super(new ConcurrentReferenceHashMap<E, Object>(initialCapacity));
+   }
+
+   /**
+    * Creates a new, empty set with a default initial capacity (16),
+    * reference types (weak keys, strong values), default
+    * load factor (0.75) and concurrencyLevel (16).
+    */
+   public ConcurrentReferenceHashSet()
+   {
+      super(new ConcurrentReferenceHashMap<E, Object>());
+   }
+
+   /**
+    * Creates a new set with the same contents as the given set.
+    *
+    * @param s the set
+    */
+   public ConcurrentReferenceHashSet(Set<? extends E> s)
+   {
+      super(new ConcurrentReferenceHashMap<E, Object>());
+      addAll(s);
+   }
+}
\ No newline at end of file


Property changes on: common-core/trunk/src/main/java/org/jboss/util/collection/ConcurrentReferenceHashSet.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native

Modified: common-core/trunk/src/main/java/org/jboss/util/collection/ConcurrentSet.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/collection/ConcurrentSet.java	2010-01-22 14:30:18 UTC (rev 3949)
+++ common-core/trunk/src/main/java/org/jboss/util/collection/ConcurrentSet.java	2010-01-22 16:01:40 UTC (rev 3950)
@@ -22,9 +22,7 @@
 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;
 
 /**
@@ -32,16 +30,12 @@
  * It's serializable if the elements are serializable.
  *
  * @param <E> the element type
- * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ * @author <a href="ales.justin at jboss.org">Ales Justin</a>
  */
-public class ConcurrentSet<E> extends AbstractSet<E> implements Serializable
+public class ConcurrentSet<E> extends MapDelegateSet<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
@@ -49,7 +43,7 @@
     */
    public ConcurrentSet()
    {
-      map = new ConcurrentHashMap<E, Object>();
+      super(new ConcurrentHashMap<E, Object>());
    }
 
    /**
@@ -63,7 +57,7 @@
     */
    public ConcurrentSet(Collection<? extends E> c)
    {
-      map = new ConcurrentHashMap<E, Object>(Math.max((int)(c.size() / .75f) + 1, 16));
+      super(new ConcurrentHashMap<E, Object>(Math.max((int)(c.size() / .75f) + 1, 16)));
       addAll(c);
    }
 
@@ -84,7 +78,7 @@
     */
    public ConcurrentSet(int initialCapacity, float loadFactor, int concurrencyLevel)
    {
-      map = new ConcurrentHashMap<E, Object>(initialCapacity, loadFactor, concurrencyLevel);
+      super(new ConcurrentHashMap<E, Object>(initialCapacity, loadFactor, concurrencyLevel));
    }
 
    /**
@@ -98,88 +92,6 @@
     */
    public ConcurrentSet(int initialCapacity)
    {
-      map = new ConcurrentHashMap<E, Object>(initialCapacity);
+      super(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();
-   }
-
-   @Override
-   public String toString()
-   {
-      return map.keySet().toString();
-   }
 }
\ No newline at end of file

Added: common-core/trunk/src/main/java/org/jboss/util/collection/MapDelegateSet.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/collection/MapDelegateSet.java	                        (rev 0)
+++ common-core/trunk/src/main/java/org/jboss/util/collection/MapDelegateSet.java	2010-01-22 16:01:40 UTC (rev 3950)
@@ -0,0 +1,136 @@
+/*
+* 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.Iterator;
+import java.util.Map;
+
+/**
+ * Set implemented by a backing Map.
+ * It's serializable if the elements are serializable.
+ *
+ * @param <E> the element type
+ * @author <a href="ales.justin at jboss.org">Ales Justin</a>
+ */
+public abstract class MapDelegateSet<E> extends AbstractSet<E> implements Serializable
+{
+   /** The delegate map */
+   private final Map<E, Object> map;
+   /** The dummy object */
+   private static final Object PRESENT = new Object();
+
+   /**
+    * Set the initial map.
+    *
+    * @param map the initial map
+    */
+   protected MapDelegateSet(Map<E, Object> map)
+   {
+      if (map == null)
+         throw new IllegalArgumentException("Null map");
+      this.map = map;
+   }
+
+   /**
+    * 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();
+   }
+
+   @Override
+   public String toString()
+   {
+      return map.keySet().toString();
+   }
+}
\ No newline at end of file

Copied: common-core/trunk/src/test/java/org/jboss/test/util/test/collection/ConcurrentRefSetUnitTestCase.java (from rev 3949, common-core/trunk/src/test/java/org/jboss/test/util/test/collection/ConcurrentSetUnitTestCase.java)
===================================================================
--- common-core/trunk/src/test/java/org/jboss/test/util/test/collection/ConcurrentRefSetUnitTestCase.java	                        (rev 0)
+++ common-core/trunk/src/test/java/org/jboss/test/util/test/collection/ConcurrentRefSetUnitTestCase.java	2010-01-22 16:01:40 UTC (rev 3950)
@@ -0,0 +1,19 @@
+package org.jboss.test.util.test.collection;
+
+import java.util.Set;
+
+import org.jboss.util.collection.ConcurrentReferenceHashSet;
+
+/**
+ * Unit tests for ConcurrentReferenceSet
+ *
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+ at SuppressWarnings("unchecked")
+public class ConcurrentRefSetUnitTestCase extends AbstractSetUnitTest
+{
+   protected Set createSet()
+   {
+      return new ConcurrentReferenceHashSet();
+   }
+}
\ No newline at end of file



More information about the jboss-svn-commits mailing list