[jboss-svn-commits] JBoss Common SVN: r2584 - in common-core/branches/2_0/src: test/java/org/jboss/test/util/test and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Sep 25 11:33:07 EDT 2007


Author: dimitris at jboss.org
Date: 2007-09-25 11:33:07 -0400 (Tue, 25 Sep 2007)
New Revision: 2584

Added:
   common-core/branches/2_0/src/test/java/org/jboss/test/util/test/collection/
   common-core/branches/2_0/src/test/java/org/jboss/test/util/test/collection/WeakSetUnitTestCase.java
Modified:
   common-core/branches/2_0/src/main/java/org/jboss/util/collection/WeakSet.java
Log:
JBCOMMON-24, handle null values and multiple invocations of hasNext()

Modified: common-core/branches/2_0/src/main/java/org/jboss/util/collection/WeakSet.java
===================================================================
--- common-core/branches/2_0/src/main/java/org/jboss/util/collection/WeakSet.java	2007-09-25 15:30:34 UTC (rev 2583)
+++ common-core/branches/2_0/src/main/java/org/jboss/util/collection/WeakSet.java	2007-09-25 15:33:07 UTC (rev 2584)
@@ -1,33 +1,32 @@
 /*
-  * JBoss, Home of Professional Open Source
-  * Copyright 2005, 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.
-  */
+ * 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.lang.ref.ReferenceQueue;
-
-import java.util.Set;
+import java.util.AbstractSet;
 import java.util.HashSet;
-import java.util.AbstractSet;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
+import java.util.Set;
 
 import org.jboss.util.NullArgumentException;
 import org.jboss.util.WeakObject;
@@ -117,10 +116,16 @@
             /** The set's iterator */
             Iterator iter = set.iterator();
 
+            /** JBCOMMON-24, handle null values and multiple invocations of hasNext() */
+            Object UNKNOWN = new Object();
+            
             /** The next available object. */
-            Object next = null;
+            Object next = UNKNOWN;
 
             public boolean hasNext() {
+               if (next != UNKNOWN) {
+                  return true;
+               }
                while (iter.hasNext()) {
                   WeakObject weak = (WeakObject)iter.next();
                   Object obj = null;
@@ -128,21 +133,18 @@
                      // object has been reclaimed by the GC
                      continue;
                   }
-
                   next = obj;
                   return true;
                }
-
                return false;
             }
 
             public Object next() {
-               if ((next == null) && !hasNext()) {
+               if ((next == UNKNOWN) && !hasNext()) {
                   throw new NoSuchElementException();
                }
-
                Object obj = next;
-               next = null;
+               next = UNKNOWN;
 
                return obj;
             }

Added: common-core/branches/2_0/src/test/java/org/jboss/test/util/test/collection/WeakSetUnitTestCase.java
===================================================================
--- common-core/branches/2_0/src/test/java/org/jboss/test/util/test/collection/WeakSetUnitTestCase.java	                        (rev 0)
+++ common-core/branches/2_0/src/test/java/org/jboss/test/util/test/collection/WeakSetUnitTestCase.java	2007-09-25 15:33:07 UTC (rev 2584)
@@ -0,0 +1,41 @@
+package org.jboss.test.util.test.collection;
+
+import java.util.Iterator;
+
+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> 
+ * @version $Revision: 43534 $
+ */
+public class WeakSetUnitTestCase extends TestCase
+{
+   public void testNullElement()
+   {
+      WeakSet set = new WeakSet();
+        
+      set.add(null);
+        
+      assertEquals(1, set.size());
+        
+      Iterator iterator = set.iterator();
+      assertTrue(iterator.hasNext());     
+      iterator.next();
+      assertFalse(iterator.hasNext());
+   }
+    
+   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