package com.jxml.quick.recycle;
import com.jxml.quick.*;
import java.util.*;
import java.lang.ref.*;

/**
 * A hash table with soft value references to QDoc objects.
 */
public final class QSoftDocHash
{
	protected HashMap hash=new HashMap();
	protected ReferenceQueue queue=new ReferenceQueue();

	public synchronized QDoc add(QDoc doc)
	{
		QDoc rv=get(doc.getKey());
		if (rv!=null)
			return rv;
		hash.put(doc.getKey(),new KR(doc,queue));
		return doc;
	}

	public synchronized QDoc get(String key)
	{
		poll();
		KR kr=(KR)hash.get(key);
		if (kr==null)
			return null;
		return (QDoc)kr.get();
	}

	void poll()
	{
		KR kr=null;
		do
		{
			if (kr!=null)
			{
				hash.remove(kr.key);
			}
		} while (kr!=null);
	}

	static class KR extends SoftReference
	{
		String key;

		KR(QDoc doc, ReferenceQueue queue)
		{
			super(doc,queue);
			key=doc.getKey();
		}
	}
}
