Class SoftHashMap
- java.lang.Object
-
- java.util.AbstractMap
-
- org.onemind.commons.java.lang.ref.SoftHashMap
-
- All Implemented Interfaces:
java.util.Map
public class SoftHashMap extends java.util.AbstractMap- Version:
- $Id: SoftHashMap.java,v 1.1 2004/10/23 15:24:35 thlee Exp $ $Name: $ Credits: Article by Heinz Kabutz at http://archive.devx.com/java/free/articles/Kabutz01/Kabutz01-1.asp
- Author:
- TiongHiang Lee (thlee@onemindsoft.org)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classSoftHashMap.SoftValueWe define our own subclass of SoftReference which contains not only the value but also the key to make it easier to find the entry in the HashMap after it's been garbage collected.
-
Field Summary
Fields Modifier and Type Field Description private intHARD_REF_SIZEThe number of "hard" references to hold internally.private java.util.LinkedListhardRefCacheThe FIFO list of hard references, order of last access.private java.util.MaphashThe internal HashMap that will hold the SoftReference.private java.lang.ref.ReferenceQueuequeueReference queue for cleared SoftReference objects.
-
Constructor Summary
Constructors Constructor Description SoftHashMap()ConstructorSoftHashMap(int hardSize)Constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void_cleanCollectedValues()Go through the ReferenceQueue and remove garbage collected SoftValue objects from the HashMapvoidclear()java.util.SetentrySet()java.lang.Objectget(java.lang.Object key)java.lang.Objectput(java.lang.Object key, java.lang.Object value)Here we put the key, value pair into the HashMap using a SoftValue object.java.lang.Objectremove(java.lang.Object key)intsize()-
Methods inherited from class java.util.AbstractMap
clone, containsKey, containsValue, equals, hashCode, isEmpty, keySet, putAll, toString, values
-
-
-
-
Field Detail
-
hash
private final java.util.Map hash
The internal HashMap that will hold the SoftReference.
-
HARD_REF_SIZE
private final int HARD_REF_SIZE
The number of "hard" references to hold internally.
-
hardRefCache
private final java.util.LinkedList hardRefCache
The FIFO list of hard references, order of last access.
-
queue
private final java.lang.ref.ReferenceQueue queue
Reference queue for cleared SoftReference objects.
-
-
Method Detail
-
get
public java.lang.Object get(java.lang.Object key)
- Specified by:
getin interfacejava.util.Map- Overrides:
getin classjava.util.AbstractMap
-
_cleanCollectedValues
private void _cleanCollectedValues()
Go through the ReferenceQueue and remove garbage collected SoftValue objects from the HashMap
-
put
public java.lang.Object put(java.lang.Object key, java.lang.Object value)Here we put the key, value pair into the HashMap using a SoftValue object.- Specified by:
putin interfacejava.util.Map- Overrides:
putin classjava.util.AbstractMap
-
remove
public java.lang.Object remove(java.lang.Object key)
- Specified by:
removein interfacejava.util.Map- Overrides:
removein classjava.util.AbstractMap
-
clear
public void clear()
- Specified by:
clearin interfacejava.util.Map- Overrides:
clearin classjava.util.AbstractMap
-
size
public int size()
- Specified by:
sizein interfacejava.util.Map- Overrides:
sizein classjava.util.AbstractMap
-
entrySet
public java.util.Set entrySet()
- Specified by:
entrySetin interfacejava.util.Map- Specified by:
entrySetin classjava.util.AbstractMap
-
-