1 /*
2 * Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
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");
72 * map.put(20_000_004L, "z");
73 * }
|
1 /*
2 * Copyright (c) 2023, 2026, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
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 * Keys in {@code ReferencedKeyMap} must be
54 * {@linkplain java.util.Objects#hasIdentity identity objects}.
55 * <p>
56 * To create a {@code ReferencedKeyMap} the user must provide a {@link Supplier}
57 * of the backing map and whether {@link WeakReference} or
58 * {@link SoftReference} is to be used.
59 *
60 * {@snippet :
61 * // Use HashMap and WeakReference
62 * Map<Long, String> map = ReferencedKeyMap.create(false, HashMap::new);
63 * map.put(10_000_000L, "a");
64 * map.put(10_000_001L, "b");
65 * map.put(10_000_002L, "c");
66 * map.put(10_000_003L, "d");
67 * map.put(10_000_004L, "e");
68 *
69 * // Use ConcurrentHashMap and SoftReference
70 * map = ReferencedKeyMap.create(true, ConcurrentHashMap::new);
71 * map.put(20_000_000L, "v");
72 * map.put(20_000_001L, "w");
73 * map.put(20_000_002L, "x");
74 * map.put(20_000_003L, "y");
75 * map.put(20_000_004L, "z");
76 * }
|