You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Juozas Baliuka <ba...@mwm.lt> on 2001/12/07 14:33:00 UTC
abaut SoftRefHashMap implementation
Hi,
I wrote two days ago, but I thik it was poor described.
I think good implementation for SoftHashMap can be useful, but not current
implementation :
1) SoftRefHashMap has public method purge(), it iterates all values and
searches removed references.
this method must be private , because application don't know then to know
then to call it, If it knows, this Map
don't need java.lang.ref.*. SoftHashMap must call this method it self.
Purge must be implemented using java.lang.ref.ReferenceQueue to detect
collected referants.
2) Implementation on Soft values is not useful, must be implemented on keys
(if you don't like standard Weak Map ). it can be useful on values, if
you hold strong references on keys,
but if hold strong references on keys may be you don't need a Map( use
referense as class's field),
or clone keys.
Class for keys final and not cloneable or you not sure hold reference on
key or not ?
It is possible, you have a bug in your application's architecture .
3) SoftRefHashMap must be final, factory method may be good idea, if you
implementing map with refences
on keys. if you implementing this on values, both purge and factory method
must be abstract.
It is may modified SoftRefHashMap code with reference values, it is better,
but not good ( Use WeakHasmap !!! ):
public final class SoftRefHashMap implements Map {
java.lang.ref.ReferenceQueue queue = new java.lang.ref.ReferenceQueue();
static class SoftValueRef extends java.lang.ref.SoftReference{
private Object key;
public SoftValueRef(Object referent,Object key, ReferenceQueue q){
super(referent,q);
this.key = key;
}
}
private void purge() {
try{
while(true)
hashMap.remove(((SoftValueRef)queue.poll()).key);
}catch(java.lang.NullPointerException npe){
}
}
private Map getMap() {
purge();
return hashMap;
}
public Object put( final Object key, final Object value ) {
Object answer = getMap().put( key, new
SoftValueRef(value,key,queue) );
if ( answer != null )
return ((Reference) answer).get();
return null;
}
----------------------------------------------------------------
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>