evictionListener in CollectionFactory should accept this map as an argument, to make it easier to write self-referential listeners

GitOrigin-RevId: e5257b1c282a95624fdb6cc2d53bff7c46828e22
This commit is contained in:
Alexey Kudravtsev
2024-03-20 15:33:37 +01:00
committed by intellij-monorepo-bot
parent 4458ed6efc
commit 23bbdb8cc7
8 changed files with 50 additions and 29 deletions

View File

@@ -10,6 +10,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.concurrent.ConcurrentMap;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
// ContainerUtil requires trove in classpath
@@ -39,15 +40,21 @@ public final class CollectionFactory {
return new ConcurrentSoftValueHashMap<>(null);
}
/**
* Create {@link ConcurrentMap} with hard-referenced keys and weak-referenced values.
* When the value get garbage-collected, the {@code evictionListener} is (eventually) invoked with this map and the corresponding key
*/
@Contract(value = "_ -> new", pure = true)
public static @NotNull <K, V> ConcurrentMap<@NotNull K, @NotNull V> createConcurrentWeakValueMap(
@NotNull Consumer<? super K> evictionListener) {
public static @NotNull <K, V> ConcurrentMap<@NotNull K, @NotNull V> createConcurrentWeakValueMap(@NotNull BiConsumer<? super ConcurrentMap<K,V>, ? super K> evictionListener) {
return new ConcurrentWeakValueHashMap<>(evictionListener);
}
/**
* Create {@link ConcurrentMap} with hard-referenced keys and soft-referenced values.
* When the value get garbage-collected, the {@code evictionListener} is (eventually) invoked with this map and the corresponding key
*/
@Contract(value = "_ -> new", pure = true)
public static @NotNull <K, V> ConcurrentMap<@NotNull K, @NotNull V> createConcurrentSoftValueMap(
@NotNull Consumer<? super K> evictionListener) {
public static @NotNull <K, V> ConcurrentMap<@NotNull K, @NotNull V> createConcurrentSoftValueMap(@NotNull BiConsumer<? super ConcurrentMap<K,V>, ? super K> evictionListener) {
return new ConcurrentSoftValueHashMap<>(evictionListener);
}
@@ -363,8 +370,13 @@ public final class CollectionFactory {
public static @NotNull <K, V> ConcurrentMap<@NotNull K, @NotNull V> createConcurrentSoftMap() {
return new ConcurrentSoftHashMap<>(null);
}
/**
* Create {@link ConcurrentMap} with soft-referenced keys and hard-referenced values.
* When the key get garbage-collected, the {@code evictionListener} is (eventually) invoked with this map and the corresponding value
*/
@Contract(value = "_ -> new", pure = true)
public static @NotNull <K, V> ConcurrentMap<@NotNull K, @NotNull V> createConcurrentSoftMap(@NotNull Consumer<? super V> evictionListener) {
public static @NotNull <K, V> ConcurrentMap<@NotNull K, @NotNull V> createConcurrentSoftMap(@NotNull BiConsumer<? super ConcurrentMap<K,V>, ? super V> evictionListener) {
return new ConcurrentSoftHashMap<>(evictionListener);
}

View File

@@ -8,7 +8,7 @@ import java.lang.ref.ReferenceQueue;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Consumer;
import java.util.function.BiConsumer;
/**
* Base class for concurrent (soft/weak) key:K -> strong value:V map
@@ -23,9 +23,9 @@ abstract class ConcurrentRefHashMap<K, V> extends AbstractMap<K, V> implements C
static final float LOAD_FACTOR = 0.75f;
static final int DEFAULT_CAPACITY = 16;
static final int DEFAULT_CONCURRENCY_LEVEL = Math.min(Runtime.getRuntime().availableProcessors(), 4);
private final Consumer<? super V> myEvictionListener;
private final BiConsumer<? super ConcurrentMap<K, V>, ? super V> myEvictionListener;
ConcurrentRefHashMap(@Nullable Consumer<? super V> evictionListener) {
ConcurrentRefHashMap(@Nullable BiConsumer<? super ConcurrentMap<K,V>, ? super V> evictionListener) {
myHashingStrategy = this;
myMap = new ConcurrentHashMap<>(DEFAULT_CAPACITY, LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
myEvictionListener = evictionListener;
@@ -66,7 +66,7 @@ abstract class ConcurrentRefHashMap<K, V> extends AbstractMap<K, V> implements C
while ((wk = (KeyReference<K>)myReferenceQueue.poll()) != null) {
V v = myMap.remove(wk);
if (myEvictionListener != null) {
myEvictionListener.accept(v);
myEvictionListener.accept(this, v);
}
processed = true;
}

View File

@@ -10,7 +10,7 @@ import java.lang.ref.ReferenceQueue;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Consumer;
import java.util.function.BiConsumer;
/**
* Base class for concurrent strong key:K -> (soft/weak) value:V map
@@ -20,10 +20,10 @@ import java.util.function.Consumer;
abstract class ConcurrentRefValueHashMap<K, V> implements ConcurrentMap<K, V> {
private final ConcurrentMap<K, ValueReference<K, V>> myMap = new ConcurrentHashMap<>();
private final Consumer<? super K> myEvictionListener;
private final BiConsumer<? super ConcurrentMap<K, V>, ? super K> myEvictionListener;
protected final ReferenceQueue<V> myQueue = new ReferenceQueue<>();
ConcurrentRefValueHashMap(@Nullable Consumer<? super K> evictionListener) {
ConcurrentRefValueHashMap(@Nullable BiConsumer<? super ConcurrentMap<K,V>, ? super K> evictionListener) {
myEvictionListener = evictionListener;
}
@@ -44,7 +44,7 @@ abstract class ConcurrentRefValueHashMap<K, V> implements ConcurrentMap<K, V> {
if (ref == null) break;
K key = ref.getKey();
if (myMap.remove(key, ref) && myEvictionListener != null) {
myEvictionListener.accept(key);
myEvictionListener.accept(this, key);
}
processed = true;
}

View File

@@ -6,7 +6,8 @@ import org.jetbrains.annotations.Nullable;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.util.function.Consumer;
import java.util.concurrent.ConcurrentMap;
import java.util.function.BiConsumer;
/**
* Concurrent soft key:K -> strong value:V map
@@ -15,7 +16,7 @@ import java.util.function.Consumer;
* Use {@link ContainerUtil#createConcurrentSoftMap()} to create this
*/
final class ConcurrentSoftHashMap<K, V> extends ConcurrentRefHashMap<K, V> {
ConcurrentSoftHashMap(@Nullable Consumer<? super V> evictionListener) {
ConcurrentSoftHashMap(@Nullable BiConsumer<? super ConcurrentMap<K,V>, ? super V> evictionListener) {
super(evictionListener);
}

View File

@@ -6,6 +6,8 @@ import org.jetbrains.annotations.Nullable;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.util.concurrent.ConcurrentMap;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
/**
@@ -16,7 +18,7 @@ import java.util.function.Consumer;
*/
final class ConcurrentSoftValueHashMap<K,V> extends ConcurrentRefValueHashMap<K,V> {
ConcurrentSoftValueHashMap(@Nullable Consumer<? super K> evictionListener) {
ConcurrentSoftValueHashMap(@Nullable BiConsumer<? super ConcurrentMap<K,V>, ? super K> evictionListener) {
super(evictionListener);
}

View File

@@ -7,6 +7,8 @@ import org.jetbrains.annotations.Nullable;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.concurrent.ConcurrentMap;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
/**
@@ -17,7 +19,7 @@ import java.util.function.Consumer;
*/
final class ConcurrentWeakValueHashMap<K,V> extends ConcurrentRefValueHashMap<K,V> {
ConcurrentWeakValueHashMap(@Nullable Consumer<? super K> evictionListener) {
ConcurrentWeakValueHashMap(@Nullable BiConsumer<? super ConcurrentMap<K,V>, ? super K> evictionListener) {
super(evictionListener);
}