view src/goodjava/util/WeakCacheMap.java @ 1528:3bd4d7963456

use goodjava/lucene/api
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 26 Jul 2020 23:11:53 -0600
parents 1b809d2fdf03
children
line wrap: on
line source

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);
	}

}