* Introduce createTable() method in CacheMap to make internal table implementation

overrideable for subclasses.
* Add new WeakCacheMap class that uses java.util.WeakHashMap internally.
This commit is contained in:
hns 2006-04-03 13:27:04 +00:00
parent f759dbfa91
commit a90c2e6615
2 changed files with 69 additions and 9 deletions

View file

@ -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);
}
}
@ -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);
}
}

View file

@ -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);
}
}