diff --git a/src/helma/util/CacheMap.java b/src/helma/util/CacheMap.java index 20d8219d..a701b5e4 100644 --- a/src/helma/util/CacheMap.java +++ b/src/helma/util/CacheMap.java @@ -29,6 +29,7 @@ // Moved to helma.util to use java.util.HashMap instead of java.util.Hashtable package helma.util; +import java.util.Map; import java.util.HashMap; import java.util.Properties; @@ -59,8 +60,8 @@ public class CacheMap implements ObjectCache { private int eachCapacity; // The tables. - private HashMap oldTable; - private HashMap newTable; + private Map oldTable; + private Map newTable; // the application to output messages to private Application app = null; @@ -100,9 +101,10 @@ public class CacheMap implements ObjectCache { // in other words, make sure our threshold for table rotation is lower // than that of the underlying HashMap for table rehashing. eachCapacity = (int) (threshold / loadFactor) + 2; - // create tables + // create tables - we'll never insert into the initial oldTable, + // it's a dummy that will be lost on the first cache rotation. oldTable = new HashMap(); - newTable = new HashMap(eachCapacity, loadFactor); + newTable = createTable(eachCapacity, loadFactor); } /// Constructs a new, empty hashtable with the specified initial @@ -143,7 +145,7 @@ public class CacheMap implements ObjectCache { // if newtable is larger than threshold, rotate. if (newTable.size() > threshold) { oldTable = newTable; - newTable = new HashMap(eachCapacity, loadFactor); + newTable = createTable(eachCapacity, loadFactor); } } @@ -184,7 +186,7 @@ public class CacheMap implements ObjectCache { return false; } - /// Returns the number of keys in object array keys that + /// Returns the number of keys in object array keys that // were not found in the Map. // Those keys that are contained in the Map are nulled out in the array. // @param keys an array of key objects we are looking for @@ -229,10 +231,10 @@ public class CacheMap implements ObjectCache { /// Puts the specified element into the hashtable, using the specified // key. The element may be retrieved by doing a get() with the same key. - // The key and the element cannot be null. + // The key and the element cannot be null. // @param key the specified key in the hashtable // @param value the specified element - // @exception NullPointerException If the value of the element + // @exception NullPointerException If the value of the element // is equal to null. // @see LruHashtable#get // @return the old value of the key, or null if it did not have one. @@ -252,7 +254,7 @@ public class CacheMap implements ObjectCache { app.logEvent("Rotating Cache tables at " + newTable.size() + "/" + oldTable.size() + " (new/old)"); oldTable = newTable; - newTable = new HashMap(eachCapacity, loadFactor); + newTable = createTable(eachCapacity, loadFactor); } return oldValue; } @@ -307,6 +309,18 @@ public class CacheMap implements ObjectCache { return newTable.toString() + oldTable.toString() + hashCode(); } + /** + * Override this method to use custom Map implementations. The + * default implementation returns a java.util.HashMap instance. + * + * @param capacity the initial capacity + * @param loadFactor the load factor + * @return a new Map used for internal caching + */ + protected Map createTable(int capacity, float loadFactor) { + return new HashMap(capacity, loadFactor); + } + } diff --git a/src/helma/util/WeakCacheMap.java b/src/helma/util/WeakCacheMap.java new file mode 100644 index 00000000..fd3c635f --- /dev/null +++ b/src/helma/util/WeakCacheMap.java @@ -0,0 +1,46 @@ +/* + * Helma License Notice + * + * The contents of this file are subject to the Helma License + * Version 2.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://adele.helma.org/download/helma/license.txt + * + * Copyright 1998-2003 Helma Software. All Rights Reserved. + * + * $RCSfile$ + * $Author$ + * $Revision$ + * $Date$ + */ + +package helma.util; + +import java.util.Map; +import java.util.WeakHashMap; + +/** + * A CacheMap subclass that uses WeakHashMaps internally for its + * rotating tables. + */ +public class WeakCacheMap extends CacheMap { + + public WeakCacheMap(int capacity) { + super(capacity); + } + + public WeakCacheMap(int capacity, float loadFactor) { + super(capacity, loadFactor); + } + + /** + * Overridden to return a java.util.WeakHashMap instance. + * + * @param capacity the initial capacity + * @param loadFactor the load factor + * @return a new Map used for internal caching + */ + protected Map createTable(int capacity, float loadFactor) { + return new WeakHashMap(capacity, loadFactor); + } +}