commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Juozas Baliuka <>
Subject abaut SoftRefHashMap implementation
Date Fri, 07 Dec 2001 13:33:00 GMT
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){


             this.key = key;


private void purge() {



         }catch(java.lang.NullPointerException npe){


private Map getMap() {

         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:   <>
For additional commands, e-mail: <>

View raw message