diff --git a/src/java/com/samskivert/util/SoftCache.java b/src/java/com/samskivert/util/SoftCache.java new file mode 100644 index 00000000..f63f0595 --- /dev/null +++ b/src/java/com/samskivert/util/SoftCache.java @@ -0,0 +1,99 @@ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2001-2006 Michael Bayne +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.util; + +import java.lang.ref.SoftReference; +import java.util.HashMap; +import java.util.Map; + +/** + * Implements a {@link SoftReference} cache wherein the values in the hashmap + * are not prevented from being garbage collected. + */ +public class SoftCache +{ + public SoftCache (int initialCapacity, float loadFactor) + { + _map = new HashMap>(initialCapacity, loadFactor); + } + + public SoftCache (int initialCapacity) + { + _map = new HashMap>(initialCapacity); + } + + public SoftCache () + { + _map = new HashMap>(); + } + + /** + * Returns true if the supplied key exists in this map and is mapped to an + * active value. + */ + public boolean containsKey (K key) + { + return (get(key) != null); + } + + /** + * Looks up and returns the value associated with the supplied key. + */ + public V get (K key) + { + V value = null; + SoftReference ref = _map.get(key); + if (ref != null) { + value = ref.get(); + if (value == null) { + _map.remove(key); + } + } + return value; + } + + /** + * Maps the specified key to the specified value. + */ + public V put (K key, V value) + { + SoftReference old = _map.put(key, new SoftReference(value)); + return (old == null) ? null : old.get(); + } + + /** + * Removes the specified key from the map. Returns the value to which the + * key was previously mapped or null. + */ + public V remove (K key) + { + SoftReference ref = _map.remove(key); + return (ref == null) ? null : ref.get(); + } + + /** + * Clears all mappings. + */ + public void clear () + { + _map.clear(); + } + + protected HashMap> _map; +} diff --git a/src/java/com/samskivert/util/SoftCacheMap.java b/src/java/com/samskivert/util/SoftCacheMap.java deleted file mode 100644 index 743f3c53..00000000 --- a/src/java/com/samskivert/util/SoftCacheMap.java +++ /dev/null @@ -1,169 +0,0 @@ -// -// samskivert library - useful routines for java programs -// Copyright (C) 2001-2006 Michael Bayne -// -// This library is free software; you can redistribute it and/or modify it -// under the terms of the GNU Lesser General Public License as published -// by the Free Software Foundation; either version 2.1 of the License, or -// (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -package com.samskivert.util; - -import java.lang.ref.SoftReference; -import java.util.AbstractMap; -import java.util.AbstractSet; -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; - -/** - * Implements a {@link SoftReference} cache wherein the values in the hashmap - * are not prevented from being garbage collected. - * - *

Beware! if you iterate over the contents of this map, some - * values may be null. Also note that if you try to store null values in this - * map, those entries will be pruned because the map cannot distinguish between - * an expired soft reference and a null value. Why would you cache null anyway? - */ -public class SoftCacheMap extends AbstractMap -{ - public SoftCacheMap (int initialCapacity, float loadFactor) - { - _map = new HashMap>(initialCapacity, loadFactor); - } - - public SoftCacheMap (int initialCapacity) - { - _map = new HashMap>(initialCapacity); - } - - public SoftCacheMap () - { - _map = new HashMap>(); - } - - // from interface Map - public int size () - { - return _map.size(); - } - - // from interface Map - public boolean containsKey (Object key) - { - return (get(key) != null); - } - - // from interface Map - public V get (Object key) - { - V value = null; - SoftReference ref = _map.get(key); - if (ref != null) { - value = ref.get(); - if (value == null) { - _map.remove(key); - } - } - return value; - } - - // from interface Map - public V put (K key, V value) - { - SoftReference old = _map.put(key, new SoftReference(value)); - return (old == null) ? null : old.get(); - } - - // from interface Map - public V remove (Object key) - { - SoftReference ref = _map.remove(key); - return (ref == null) ? null : ref.get(); - } - - // from interface Map - public void clear () - { - _map.clear(); - } - - // from interface Map - public Set keySet () - { - return _map.keySet(); - } - - // from interface Map - public Set> entrySet () - { - return new EntrySet(); - } - - /** - * Used by {@link EntrySet}. - */ - protected Entry getEntry (Object key) - { - SoftReference value = _map.get(key); - @SuppressWarnings("unchecked") K ckey = (K)key; - return (value == null) ? null : new MapEntry(ckey, value.get()); - } - - protected class EntrySet extends AbstractSet> { - public Iterator> iterator () { - final Iterator>> iter = - _map.entrySet().iterator(); - return new Iterator>() { - public boolean hasNext () { - return iter.hasNext(); - } - public Entry next () { - Entry> ent = iter.next(); - return (ent == null) ? null : - new MapEntry(ent.getKey(), ent.getValue().get()); - } - public void remove () { - iter.remove(); - } - }; - } - - public boolean contains (Object o) { - if (!(o instanceof Entry)) { - return false; - } - @SuppressWarnings("unchecked") Entry entry = (Entry)o; - return containsKey(entry.getKey()); - } - - public boolean remove (Object o) { - if (!(o instanceof Entry)) { - return false; - } - @SuppressWarnings("unchecked") Entry entry = (Entry)o; - return remove(entry.getKey()); - } - - public int size () { - return _map.size(); - } - - public void clear () { - _map.clear(); - } - } - - protected HashMap> _map; -}