32 import java.util.AbstractMap;
33 import java.util.Collection;
34 import java.util.HashMap;
35 import java.util.Objects;
36 import java.util.Map;
37 import java.util.Set;
38 import java.util.concurrent.ConcurrentHashMap;
39 import java.util.function.Supplier;
40 import java.util.function.UnaryOperator;
41 import java.util.stream.Collectors;
42 import java.util.stream.Stream;
43
44 import jdk.internal.access.SharedSecrets;
45 import jdk.internal.misc.CDS;
46
47 /**
48 * This class provides management of {@link Map maps} where it is desirable to
49 * remove entries automatically when the key is garbage collected. This is
50 * accomplished by using a backing map where the keys are either a
51 * {@link WeakReference} or a {@link SoftReference}.
52 * <p>
53 * To create a {@link ReferencedKeyMap} the user must provide a {@link Supplier}
54 * of the backing map and whether {@link WeakReference} or
55 * {@link SoftReference} is to be used.
56 *
57 * {@snippet :
58 * // Use HashMap and WeakReference
59 * Map<Long, String> map = ReferencedKeyMap.create(false, HashMap::new);
60 * map.put(10_000_000L, "a");
61 * map.put(10_000_001L, "b");
62 * map.put(10_000_002L, "c");
63 * map.put(10_000_003L, "d");
64 * map.put(10_000_004L, "e");
65 *
66 * // Use ConcurrentHashMap and SoftReference
67 * map = ReferencedKeyMap.create(true, ConcurrentHashMap::new);
68 * map.put(20_000_000L, "v");
69 * map.put(20_000_001L, "w");
70 * map.put(20_000_002L, "x");
71 * map.put(20_000_003L, "y");
|
32 import java.util.AbstractMap;
33 import java.util.Collection;
34 import java.util.HashMap;
35 import java.util.Objects;
36 import java.util.Map;
37 import java.util.Set;
38 import java.util.concurrent.ConcurrentHashMap;
39 import java.util.function.Supplier;
40 import java.util.function.UnaryOperator;
41 import java.util.stream.Collectors;
42 import java.util.stream.Stream;
43
44 import jdk.internal.access.SharedSecrets;
45 import jdk.internal.misc.CDS;
46
47 /**
48 * This class provides management of {@link Map maps} where it is desirable to
49 * remove entries automatically when the key is garbage collected. This is
50 * accomplished by using a backing map where the keys are either a
51 * {@link WeakReference} or a {@link SoftReference}.
52 * Keys must be {@linkplain Class#isIdentity() identity objects.}
53 * <p>
54 * To create a {@link ReferencedKeyMap} the user must provide a {@link Supplier}
55 * of the backing map and whether {@link WeakReference} or
56 * {@link SoftReference} is to be used.
57 *
58 * {@snippet :
59 * // Use HashMap and WeakReference
60 * Map<Long, String> map = ReferencedKeyMap.create(false, HashMap::new);
61 * map.put(10_000_000L, "a");
62 * map.put(10_000_001L, "b");
63 * map.put(10_000_002L, "c");
64 * map.put(10_000_003L, "d");
65 * map.put(10_000_004L, "e");
66 *
67 * // Use ConcurrentHashMap and SoftReference
68 * map = ReferencedKeyMap.create(true, ConcurrentHashMap::new);
69 * map.put(20_000_000L, "v");
70 * map.put(20_000_001L, "w");
71 * map.put(20_000_002L, "x");
72 * map.put(20_000_003L, "y");
|