• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023, 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
23  * questions.
24  */
25 
26 package jdk.internal.util;
27 
28 import java.lang.ref.ReferenceQueue;
29 import java.lang.ref.WeakReference;
30 import java.util.Objects;
31 
32 /**
33  * {@link WeakReference} wrapper key for entries in the backing map.
34  *
35  * @param <T> key type
36  *
37  * @since 21
38  */
39 final class WeakReferenceKey<T> extends WeakReference<T> implements ReferenceKey<T> {
40     /**
41      * Saved hashcode of the key. Used when {@link WeakReference} is
42      * null.
43      */
44     private final int hashcode;
45 
46     /**
47      * Package-Protected constructor.
48      *
49      * @param key   unwrapped key value
50      * @param queue reference queue
51      */
WeakReferenceKey(T key, ReferenceQueue<T> queue)52     WeakReferenceKey(T key, ReferenceQueue<T> queue) {
53         super(key, queue);
54         this.hashcode = Objects.hashCode(key);
55     }
56 
57     /**
58      * Cleanup unused key. No need to enqueue since the key did not make it
59      * into the map.
60      */
61     @Override
unused()62     public void unused() {
63         clear();
64     }
65 
66     @Override
equals(Object obj)67     public boolean equals(Object obj) {
68         // Necessary when removing a null reference
69         if (obj == this) {
70             return true;
71         }
72         // Necessary when comparing an unwrapped key
73         if (obj instanceof ReferenceKey<?> key) {
74             obj = key.get();
75         }
76         // Note: refersTo is insufficient since keys require equivalence.
77         // refersTo would also require a cast to type T.
78         return Objects.equals(get(), obj);
79     }
80 
81     @Override
hashCode()82     public int hashCode() {
83         // Use saved hashcode
84         return hashcode;
85     }
86 
87     @Override
toString()88     public String toString() {
89         return this.getClass().getCanonicalName() + "#" + System.identityHashCode(this);
90     }
91 }
92