comparison src/goodjava/util/WeakCacheMap.java @ 1498:1b809d2fdf03

add CacheMap
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 08 May 2020 18:07:14 -0600
parents
children
comparison
equal deleted inserted replaced
1497:f04bfbb08721 1498:1b809d2fdf03
1 package goodjava.util;
2
3 import java.lang.ref.ReferenceQueue;
4 import java.lang.ref.WeakReference;
5
6
7 public class WeakCacheMap<K,V> extends CacheMap<K,V> {
8
9 static final class MyWeakReference<K,V> extends WeakReference<V> implements MyReference<K,V> {
10 private final K key;
11
12 MyWeakReference(K key,V value,ReferenceQueue<V> q) {
13 super(value,q);
14 this.key = key;
15 }
16
17 public K key() {
18 return key;
19 }
20
21 public boolean equals(Object obj) {
22 Object o = this.get();
23 if( o==null )
24 return false;
25 WeakReference ref = (WeakReference)obj;
26 return o.equals(ref.get());
27 }
28
29 }
30
31 protected MyReference<K,V> newReference(K key,V value,ReferenceQueue<V> q) {
32 return new MyWeakReference<K,V>(key,value,q);
33 }
34
35 }