diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/util/WeakCacheMap.java	Fri May 08 18:07:14 2020 -0600
@@ -0,0 +1,35 @@
+package goodjava.util;
+
+import java.lang.ref.ReferenceQueue;
+import java.lang.ref.WeakReference;
+
+
+public class WeakCacheMap<K,V> extends CacheMap<K,V> {
+
+	static final class MyWeakReference<K,V> extends WeakReference<V> implements MyReference<K,V> {
+		private final K key;
+
+		MyWeakReference(K key,V value,ReferenceQueue<V> q) {
+			super(value,q);
+			this.key = key;
+		}
+
+		public K key() {
+			return key;
+		}
+
+		public boolean equals(Object obj) {
+			Object o = this.get();
+			if( o==null )
+				return false;
+			WeakReference ref = (WeakReference)obj;
+			return o.equals(ref.get());
+		}
+
+	}
+
+	protected MyReference<K,V> newReference(K key,V value,ReferenceQueue<V> q) {
+		return new MyWeakReference<K,V>(key,value,q);
+	}
+
+}