aboutsummaryrefslogtreecommitdiff
path: root/src/java.base/share/classes/java/util/WeakHashMap.java
diff options
context:
space:
mode:
authorMandy Chung <mchung@openjdk.org>2020-12-06 00:08:22 +0000
committerMandy Chung <mchung@openjdk.org>2020-12-06 00:08:22 +0000
commit972bc3b408251b0566f1f488382e66b8cee3c8dd (patch)
tree64b6b42b6df389763c3860c7d6d40c8b32e887f2 /src/java.base/share/classes/java/util/WeakHashMap.java
parent78be334c3817a1b5840922a9bf1339a40dcc5185 (diff)
downloadopenjdk-972bc3b408251b0566f1f488382e66b8cee3c8dd.tar.gz
openjdk-972bc3b408251b0566f1f488382e66b8cee3c8dd.zip
8256167: Convert JDK use of `Reference::get` to `Reference::refersTo`
Reviewed-by: sspitsyn, shade, dfuchs, alanb, kbarrett
Diffstat (limited to 'src/java.base/share/classes/java/util/WeakHashMap.java')
-rw-r--r--src/java.base/share/classes/java/util/WeakHashMap.java24
1 files changed, 14 insertions, 10 deletions
diff --git a/src/java.base/share/classes/java/util/WeakHashMap.java b/src/java.base/share/classes/java/util/WeakHashMap.java
index 7a3b3dd3f4e..1f4a445eb12 100644
--- a/src/java.base/share/classes/java/util/WeakHashMap.java
+++ b/src/java.base/share/classes/java/util/WeakHashMap.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -27,7 +27,6 @@ package java.util;
import java.lang.ref.WeakReference;
import java.lang.ref.ReferenceQueue;
-import java.util.concurrent.ThreadLocalRandom;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
@@ -283,8 +282,14 @@ public class WeakHashMap<K,V>
* Checks for equality of non-null reference x and possibly-null y. By
* default uses Object.equals.
*/
- private static boolean eq(Object x, Object y) {
- return x == y || x.equals(y);
+ private boolean matchesKey(Entry<K,V> e, Object key) {
+ // check if the given entry refers to the given key without
+ // keeping a strong reference to the entry's referent
+ if (e.refersTo(key)) return true;
+
+ // then check for equality if the referent is not cleared
+ Object k = e.get();
+ return k != null && key.equals(k);
}
/**
@@ -399,7 +404,7 @@ public class WeakHashMap<K,V>
int index = indexFor(h, tab.length);
Entry<K,V> e = tab[index];
while (e != null) {
- if (e.hash == h && eq(k, e.get()))
+ if (e.hash == h && matchesKey(e, k))
return e.value;
e = e.next;
}
@@ -428,7 +433,7 @@ public class WeakHashMap<K,V>
Entry<K,V>[] tab = getTable();
int index = indexFor(h, tab.length);
Entry<K,V> e = tab[index];
- while (e != null && !(e.hash == h && eq(k, e.get())))
+ while (e != null && !(e.hash == h && matchesKey(e, k)))
e = e.next;
return e;
}
@@ -452,7 +457,7 @@ public class WeakHashMap<K,V>
int i = indexFor(h, tab.length);
for (Entry<K,V> e = tab[i]; e != null; e = e.next) {
- if (h == e.hash && eq(k, e.get())) {
+ if (h == e.hash && matchesKey(e, k)) {
V oldValue = e.value;
if (value != oldValue)
e.value = value;
@@ -515,8 +520,7 @@ public class WeakHashMap<K,V>
src[j] = null;
while (e != null) {
Entry<K,V> next = e.next;
- Object key = e.get();
- if (key == null) {
+ if (e.refersTo(null)) {
e.next = null; // Help GC
e.value = null; // " "
size--;
@@ -597,7 +601,7 @@ public class WeakHashMap<K,V>
while (e != null) {
Entry<K,V> next = e.next;
- if (h == e.hash && eq(k, e.get())) {
+ if (h == e.hash && matchesKey(e, k)) {
modCount++;
size--;
if (prev == e)