changeset 8247:c643a1d780c5

Implements one possible correction of compute() remove case for oldValue==null. Includes other conformance issues noted by Brent Christian
author mduigou
date Thu, 18 Apr 2013 11:37:33 -0700
parents 0bba7485ca4f
children d88f460012d1
files src/share/classes/java/util/HashMap.java src/share/classes/java/util/Map.java test/java/util/Map/Defaults.java
diffstat 3 files changed, 20 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/HashMap.java	Wed Apr 17 22:05:52 2013 -0700
+++ b/src/share/classes/java/util/HashMap.java	Thu Apr 18 11:37:33 2013 -0700
@@ -631,7 +631,6 @@
                     return e.value;
                 }
                 e.value = value;
-                modCount++;
                 e.recordAccess(this);
                 return null;
             }
@@ -728,7 +727,15 @@
         for (; e != null; e = e.next) {
             if (e.hash == hash && Objects.equals(e.key, key)) {
                 V oldValue = e.value;
-                return oldValue == null ? (e.value = mappingFunction.apply(key)) : oldValue;
+                if (oldValue == null) {
+                    V newValue = mappingFunction.apply(key);
+                    e.value = newValue;
+                    e.recordAccess(this);
+                    return newValue;
+                } else {
+                    return oldValue;
+                }
+
             }
         }
 
@@ -796,9 +803,9 @@
             if (e.hash == hash && Objects.equals(e.key, key)) {
                 V oldValue = e.value;
                 V newValue = remappingFunction.apply(key, oldValue);
-                if (newValue != oldValue) {
-                    modCount++;
+                if (newValue != oldValue || oldValue == null) {
                     if (newValue == null) {
+                        modCount++;
                         size--;
                         if (prev == e)
                             table[i] = next;
--- a/src/share/classes/java/util/Map.java	Wed Apr 17 22:05:52 2013 -0700
+++ b/src/share/classes/java/util/Map.java	Thu Apr 18 11:37:33 2013 -0700
@@ -996,6 +996,9 @@
                     if ((oldValue = putIfAbsent(key, newValue)) == null)
                         return newValue;
                 } else {
+                    if(containsKey(key) && !remove(key, oldValue)) {
+                        throw new ConcurrentModificationException();
+                    }
                     return null;
                 }
             }
--- a/test/java/util/Map/Defaults.java	Wed Apr 17 22:05:52 2013 -0700
+++ b/test/java/util/Map/Defaults.java	Thu Apr 18 11:37:33 2013 -0700
@@ -274,6 +274,12 @@
         assertSame(map.compute(null, (k, v) -> {
             assertSame(k, null);
             assertNull(v);
+            return null;
+        }), null, description);
+        assertFalse(map.containsKey(null), description + ": null key present.");
+        assertSame(map.compute(null, (k, v) -> {
+            assertSame(k, null);
+            assertNull(v);
             return EXTRA_VALUE;
         }), EXTRA_VALUE, description);
         assertTrue(map.containsKey(null));