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 class
SoftHashMap.SoftValue
We 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 int
HARD_REF_SIZE
The number of "hard" references to hold internally.private java.util.LinkedList
hardRefCache
The FIFO list of hard references, order of last access.private java.util.Map
hash
The internal HashMap that will hold the SoftReference.private java.lang.ref.ReferenceQueue
queue
Reference 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 HashMapvoid
clear()
java.util.Set
entrySet()
java.lang.Object
get(java.lang.Object key)
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.java.lang.Object
remove(java.lang.Object key)
int
size()
-
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:
get
in interfacejava.util.Map
- Overrides:
get
in 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:
put
in interfacejava.util.Map
- Overrides:
put
in classjava.util.AbstractMap
-
remove
public java.lang.Object remove(java.lang.Object key)
- Specified by:
remove
in interfacejava.util.Map
- Overrides:
remove
in classjava.util.AbstractMap
-
clear
public void clear()
- Specified by:
clear
in interfacejava.util.Map
- Overrides:
clear
in classjava.util.AbstractMap
-
size
public int size()
- Specified by:
size
in interfacejava.util.Map
- Overrides:
size
in classjava.util.AbstractMap
-
entrySet
public java.util.Set entrySet()
- Specified by:
entrySet
in interfacejava.util.Map
- Specified by:
entrySet
in classjava.util.AbstractMap
-
-