[jbosscache-commits] JBoss Cache SVN: r4639 - in core/trunk/src: test/java/org/jboss/cache/util and 1 other directory.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Wed Oct 17 21:55:48 EDT 2007


Author: genman
Date: 2007-10-17 21:55:47 -0400 (Wed, 17 Oct 2007)
New Revision: 4639

Modified:
   core/trunk/src/main/java/org/jboss/cache/util/DeltaMap.java
   core/trunk/src/test/java/org/jboss/cache/util/DeltaMapTest.java
Log:
Fix bug with iteration

Modified: core/trunk/src/main/java/org/jboss/cache/util/DeltaMap.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/util/DeltaMap.java	2007-10-17 19:18:52 UTC (rev 4638)
+++ core/trunk/src/main/java/org/jboss/cache/util/DeltaMap.java	2007-10-18 01:55:47 UTC (rev 4639)
@@ -192,6 +192,14 @@
       exclude.clear();
       changed.clear();
    }
+   
+   /**
+    * Returns true if the internal map was modified. 
+    */
+   public boolean isModified()
+   {
+      return !changed.isEmpty() || !exclude.isEmpty();
+   }
 
    /**
     * Iterator that skips over removed entries.
@@ -217,23 +225,24 @@
             return true;
          if (orig)
          {
-            while (i.hasNext() && redef(next = i.next()));
-            nextSet = true;
-            if (!i.hasNext())
-            {
-               orig = false;
-               i = changed.entrySet().iterator();
+            while (true) {
+               if (!i.hasNext()) {
+                  orig = false;
+                  i = changed.entrySet().iterator();
+                  return hasNext();
+               }
+               next = i.next();
+               if (!redef(next)) {
+                  nextSet = true;
+                  return true;
+               }
             }
          }
-         else
-         {
-            if (!i.hasNext())
-               return false;
-            next = i.next();
-            nextSet = true;
-            return true;
-         }
-         return i.hasNext();
+        if (!i.hasNext())
+           return false;
+        next = i.next();
+        nextSet = true;
+        return true;
       }
 
       public java.util.Map.Entry<K, V> next()
@@ -271,4 +280,20 @@
       changed.clear();
    }
 
+   /**
+    * Returns a Map of the entries changed, not including those removed.
+    */
+   public Map<K, V> getChanged()
+   {
+      return changed;
+   }
+
+   /**
+    * Returns the entries removed, including entries excluded by the constructor.
+    */
+   public Set<K> getRemoved()
+   {
+      return exclude;
+   }
+
 }

Modified: core/trunk/src/test/java/org/jboss/cache/util/DeltaMapTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/util/DeltaMapTest.java	2007-10-17 19:18:52 UTC (rev 4638)
+++ core/trunk/src/test/java/org/jboss/cache/util/DeltaMapTest.java	2007-10-18 01:55:47 UTC (rev 4639)
@@ -7,6 +7,7 @@
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 import java.util.Map.Entry;
+import java.util.concurrent.ConcurrentHashMap;
 
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
@@ -36,12 +37,25 @@
       hm.put(K, Y);
       backup = new HashMap<String, String>(hm);
       dm = DeltaMap.create(hm);
+      assertEquals(false, dm.isModified());
+      assertEquals(hm, dm);
    }
    
+   public void testConcurrent() throws Exception
+   {
+      ConcurrentHashMap<Object, String> m = new ConcurrentHashMap<Object, String>();
+      m.put(new Object(), Z);
+      m.put(new Object(), Y);
+      DeltaMap<Object, String> dm = DeltaMap.create(m);
+      assertEquals(m, dm);
+      assertEquals(m.toString(), dm.toString());
+   }
+   
    public void testChanges() throws Exception
    {
       assertEquals("" + dm.toDebugString(), backup, dm);
       assertEquals(Z, dm.remove(Y));
+      assertEquals(true, dm.isModified());
       assertEquals(null, dm.remove(Y));
       assertEquals("changes not made to underlying map", backup, hm);
       assertEquals(false, dm.containsKey(Y));
@@ -75,6 +89,7 @@
    public void testExclude2() throws Exception
    {
        dm = DeltaMap.excludeKeys(hm, hm.keySet());
+       assertEquals(true, dm.isModified());
        assertEquals(0, dm.size());
    }
    




More information about the jbosscache-commits mailing list