Replace map with set. (#9833)

Motivation:
Replace Map with Set. `reportedLeaks` has better semantics as a Set, and if it is a Map, it seems that the value of this Map has no meaning to us.

Modifications:

Use Set.

Result:
Cleaner code
This commit is contained in:
时无两丶 2019-12-04 20:53:11 +08:00 committed by Norman Maurer
parent 9ba13f0afc
commit df121e5e55

View File

@ -168,7 +168,8 @@ public class ResourceLeakDetector<T> {
Collections.newSetFromMap(new ConcurrentHashMap<DefaultResourceLeak<?>, Boolean>());
private final ReferenceQueue<Object> refQueue = new ReferenceQueue<Object>();
private final ConcurrentMap<String, Boolean> reportedLeaks = PlatformDependent.newConcurrentHashMap();
private final Set<String> reportedLeaks =
Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
private final String resourceType;
private final int samplingInterval;
@ -271,7 +272,6 @@ public class ResourceLeakDetector<T> {
private void clearRefQueue() {
for (;;) {
@SuppressWarnings("unchecked")
DefaultResourceLeak ref = (DefaultResourceLeak) refQueue.poll();
if (ref == null) {
break;
@ -288,7 +288,6 @@ public class ResourceLeakDetector<T> {
// Detect and report previous leaks.
for (;;) {
@SuppressWarnings("unchecked")
DefaultResourceLeak ref = (DefaultResourceLeak) refQueue.poll();
if (ref == null) {
break;
@ -299,7 +298,7 @@ public class ResourceLeakDetector<T> {
}
String records = ref.toString();
if (reportedLeaks.putIfAbsent(records, Boolean.TRUE) == null) {
if (reportedLeaks.add(records)) {
if (records.isEmpty()) {
reportUntracedLeak(resourceType);
} else {