I didn't change a whole besides some formating and getting rid of some redundant code (i felt it was redundant anyways).
-Nick
? src/main/org/exolab/castor/persist/cache/.nbattrs
Index: src/main/org/exolab/castor/persist/cache/CacheFactory.java
===================================================================
RCS file:
/cvs/castor/castor/src/main/org/exolab/castor/persist/cache/CacheFactory.java,v
retrieving revision 1.1
diff -u -r1.1 CacheFactory.java
--- a/src/main/org/exolab/castor/persist/cache/CacheFactory.java 13 Feb 2004
21:16:54 -0000 1.1
+++ b/src/main/org/exolab/castor/persist/cache/CacheFactory.java 5 Apr 2004
03:36:30 -0000
@@ -49,89 +49,85 @@
/**
* Factory for creating Cache instances.
- *
+ *
* @author <a href="werner DOT guttmann AT gmx DOT com">Werner Guttmann</a>
* @version $Revision: 1.1 $ $Date: 2004/02/13 21:16:54 $
*/
public class CacheFactory {
-
- /**
+
+ /**
* Default cache type identifier
*/
public final static String DEFAULT_TYPE_IDENTIFIER = "count-limited";
- /**
- * Default LRU mechanism
- */
- public final static CacheType DEFAULT_TYPE = CacheType.CACHE_COUNT_LIMITED;
- /**
- * Specify the default LRU parameter
- */
- public final static int DEFAULT_PARAM = 100;
-
- /**
- * The <a href="http://jakarta.apache.org/commons/logging/">Jakarta
- * Commons Logging</a> instance used for all logging.
- */
- private static Log _log = LogFactory.getFactory().getInstance(
CacheFactory.class );
-
-
- /**
- * Factory method to create a LRU map of specified type.
- *
- * @param type mechanism type
- * @param param capacity of the lru
- */
- public static Cache create( String type, int param )
- throws InvalidCacheTypeException
- {
-
- Cache cache = null;
- CacheType cacheType = null;
+ /**
+ * Default LRU mechanism
+ */
+ public final static CacheType DEFAULT_TYPE = CacheType.CACHE_COUNT_LIMITED;
+ /**
+ * Specify the default LRU parameter
+ */
+ public final static int DEFAULT_PARAM = 100;
+
+ /**
+ * The <a href="http://jakarta.apache.org/commons/logging/">Jakarta
+ * Commons Logging</a> instance used for all logging.
+ */
+ private static Log _log = LogFactory.getFactory().getInstance( CacheFactory.class
);
+
+
+ /**
+ * Factory method to create a LRU map of specified type.
+ *
+ * @param type mechanism type
+ * @param param capacity of the lru
+ */
+ public static Cache create( String type, int param )
+ throws InvalidCacheTypeException {
+
+ Cache cache = null;
+ CacheType cacheType = null;
if (type == null) {
- type = DEFAULT_TYPE_IDENTIFIER;
+ type = DEFAULT_TYPE_IDENTIFIER;
+ }
+
+ try {
+ cacheType = CacheType.create(type);
}
-
- try {
- cacheType = CacheType.create (type);
- }
- catch (InvalidCacheTypeException e) {
- _log.error ("Invalid cache type encountered", e);
+ catch (InvalidCacheTypeException e) {
+ _log.error("Invalid cache type encountered", e);
throw e;
- }
+ }
- if (cacheType == DEFAULT_TYPE) {
- param = DEFAULT_PARAM;
- }
-
- if (cacheType == CacheType.CACHE_COUNT_LIMITED) {
- if ( param > 0 )
- cache = new CountLimited( param );
- else
- cache = new NoCache();
- }
- else if (cacheType == CacheType.CACHE_TIME_LIMITED) {
- if ( param > 0 )
- cache = new TimeLimited( param );
- else
- cache = new NoCache();
- }
- else if (cacheType == CacheType.CACHE_UNLIMITED) {
- cache = new Unlimited();
- }
- else if (cacheType == CacheType.CACHE_NONE) {
- cache = new NoCache();
- }
-
- if (_log.isDebugEnabled()) {
- _log.debug ("Creating cache instance for type " +
cacheType.toString());
- }
+ if (cacheType == DEFAULT_TYPE) {
+ param = DEFAULT_PARAM;
+ }
+
+ if ((param <= 0 && cacheType != CacheType.CACHE_UNLIMITED) || cacheType ==
CacheType.CACHE_NONE){
+ cache = new NoCache(); //either param is set to low, or cacheType is
NONE. If cache type
+ //is UNLIMITED we dont care about the param.
+ }else if (cacheType == CacheType.CACHE_COUNT_LIMITED) {
+ cache = new CountLimited( param );
+ }
+ else if (cacheType == CacheType.CACHE_TIME_LIMITED) {
+ cache = new TimeLimited( param );
+ }
+ else if (cacheType == CacheType.CACHE_UNLIMITED) {
+ cache = new Unlimited();
+ }
+ else if (cacheType == CacheType.CACHE_NONE) {
+ cache = new NoCache();
+ }
+
+ if (_log.isDebugEnabled()) {
+ _log.debug("Creating cache instance for type " + cacheType.toString());
+ }
// setting cache type
cache.setCacheType(cacheType);
-
- return cache;
- }
-
+
+ return cache;
+ }
+
}
Index: src/main/org/exolab/castor/persist/cache/CountLimited.java
===================================================================
RCS file:
/cvs/castor/castor/src/main/org/exolab/castor/persist/cache/CountLimited.java,v
retrieving revision 1.1
diff -u -r1.1 CountLimited.java
--- a/src/main/org/exolab/castor/persist/cache/CountLimited.java 13 Feb 2004
21:19:44 -0000 1.1
+++ b/src/main/org/exolab/castor/persist/cache/CountLimited.java 5 Apr 2004
03:36:31 -0000
@@ -55,10 +55,10 @@
* CountLimited is a count limted least-recently-used <tt>Map</tt>.
* <p>
* Every object being put in the Map will live until the
- * map is full. If the map is full, a least-recently-used object
- * will be disposed.
+ * map is full. If the map is full, a least-recently-used object
+ * will be disposed.
* <p>
- * Method [EMAIL PROTECTED] #dispose(Object)} will be called whenever an
+ * Method [EMAIL PROTECTED] #dispose(Object)} will be called whenever an
* old object is diposed. Developer can get notify by overriding
* the dispose method [EMAIL PROTECTED] #dispose(Object)}.
*
@@ -66,233 +66,231 @@
* @author <a href="werner DOT guttmann AT gmx DOT com">Werner Guttmann</a>
* @version $Revision: 1.1 $ $Date: 2004/02/13 21:19:44 $
*/
-public class CountLimited
-extends AbstractBaseCache
-implements Cache
-{
-
- private final static int LRU_OLD = 0;
- private final static int LRU_NEW = 1;
-
- private Hashtable mapKeyPos;
- private Object[] keys;
- private Object[] values;
- private int[] status;
- private int cur;
- private int size;
-
- /**
- * The <a href="http://jakarta.apache.org/commons/logging/">Jakarta
- * Commons Logging</a> instance used for all logging.
- */
- private static Log _log =
LogFactory.getFactory().getInstance(CountLimited.class);
-
- public CountLimited( int size ) {
- keys = new Object[size];
- values = new Object[size];
- status = new int[size];
- mapKeyPos = new Hashtable(size);
- this.size = size;
-
- if (_log.isDebugEnabled()) {
- _log.trace ("Successfully created count-limited cache
instance" );
- }
- }
-
- /**
- * Maps the specified <code>key</code> to the specified
- * <code>value</code> in this Map. Neither the key nor the
- * value can be <code>null</code>.
- * <p>
- * The value can be retrieved by calling the <code>get</code> method
- * with a key that is equal to the original key, before it is diposed
- * when the Map is full.
- * <p>
- * @param key the Map key.
- * @param value the value.
- * @return the previous value of the specified key in this Map,
- * or <code>null</code> if it did not have one.
- * @exception NullPointerException if the key or value is
- * <code>null</code>.
- */
- public synchronized Object put( Object key, Object value ) {
-
- if (_log.isDebugEnabled()) {
- _log.trace ("Putting entry into cache for key " + key + " to "
+ value);
- }
-
- Object oldPos = mapKeyPos.get(key);
-
- if ( oldPos != null ) {
- int pos = ((Integer)oldPos).intValue();
- Object oldObject = values[pos];
- values[pos] = value;
- status[pos] = LRU_NEW;
-
-
- dispose( oldObject );
-
- return oldObject;
- } else {
-
- // skip to new pos -- for Cache, change walkStatus() to get
lock....
- while (walkStatus() != LRU_OLD) {
- // skip to new position
- }
-
- Object intvalue;// = null;
- if ( keys[cur] != null ) {
- intvalue = mapKeyPos.remove(keys[cur]);
- // if ( intvalue == null )
- // intvalue = new
Integer(cur);
- } else {
- intvalue = new Integer(cur);
- }
- Object oldObject = values[cur];
- keys[cur] = key;
- values[cur] = value;
- status[cur] = LRU_NEW;
- mapKeyPos.put(key, intvalue);
- cur++;
- if ( cur >= size ) cur = 0;
- if ( oldObject != null )
- dispose( oldObject );
- return oldObject;
- }
- }
-
- /**
- *Returns the value to which the specified key is mapped in this Map.
- [EMAIL PROTECTED] key - a key in the Map.
- [EMAIL PROTECTED] the value to which the key is mapped in this Map; null if
- * the key is not mapped to any value in this Map.
- */
- public synchronized Object get( Object key ) {
- Object intvalue = mapKeyPos.get(key);
- Object cachedObject = null;
- if ( intvalue != null ) {
- int pos = ((Integer)intvalue).intValue();
- status[pos] = LRU_NEW;
- cachedObject = values[pos];
- }
-
- if (_log.isDebugEnabled()) {
- _log.trace ("Returning cache entry for key " + key + ": " +
cachedObject);
- }
-
- return cachedObject;
- }
-
- /**
- * Removes the key (and its corresponding value) from this
- * Map. This method does nothing if the key is not in the Map.
- *
- * @param key the key that needs to be removed.
- * @return the value to which the key had been mapped in this Map,
- * or <code>null</code> if the key did not have a mapping.
- */
- public synchronized Object remove( Object key ) {
- Object intvalue = mapKeyPos.remove(key);
- Object removedObject = null;
- if ( intvalue != null ) {
- int pos = ((Integer)intvalue).intValue();
- removedObject = values[pos];
- keys[pos] = null;
- values[pos] = null;
- status[pos] = LRU_OLD;
- }
- if (_log.isDebugEnabled()) {
- _log.trace ("Removing cache entry for key " + key + ": " +
removedObject);
- }
-
- return removedObject;
- }
-
- /**
- * Returns an enumeration of the values in this LRU map.
- * Use the Enumeration methods on the returned object to fetch the elements
- * sequentially.
- *
- * @return an enumeration of the values in this Map.
- * @see java.util.Enumeration
- */
- public Enumeration elements() {
- return new ValuesEnumeration(values);
- }
-
-
- /**
- * Remove the object identified by key from the cache.
- *
- * @param key the key that needs to be removed.
- */
- public void expire(Object key)
- {
- if (_log.isDebugEnabled()) {
- _log.trace ("Expiring cache entry for key " + key);
- }
- if ( remove(key) == null ) {
- // log.trace ("CountLimited LRU expire: "+key+" not found");
- }
- else {
- // log.trace ("CountLimited LRU expire: "+key+" removed from
cache");
- }
- dispose(key);
- }
-
- /**
- * This method is called when an object is disposed.
- * Override this method if you interested in the disposed object.
- *
- * @param o - the disposed object
- */
- protected void dispose( Object o ) {
- if (_log.isDebugEnabled()) {
- _log.trace ("Disposing object " + o);
- }
- }
-
- private int walkStatus() {
- int s = status[cur];
- if ( s == LRU_NEW ) {
- status[cur] = LRU_OLD;
- cur++;
- if ( cur >= size ) cur = 0;
- return LRU_NEW;
- } else {
- return LRU_OLD;
- }
- }
-
- private class ValuesEnumeration implements Enumeration {
- private int cur;
- private Object[] values;
- private ValuesEnumeration( Object[] v ) {
- Vector t = new Vector(v.length);
- for ( int i=0; i<v.length; i++ ) {
- if ( v[i] != null ) {
- t.add(v[i]);
- }
- }
- values = t.toArray();
- }
- public boolean hasMoreElements() {
- if ( values != null && values.length > cur )
- return true;
- return false;
- }
- public Object nextElement() throws NoSuchElementException {
- if ( values == null || values.length <= cur )
- throw new NoSuchElementException();
- return values[cur++];
- }
- }
-
- /* Indicates whether the cache holds a valuze object for the specified key.
- * @see org.exolab.castor.persist.cache.Cache#contains(java.lang.Object)
- */
- public boolean contains(Object key) {
- return (this.get(key) != null);
- }
-
+public class CountLimited
+extends AbstractBaseCache {
+
+ private final static int LRU_OLD = 0;
+ private final static int LRU_NEW = 1;
+
+ private Hashtable mapKeyPos;
+ private Object[] keys;
+ private Object[] values;
+ private int[] status;
+ private int cur;
+ private int size;
+
+ /**
+ * The <a href="http://jakarta.apache.org/commons/logging/">Jakarta
+ * Commons Logging</a> instance used for all logging.
+ */
+ private static Log _log = LogFactory.getFactory().getInstance(CountLimited.class);
+
+ public CountLimited( int size ) {
+ keys = new Object[size];
+ values = new Object[size];
+ status = new int[size];
+ mapKeyPos = new Hashtable(size);
+ this.size = size;
+
+ if (_log.isDebugEnabled()) {
+ _log.trace("Successfully created count-limited cache instance" );
+ }
+ }
+
+ /**
+ * Maps the specified <code>key</code> to the specified
+ * <code>value</code> in this Map. Neither the key nor the
+ * value can be <code>null</code>.
+ * <p>
+ * The value can be retrieved by calling the <code>get</code> method
+ * with a key that is equal to the original key, before it is diposed
+ * when the Map is full.
+ * <p>
+ * @param key the Map key.
+ * @param value the value.
+ * @return the previous value of the specified key in this Map,
+ * or <code>null</code> if it did not have one.
+ * @exception NullPointerException if the key or value is
+ * <code>null</code>.
+ */
+ public synchronized Object put( Object key, Object value ) {
+
+ if (_log.isDebugEnabled()) {
+ _log.trace("Putting entry into cache for key " + key + " to " + value);
+ }
+
+ Object oldPos = mapKeyPos.get(key);
+
+ if ( oldPos != null ) {
+ int pos = ((Integer)oldPos).intValue();
+ Object oldObject = values[pos];
+ values[pos] = value;
+ status[pos] = LRU_NEW;
+
+ dispose( oldObject );
+
+ return oldObject;
+ } else {
+
+ //set the current current position in the cache
+ getLock();
+
+ Object intvalue;
+ if ( keys[cur] != null ) {
+ intvalue = mapKeyPos.remove(keys[cur]);
+ } else {
+ intvalue = new Integer(cur);
+ }
+ Object oldObject = values[cur];
+ keys[cur] = key;
+ values[cur] = value;
+ status[cur] = LRU_NEW;
+ mapKeyPos.put(key, intvalue);
+ cur++;
+ if ( cur >= size ){
+ cur = 0;
+ }
+ if ( oldObject != null ){
+ dispose( oldObject );
+ }
+ return oldObject;
+ }
+ }
+
+ /**
+ *Returns the value to which the specified key is mapped in this Map.
+ [EMAIL PROTECTED] key - a key in the Map.
+ [EMAIL PROTECTED] the value to which the key is mapped in this Map; null if
+ * the key is not mapped to any value in this Map.
+ */
+ public synchronized Object get( Object key ) {
+ Object intvalue = mapKeyPos.get(key);
+ Object cachedObject = null;
+ if ( intvalue != null ) {
+ int pos = ((Integer)intvalue).intValue();
+ status[pos] = LRU_NEW;
+ cachedObject = values[pos];
+ }
+
+ if (_log.isDebugEnabled()) {
+ _log.trace("Returning cache entry for key " + key + ": " + cachedObject);
+ }
+
+ return cachedObject;
+ }
+
+ /**
+ * Removes the key (and its corresponding value) from this
+ * Map. This method does nothing if the key is not in the Map.
+ *
+ * @param key the key that needs to be removed.
+ * @return the value to which the key had been mapped in this Map,
+ * or <code>null</code> if the key did not have a mapping.
+ */
+ public synchronized Object remove( Object key ) {
+ Object intvalue = mapKeyPos.remove(key);
+ Object removedObject = null;
+ if ( intvalue != null ) {
+ int pos = ((Integer)intvalue).intValue();
+ removedObject = values[pos];
+ keys[pos] = null;
+ values[pos] = null;
+ status[pos] = LRU_OLD;
+ }
+ if (_log.isDebugEnabled()) {
+ _log.trace("Removing cache entry for key " + key + ": " + removedObject);
+ }
+
+ return removedObject;
+ }
+
+ /**
+ * Returns an enumeration of the values in this LRU map.
+ * Use the Enumeration methods on the returned object to fetch the elements
+ * sequentially.
+ *
+ * @return an enumeration of the values in this Map.
+ * @see java.util.Enumeration
+ */
+ public Enumeration elements() {
+ return new ValuesEnumeration(values);
+ }
+
+
+ /**
+ * Remove the object identified by key from the cache.
+ *
+ * @param key the key that needs to be removed.
+ */
+ public void expire(Object key) {
+ if (_log.isDebugEnabled()) {
+ _log.trace("Expiring cache entry for key " + key);
+ }
+
+ remove(key);
+
+ dispose(key);
+ }
+
+ /**
+ * This method is called when an object is disposed.
+ * Override this method if you interested in the disposed object.
+ *
+ * @param o - the disposed object
+ */
+ protected void dispose( Object o ) {
+ if (_log.isDebugEnabled()) {
+ _log.trace("Disposing object " + o);
+ }
+ }
+
+ /**
+ * Goes through <code>status</code> and looks for any old entries.
+ *
+ */
+ private void getLock(){
+ int s = status[cur];
+ while(s != LRU_OLD){
+ if ( s == LRU_NEW ) {
+ status[cur] = LRU_OLD;
+ cur++;
+ if ( cur >= size ){
+ cur = 0;
+ }
+ }
+ s = status[cur];
+ }
+ }
+
+ private class ValuesEnumeration implements Enumeration {
+ private int cur;
+ private Object[] values;
+ private ValuesEnumeration( Object[] v ) {
+ Vector t = new Vector(v.length);
+ for ( int i=0; i<v.length; i++ ) {
+ if ( v[i] != null ) {
+ t.add(v[i]);
+ }
+ }
+ values = t.toArray();
+ }
+ public boolean hasMoreElements() {
+ if ( values != null && values.length > cur )
+ return true;
+ return false;
+ }
+ public Object nextElement() throws NoSuchElementException {
+ if ( values == null || values.length <= cur )
+ throw new NoSuchElementException();
+ return values[cur++];
+ }
+ }
+
+ /* Indicates whether the cache holds a valuze object for the specified key.
+ * @see org.exolab.castor.persist.cache.Cache#contains(java.lang.Object)
+ */
+ public boolean contains(Object key) {
+ return (this.get(key) != null);
+ }
+
}
\ No newline at end of file
Index: src/main/org/exolab/castor/persist/cache/NoCache.java
===================================================================
RCS file: /cvs/castor/castor/src/main/org/exolab/castor/persist/cache/NoCache.java,v
retrieving revision 1.1
diff -u -r1.1 NoCache.java
--- a/src/main/org/exolab/castor/persist/cache/NoCache.java 13 Feb 2004 21:20:06
-0000 1.1
+++ b/src/main/org/exolab/castor/persist/cache/NoCache.java 5 Apr 2004 03:36:31
-0000
@@ -54,131 +54,129 @@
/**
* NoCache is a Map which dispose all object right the way.
* <p>
- * Every object being put in the Map will be disposed.
+ * Every object being put in the Map will be disposed.
* <p>
- * Method [EMAIL PROTECTED] #dispose(Object)} will be called whenever an
+ * Method [EMAIL PROTECTED] #dispose(Object)} will be called whenever an
* old object is diposed. Developer can get notify by overriding
* the dispose method [EMAIL PROTECTED] #dispose(Object)}.
*
* @author <a href="werner DOT guttmann AT gmx DOT com">Werner Guttmann</a>
* @version $Revision: 1.1 $ $Date: 2004/02/13 21:20:06 $
*/
-public class NoCache
-extends AbstractBaseCache
-implements Cache
-{
-
- /**
- * The <a href="http://jakarta.apache.org/commons/logging/">Jakarta
- * Commons Logging</a> instance used for all logging.
- */
- private static Log _log = LogFactory.getFactory().getInstance(NoCache.class);
-
- /**
- * Maps the specified <code>key</code> to the specified
- * <code>value</code> in this Map. Neither the key nor the
- * value can be <code>null</code>. End of theory.
- * <p>
- * Every object being put in the Map will be disposed.
- * <p>
- * @param key the Map key.
- * @param value the value.
- * @return the previous value of the specified key in this Map,
- * or <code>null</code> if it did not have one.
- * @exception NullPointerException if the key or value is
- * <code>null</code>.
- */
- public synchronized Object put( Object key, Object value ) {
- if (_log.isDebugEnabled()) {
- _log.trace ("Creating cache entry for key " + key + " with
value " + value);
- }
- dispose( value );
- return null;
- }
-
- /**
- *Returns the value to which the specified key is mapped in this Map.
- [EMAIL PROTECTED] key - a key in the Map.
- [EMAIL PROTECTED] the value to which the key is mapped in this Map; null if
- * the key is not mapped to any value in this Map.
- */
- public synchronized Object get( Object key ) {
- if (_log.isDebugEnabled()) {
- _log.trace ("Getting cache entry for key " + key);
- }
- return null;
- }
-
- /**
- * Removes the key (and its corresponding value) from this
- * Map. This method does nothing if the key is not in the Map.
- *
- * @param key the key that needs to be removed.
- * @return the value to which the key had been mapped in this Map,
- * or <code>null</code> if the key did not have a mapping.
- */
- public synchronized Object remove( Object key ) {
- if (_log.isDebugEnabled()) {
- _log.trace ("Removing cache entry for key " + key);
- }
- return null;
- }
-
- /**
- * Returns an enumeration of the values in this LRU map.
- * Use the Enumeration methods on the returned object to fetch the elements
- * sequentially.
- *
- * @return an enumeration of the values in this Map.
- * @see java.util.Enumeration
- */
- public Enumeration elements() {
- return new EmptyEnumeration();
- }
-
- /**
- * Remove the object identified by key from the cache.
- *
- * @param key the key that needs to be removed.
- */
- public void expire(Object key) {
- if (_log.isDebugEnabled()) {
- _log.trace ("Expiring cache entry for key " + key);
- }
- dispose(key);
- }
-
- /**
- * This method is called when an object is disposed.
- * Override this method if you interested in the disposed object.
- */
- protected void dispose( Object o ) {
- if (_log.isDebugEnabled()) {
- _log.trace ("Disposing object " + o);
- }
- }
-
- private class EmptyEnumeration implements Enumeration {
-
- /**
- * Creates an instance of this class.
- */
- private EmptyEnumeration() {
+public class NoCache
+extends AbstractBaseCache {
+
+ /**
+ * The <a href="http://jakarta.apache.org/commons/logging/">Jakarta
+ * Commons Logging</a> instance used for all logging.
+ */
+ private static Log _log = LogFactory.getFactory().getInstance(NoCache.class);
+
+ /**
+ * Maps the specified <code>key</code> to the specified
+ * <code>value</code> in this Map. Neither the key nor the
+ * value can be <code>null</code>. End of theory.
+ * <p>
+ * Every object being put in the Map will be disposed.
+ * <p>
+ * @param key the Map key.
+ * @param value the value.
+ * @return the previous value of the specified key in this Map,
+ * or <code>null</code> if it did not have one.
+ * @exception NullPointerException if the key or value is
+ * <code>null</code>.
+ */
+ public synchronized Object put( Object key, Object value ) {
+ if (_log.isDebugEnabled()) {
+ _log.trace("Creating cache entry for key " + key + " with value " +
value);
+ }
+ dispose( value );
+ return null;
+ }
+
+ /**
+ *Returns the value to which the specified key is mapped in this Map.
+ [EMAIL PROTECTED] key - a key in the Map.
+ [EMAIL PROTECTED] the value to which the key is mapped in this Map; null if
+ * the key is not mapped to any value in this Map.
+ */
+ public synchronized Object get( Object key ) {
+ if (_log.isDebugEnabled()) {
+ _log.trace("Getting cache entry for key " + key);
+ }
+ return null;
+ }
+
+ /**
+ * Removes the key (and its corresponding value) from this
+ * Map. This method does nothing if the key is not in the Map.
+ *
+ * @param key the key that needs to be removed.
+ * @return the value to which the key had been mapped in this Map,
+ * or <code>null</code> if the key did not have a mapping.
+ */
+ public synchronized Object remove( Object key ) {
+ if (_log.isDebugEnabled()) {
+ _log.trace("Removing cache entry for key " + key);
+ }
+ return null;
+ }
+
+ /**
+ * Returns an enumeration of the values in this LRU map.
+ * Use the Enumeration methods on the returned object to fetch the elements
+ * sequentially.
+ *
+ * @return an enumeration of the values in this Map.
+ * @see java.util.Enumeration
+ */
+ public Enumeration elements() {
+ return new EmptyEnumeration();
+ }
+
+ /**
+ * Remove the object identified by key from the cache.
+ *
+ * @param key the key that needs to be removed.
+ */
+ public void expire(Object key) {
+ if (_log.isDebugEnabled()) {
+ _log.trace("Expiring cache entry for key " + key);
+ }
+ dispose(key);
+ }
+
+ /**
+ * This method is called when an object is disposed.
+ * Override this method if you interested in the disposed object.
+ */
+ protected void dispose( Object o ) {
+ if (_log.isDebugEnabled()) {
+ _log.trace("Disposing object " + o);
+ }
+ }
+
+ private class EmptyEnumeration implements Enumeration {
+
+ /**
+ * Creates an instance of this class.
+ */
+ private EmptyEnumeration() {
// creates ....
- }
- public boolean hasMoreElements() {
- return false;
- }
- public Object nextElement() throws NoSuchElementException {
- throw new NoSuchElementException();
- }
- }
-
- /* Indicates whether the cache holds a valuze object for the specified key.
- * @see org.exolab.castor.persist.cache.Cache#contains(java.lang.Object)
- */
- public boolean contains(Object key) {
- return false;
- }
-
+ }
+ public boolean hasMoreElements() {
+ return false;
+ }
+ public Object nextElement() throws NoSuchElementException {
+ throw new NoSuchElementException();
+ }
+ }
+
+ /* Indicates whether the cache holds a valuze object for the specified key.
+ * @see org.exolab.castor.persist.cache.Cache#contains(java.lang.Object)
+ */
+ public boolean contains(Object key) {
+ return false;
+ }
+
}
\ No newline at end of file
Index: src/main/org/exolab/castor/persist/cache/TimeLimited.java
===================================================================
RCS file:
/cvs/castor/castor/src/main/org/exolab/castor/persist/cache/TimeLimited.java,v
retrieving revision 1.2
diff -u -r1.2 TimeLimited.java
--- a/src/main/org/exolab/castor/persist/cache/TimeLimited.java 2 Apr 2004 05:31:59
-0000 1.2
+++ b/src/main/org/exolab/castor/persist/cache/TimeLimited.java 5 Apr 2004 03:36:31
-0000
@@ -57,9 +57,9 @@
* TimeLimited is a time limted least-recently-used <tt>Map</tt>.
* <p>
* Every object being put in the Map will live until the timeout
- * expired.
+ * expired.
* <p>
- * Method [EMAIL PROTECTED] #dispose(Object)} will be called whenever an
+ * Method [EMAIL PROTECTED] #dispose(Object)} will be called whenever an
* old object is diposed. Developer can get notify by overriding
* the dispose method [EMAIL PROTECTED] #dispose(Object)}.
*
@@ -67,10 +67,8 @@
* @author <a href="werner DOT guttmann AT gmx DOT com">Werner Guttmann</a>
* @version $Revision: 1.2 $ $Date: 2004/04/02 05:31:59 $
*/
-public class TimeLimited
-extends AbstractBaseCache
-implements Cache
-{
+public class TimeLimited
+extends AbstractBaseCache {
/**
* The <a href="http://jakarta.apache.org/commons/logging/">Jakarta
@@ -79,8 +77,8 @@
private static Log _log = LogFactory.getFactory().getInstance(TimeLimited.class);
/**
- * The Default precision in millisecond is 1000. Percision is the interval
- * between each time which the timer thread will wake up and trigger
+ * The Default precision in millisecond is 1000. Percision is the interval
+ * between each time which the timer thread will wake up and trigger
* clean up of least-recently-used Object.
*/
public final static int DEFAULT_PRECISION = 1000;
@@ -98,7 +96,7 @@
private Hashtable map;
/**
- * Creates an instance of TimeLimited.
+ * Creates an instance of TimeLimited.
*
* @param interval the number of ticks an object lives before it will be disposed.
*/
@@ -109,13 +107,13 @@
}
/**
- * Maps the specified <code>key</code> to the specified
- * <code>value</code> in this Map. Neither the key nor the
- * value can be <code>null</code>.
+ * Maps the specified <code>key</code> to the specified
+ * <code>value</code> in this Map. Neither the key nor the
+ * value can be <code>null</code>.
* <p>
- * The value can be retrieved by calling the <code>get</code> method
+ * The value can be retrieved by calling the <code>get</code> method
* with a key that is equal to the original key, before it is diposed
- * when the timeout of the entry is expired.
+ * when the timeout of the entry is expired.
* <p>
* @param key the Map key.
* @param value the value.
@@ -125,11 +123,12 @@
* <code>null</code>.
*/
public synchronized Object put(Object key, Object value) {
- QueueItem oldItem = (QueueItem) map.get(key);
+ QueueItem oldItem = (QueueItem) map.get(key);
if ( oldItem != null ) {
- Object oldObject = oldItem.item;
- oldItem.item = value;
+ Object oldObject = oldItem.item;
remove(oldItem);
+
+ oldItem.item = value;
add(oldItem);
return oldObject;
} else {
@@ -143,19 +142,19 @@
/**
*Returns the value to which the specified key is mapped in this Map.
[EMAIL PROTECTED] key - a key in the Map.
- [EMAIL PROTECTED] the value to which the key is mapped in this Map; null if
+ [EMAIL PROTECTED] the value to which the key is mapped in this Map; null if
* the key is not mapped to any value in this Map.
*/
public synchronized Object get(Object key) {
Object o = map.get(key);
if ( o == null )
return null;
- else
+ else
return ((QueueItem)o).item;
}
/**
- * Removes the key (and its corresponding value) from this
+ * Removes the key (and its corresponding value) from this
* Map. This method does nothing if the key is not in the Map.
*
* @param key the key that needs to be removed.
@@ -167,12 +166,12 @@
QueueItem queueItem = (QueueItem) map.remove(key);
if (queueItem == null) {
if (_log.isDebugEnabled()) {
- _log.trace ("TimeLimitedLRU: not in cache ... remove(" + key + ")");
+ _log.trace("TimeLimitedLRU: not in cache ... remove(" + key + ")");
}
return null;
} else {
if (_log.isDebugEnabled()) {
- _log.trace ("TimeLimitedLRU: remove(" + key + ") = " + queueItem.item);
+ _log.trace("TimeLimitedLRU: remove(" + key + ") = " + queueItem.item);
}
remove(queueItem);
return queueItem.item;
@@ -209,7 +208,7 @@
*/
public boolean contains(Object key) {
if (_log.isDebugEnabled()) {
- _log.trace ("Testing for entry for key " + key);
+ _log.trace("Testing for entry for key " + key);
}
return (this.get(key) != null);
}
@@ -221,14 +220,14 @@
* @param o - the disposed object
*/
protected void dispose( Object o ) {
- if (_log.isDebugEnabled())
+ if (_log.isDebugEnabled())
_log.trace("Disposing " + o);
}
private void remove(QueueItem item) {
//_log.trace ( "removing: <" + item + "> while...head=<"+head+">
tail=<"+tail+">" );
QueueItem temp;
- if ( item == null )
+ if ( item == null )
throw new NullPointerException();
if ( item == head ) {
@@ -278,7 +277,7 @@
}
}
- /*
+ /*
* call by ticker daemon
*/
private synchronized void tick() {
@@ -289,8 +288,7 @@
head.time--;
tailtime--;
}
- while ( head != null && head.time <= 0 ) {
-
+ while ( head != null && head.time <= 0 ) {
temp = head;
o = head.item;
@@ -298,9 +296,6 @@
map.remove(temp.key);
dispose(o);
}
- // if ( head == null ) {
- // ticker.stopTick();
- // }
}
private class ValuesEnumeration implements Enumeration {
@@ -314,7 +309,7 @@
v.trimToSize();
}
public boolean hasMoreElements() {
- if ( v.size() > cur )
+ if ( v.size() > cur )
return true;
return false;
}
@@ -324,7 +319,7 @@
Object o = v.get(cur++);
if ( o != null )
return ((QueueItem)o).item;
- else
+ else
return null;
}
}
@@ -380,12 +375,15 @@
try {
while ( true ) {
if ( isStopped ) {
- synchronized(lock) { lock.wait(); }
+ synchronized(lock) {
+ lock.wait();
+ }
isStopped = false;
- } else {
+ } else {
long time = System.currentTimeMillis();
- if ( time - lastTime < tick )
+ if ( time - lastTime < tick ){
sleep(tick-(time-lastTime));
+ }
lastTime = System.currentTimeMillis();
}
@@ -398,14 +396,16 @@
} catch ( InterruptedException e ) {
/*
* kindly disregard this exception
- */
+ */
}
}
+
public void addListener( TimeLimited t ) {
synchronized ( listenerLock ) {
listener = new LinkList( listener, t );
}
}
+
private class LinkList {
private LinkList next;
private TimeLimited t;
Index: src/main/org/exolab/castor/persist/cache/Unlimited.java
===================================================================
RCS file: /cvs/castor/castor/src/main/org/exolab/castor/persist/cache/Unlimited.java,v
retrieving revision 1.1
diff -u -r1.1 Unlimited.java
--- a/src/main/org/exolab/castor/persist/cache/Unlimited.java 13 Feb 2004 21:20:56
-0000 1.1
+++ b/src/main/org/exolab/castor/persist/cache/Unlimited.java 5 Apr 2004 03:36:31
-0000
@@ -52,7 +52,7 @@
/**
- * UnLimited is Map which implements the [EMAIL PROTECTED] LRU} interface.
+ * UnLimited is Map which implements the [EMAIL PROTECTED]
org.exolab.castor.persist.LRU} interface.
* <p>
* Every object being put in the Map will live until it is
* removed manually.
@@ -61,130 +61,127 @@
* @author <a href="werner DOT guttmann AT gmx DOT com">Werner Guttmann</a>
* @version $Revision: 1.1 $ $Date: 2004/02/13 21:20:56 $
*/
-public class Unlimited
-extends AbstractBaseCache
-{
-
- private Hashtable map = new Hashtable();
-
- /**
- * The <a href="http://jakarta.apache.org/commons/logging/">Jakarta
- * Commons Logging</a> instance used for all logging.
- */
- private static Log _log = LogFactory.getFactory().getInstance(Unlimited.class);
-
- /**
- * Constructor
- *
- */
- public Unlimited() {
- if (_log.isDebugEnabled()) {
- _log.trace ("Successfully created unlimited cache instance" );
- }
- }
-
- /**
- * Maps the specified <code>key</code> to the specified
- * <code>value</code> in this Map. Neither the key nor the
- * value can be <code>null</code>.
- * <p>
- * The value can be retrieved by calling the <code>get</code> method
- * with a key that is equal to the original key.
- * <p>
- * @param key the Map key.
- * @param value the value.
- * @return the previous value of the specified key in this Map,
- * or <code>null</code> if it did not have one.
- * @exception NullPointerException if the key or value is
- * <code>null</code>.
- */
- public Object put(Object key, Object value) {
- if (_log.isDebugEnabled()) {
- _log.trace ("Creating cache entry for key " + key + " with
value " + value);
- }
- return map.put(key,value);
- }
-
- /**
- *Returns the value to which the specified key is mapped in this Map.
- [EMAIL PROTECTED] key - a key in the Map.
- [EMAIL PROTECTED] the value to which the key is mapped in this Map; null if
- * the key is not mapped to any value in this Map.
- */
- public Object get(Object key) {
- if (_log.isDebugEnabled()) {
- _log.trace ("Getting cache entry for key " + key);
- }
- return map.get(key);
- }
-
- /**
- * Removes the key (and its corresponding value) from this
- * Map. This method does nothing if the key is not in the Map.
- *
- * @param key the key that needs to be removed.
- * @return the value to which the key had been mapped in this Map,
- * or <code>null</code> if the key did not have a mapping.
- */
- public Object remove(Object key) {
- if (_log.isDebugEnabled()) {
- _log.trace ("Removing cache entry for key " + key);
- }
- return map.remove(key);
- }
-
- /**
- * Returns an enumeration of the values in this LRU map.
- * Use the Enumeration methods on the returned object to fetch the elements
- * sequentially.
- *
- * @return an enumeration of the values in this Map.
- * @see java.util.Enumeration
- */
- public Enumeration elements() {
- return map.elements();
- }
-
-
- /**
- * Remove the object identified by key from the cache.
- *
- * @param key the key that needs to be removed.
- */
- public void expire(Object key) {
- if (_log.isDebugEnabled()) {
- _log.trace ("Expiring cache entry for key " + key);
- }
- if ( remove(key) == null ) {
- // _log.trace ("Unlimited LRU expire: "+key+" not found");
- }
- else {
- // _log.trace ("Unlimited LRU expire: "+key+" removed from
cache");
- }
- dispose(key);
- }
-
- /**
- * This method is called when an object is disposed.
- * Override this method if you interested in the disposed object.
- *
- * @param o - the disposed object
- */
- protected void dispose( Object o ) {
- if (_log.isDebugEnabled()) {
- _log.trace ("Disposing object " + o);
- }
- }
-
- /* Indicates whether the cache holds a valuze object for the specified key.
- * @see org.exolab.castor.persist.cache.Cache#contains(java.lang.Object)
- */
- public boolean contains(Object key) {
- if (_log.isDebugEnabled()) {
- _log.trace ("Testing for entry for key " + key);
- }
-
- return (this.get(key) != null);
- }
-
+public class Unlimited
+extends AbstractBaseCache {
+
+ private Hashtable map = new Hashtable();
+
+ /**
+ * The <a href="http://jakarta.apache.org/commons/logging/">Jakarta
+ * Commons Logging</a> instance used for all logging.
+ */
+ private static Log _log = LogFactory.getFactory().getInstance(Unlimited.class);
+
+ /**
+ * Constructor
+ *
+ */
+ public Unlimited() {
+ if (_log.isDebugEnabled()) {
+ _log.trace("Successfully created unlimited cache instance" );
+ }
+ }
+
+ /**
+ * Maps the specified <code>key</code> to the specified
+ * <code>value</code> in this Map. Neither the key nor the
+ * value can be <code>null</code>.
+ * <p>
+ * The value can be retrieved by calling the <code>get</code> method
+ * with a key that is equal to the original key.
+ * <p>
+ * @param key the Map key.
+ * @param value the value.
+ * @return the previous value of the specified key in this Map,
+ * or <code>null</code> if it did not have one.
+ * @exception NullPointerException if the key or value is
+ * <code>null</code>.
+ */
+ public Object put(Object key, Object value) {
+ if (_log.isDebugEnabled()) {
+ _log.trace("Creating cache entry for key " + key + " with value " +
value);
+ }
+ return map.put(key,value);
+ }
+
+ /**
+ *Returns the value to which the specified key is mapped in this Map.
+ [EMAIL PROTECTED] key - a key in the Map.
+ [EMAIL PROTECTED] the value to which the key is mapped in this Map; null if
+ * the key is not mapped to any value in this Map.
+ */
+ public Object get(Object key) {
+ if (_log.isDebugEnabled()) {
+ _log.trace("Getting cache entry for key " + key);
+ }
+ return map.get(key);
+ }
+
+ /**
+ * Removes the key (and its corresponding value) from this
+ * Map. This method does nothing if the key is not in the Map.
+ *
+ * @param key the key that needs to be removed.
+ * @return the value to which the key had been mapped in this Map,
+ * or <code>null</code> if the key did not have a mapping.
+ */
+ public Object remove(Object key) {
+ if (_log.isDebugEnabled()) {
+ _log.trace("Removing cache entry for key " + key);
+ }
+ return map.remove(key);
+ }
+
+ /**
+ * Returns an enumeration of the values in this LRU map.
+ * Use the Enumeration methods on the returned object to fetch the elements
+ * sequentially.
+ *
+ * @return an enumeration of the values in this Map.
+ * @see java.util.Enumeration
+ */
+ public Enumeration elements() {
+ return map.elements();
+ }
+
+
+ /**
+ * Remove the object identified by key from the cache.
+ *
+ * @param key the key that needs to be removed.
+ */
+ public void expire(Object key) {
+ if (_log.isDebugEnabled()) {
+ _log.trace("Expiring cache entry for key " + key);
+ }
+ remove(key);
+
+ dispose(key);
+ }
+
+ /**
+ * This method is called when an object is disposed.
+ * Override this method if you interested in the disposed object.
+ *
+ * @param o - the disposed object
+ */
+ protected void dispose( Object o ) {
+ if (_log.isDebugEnabled()) {
+ _log.trace("Disposing object " + o);
+ }
+ }
+
+ /**
+ * Indicates whether the cache holds a valuze object for the specified key.
+ *
+ * @see org.exolab.castor.persist.cache.Cache#contains(java.lang.Object)
+ */
+ public boolean contains(Object key) {
+ if (_log.isDebugEnabled()) {
+ _log.trace("Testing for entry for key " + key);
+ }
+
+ return (this.get(key) != null);
+ }
+
}
\ No newline at end of file
