annotate src/goodjava/util/WeakCacheMap.java @ 1752:b4fa42c1e999

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 25 Jan 2023 12:22:09 -0700
parents 1b809d2fdf03
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1498
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package goodjava.util;
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.lang.ref.ReferenceQueue;
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.lang.ref.WeakReference;
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 public class WeakCacheMap<K,V> extends CacheMap<K,V> {
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 static final class MyWeakReference<K,V> extends WeakReference<V> implements MyReference<K,V> {
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 private final K key;
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 MyWeakReference(K key,V value,ReferenceQueue<V> q) {
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 super(value,q);
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 this.key = key;
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 }
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 public K key() {
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 return key;
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 }
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 public boolean equals(Object obj) {
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 Object o = this.get();
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 if( o==null )
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 return false;
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 WeakReference ref = (WeakReference)obj;
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 return o.equals(ref.get());
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 }
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 }
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 protected MyReference<K,V> newReference(K key,V value,ReferenceQueue<V> q) {
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 return new MyWeakReference<K,V>(key,value,q);
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 }
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34
1b809d2fdf03 add CacheMap
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 }