Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: OT SoftRefHashtable uses SoftReferences



what's the difference between your class and WeakHashMap class?

On Apr 11, 2005, at 1:25 PM, Nicholas R. Rinard wrote:

I recently implemented a Hashtable which uses SoftReferences instead of regular references. I thought this code might be useful to someone else, since it has helped me be a little more gentle with my memory footprint. I also though I might slyly benefit if someone notices a bug that I overlooked. This code is simple, almost trivial, so it can be public domain, but it is also useful, so especially if you notice a bug please let me know.




package edu.dartmouth.lex.util;

import java.lang.ref.SoftReference;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

/**
* A Hashtable which stores all its values as soft references. Objects should be put
* in and gotten out as objects, not as soft references: use it like a normal Hashtable.
* SoftRefHashtable will return null if you ask for an object which has been gc'd.
*
* @author Nicholas R. Rinard
* @version 1.0
*/
public class SoftRefHashtable extends Hashtable {


    public SoftRefHashtable() {
    }

    public Object get( Object key ) {
        SoftReference sr = (SoftReference) super.get( key );
        if( sr == null ) {
            return null;
        } else {
            Object ret = sr.get();

            if( ret == null ) {
                remove( key );
            }
            return ret;
        }
    }

/**
* If the value parameter is null, the put operation is skipped and null is returned.
*/
public Object put( Object key, Object value ) {
if( value != null ) {
return super.put( key, new SoftReference( value ) );
}
return null; // another option would be to throw an IllegalArgumentException
}


    public int size() {
        int ret = 0;
        Enumeration e = keys();
        while( e.hasMoreElements() ) {
            Object o = get( e.nextElement() );
            if( o != null )
                ret ++;
        }
        return ret;
    }

    public Enumeration elements() {
        Vector v = new Vector();

        Enumeration e = super.elements();
        while( e.hasMoreElements() ) {
            Object o = ((SoftReference)e.nextElement()).get();
            v.addElement( o );
        }

        return v.elements();
    }

/**
* This N*log( N ) method clears the hashtable of keys and elements for which
* the element has been garbage collected. Returns the number of keys/values removed.
*/
public int purge() {
int keysRemoved = 0;
Enumeration keys = this.keys();
while( keys.hasMoreElements() ) { // N
Object key = keys.nextElement();
Object value = this.get( key ); // log( N )
if( value == null ) {
this.remove( key );
keysRemoved++;
}
}
return keysRemoved;
}


}

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden


Dmitry Markman

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden
References: 
 >OT SoftRefHashtable uses SoftReferences (From: "Nicholas R. Rinard" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.