125 private ReferencedKeyMap(boolean isSoft, Map<ReferenceKey<K>, V> map, ReferenceQueue<K> stale) {
126 this.isSoft = isSoft;
127 this.map = map;
128 this.stale = stale;
129 }
130
131 /**
132 * Create a new {@link ReferencedKeyMap} map.
133 *
134 * @param isSoft true if {@link SoftReference} keys are to
135 * be used, {@link WeakReference} otherwise.
136 * @param supplier {@link Supplier} of the backing map
137 *
138 * @return a new map with {@link Reference} keys
139 *
140 * @param <K> the type of keys maintained by the new map
141 * @param <V> the type of mapped values
142 */
143 public static <K, V> ReferencedKeyMap<K, V>
144 create(boolean isSoft, Supplier<Map<ReferenceKey<K>, V>> supplier) {
145 return create(isSoft, false, supplier);
146 }
147
148 /**
149 * Create a new {@link ReferencedKeyMap} map.
150 *
151 * @param isSoft true if {@link SoftReference} keys are to
152 * be used, {@link WeakReference} otherwise.
153 * @param useNativeQueue true if uses NativeReferenceQueue
154 * otherwise use {@link ReferenceQueue}.
155 * @param supplier {@link Supplier} of the backing map
156 *
157 * @return a new map with {@link Reference} keys
158 *
159 * @param <K> the type of keys maintained by the new map
160 * @param <V> the type of mapped values
161 */
162 public static <K, V> ReferencedKeyMap<K, V>
163 create(boolean isSoft, boolean useNativeQueue, Supplier<Map<ReferenceKey<K>, V>> supplier) {
164 return new ReferencedKeyMap<K, V>(isSoft, supplier.get(),
165 useNativeQueue ? SharedSecrets.getJavaLangRefAccess().newNativeReferenceQueue()
166 : new ReferenceQueue<>()
167 );
168 }
169
170 /**
171 * {@return a key suitable for a map entry}
172 *
173 * @param key unwrapped key
174 */
175 @SuppressWarnings("unchecked")
176 private ReferenceKey<K> entryKey(Object key) {
177 if (isSoft) {
178 return new SoftReferenceKey<>((K)key, stale);
179 } else {
180 return new WeakReferenceKey<>((K)key, stale);
181 }
182 }
183
184 /**
185 * {@return a key suitable for lookup}
186 *
187 * @param key unwrapped key
|
125 private ReferencedKeyMap(boolean isSoft, Map<ReferenceKey<K>, V> map, ReferenceQueue<K> stale) {
126 this.isSoft = isSoft;
127 this.map = map;
128 this.stale = stale;
129 }
130
131 /**
132 * Create a new {@link ReferencedKeyMap} map.
133 *
134 * @param isSoft true if {@link SoftReference} keys are to
135 * be used, {@link WeakReference} otherwise.
136 * @param supplier {@link Supplier} of the backing map
137 *
138 * @return a new map with {@link Reference} keys
139 *
140 * @param <K> the type of keys maintained by the new map
141 * @param <V> the type of mapped values
142 */
143 public static <K, V> ReferencedKeyMap<K, V>
144 create(boolean isSoft, Supplier<Map<ReferenceKey<K>, V>> supplier) {
145 return new ReferencedKeyMap<K, V>(isSoft, supplier.get(), new ReferenceQueue<>());
146 }
147
148 /**
149 * {@return a key suitable for a map entry}
150 *
151 * @param key unwrapped key
152 */
153 @SuppressWarnings("unchecked")
154 private ReferenceKey<K> entryKey(Object key) {
155 if (isSoft) {
156 return new SoftReferenceKey<>((K)key, stale);
157 } else {
158 return new WeakReferenceKey<>((K)key, stale);
159 }
160 }
161
162 /**
163 * {@return a key suitable for lookup}
164 *
165 * @param key unwrapped key
|