com.partnersoft.gadgets
Class HashedCache<K,V>

java.lang.Object
  extended by com.partnersoft.gadgets.HashedCache<K,V>
All Implemented Interfaces:
Cache<K,V>, java.lang.Comparable

public abstract class HashedCache<K,V>
extends java.lang.Object
implements Cache<K,V>

A fixed-size caching implementation based on a hashed index.

Copyright 1998-2009 Partner Software, Inc.

Version:
$Id: HashedCache.java 2328 2010-01-06 15:38:22Z paul $
Author:
Paul Reavis

Field Summary
static int DEFAULTSIZE
           
 
Constructor Summary
HashedCache()
          Create a hashed cache of the default size, 42.
HashedCache(int size)
          Create a hashed cache of the desired size.
 
Method Summary
 V cachedValueFor(K index)
          Get the object associated with the given index, keeping the returned item in cache subject to capacity and other settings.
 void clear()
          Clears all items from the cache, so that they must be reloaded.
 int compareTo(java.lang.Object nother)
           
 void finalize(V flushCandidate)
          Optional override for subclasses - called before flushing an object.
 void flush()
          Flushes the cache, removing values until the capacity is reached.
 int getByteSize()
          Gets an estimate of the size of the cached values, in bytes.
 int getCapacity()
          Gets the maximum number of cached values.
 java.lang.String getName()
           
 boolean getUsingSeparateReaperThread()
          If true, creates a separate thread which reaps old entries from the cache.
 boolean isFlushable(V flushCandidate)
          Optional override for subclasses - tests if an object can be flushed.
 boolean isManualFlushing()
           
 java.util.List<V> listCachedValues()
          Lists the values currently in cache.
 boolean remove(java.lang.Object index)
           
 void setCapacity(int newSize)
          Sets the maximum number of cached values.
 void setManualFlushing(boolean manualFlushing)
          Setting this to true turns of automatic flushing done whenever new cached values are gotten.
 void setUsingSeparateReaperThread(boolean tizit)
           
 int size()
          Returns the current cache size (number of cached values).
abstract  V uncachedValueFor(K index)
          Get the object associated with the given index, without regard to the cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULTSIZE

public static final int DEFAULTSIZE
See Also:
Constant Field Values
Constructor Detail

HashedCache

public HashedCache()
Create a hashed cache of the default size, 42.


HashedCache

public HashedCache(int size)
Create a hashed cache of the desired size.

Method Detail

cachedValueFor

public V cachedValueFor(K index)
Description copied from interface: Cache
Get the object associated with the given index, keeping the returned item in cache subject to capacity and other settings.

Specified by:
cachedValueFor in interface Cache<K,V>

finalize

public void finalize(V flushCandidate)
Optional override for subclasses - called before flushing an object. Does nothing by default.


isFlushable

public boolean isFlushable(V flushCandidate)
Optional override for subclasses - tests if an object can be flushed. Returns true by default.


uncachedValueFor

public abstract V uncachedValueFor(K index)
Description copied from interface: Cache
Get the object associated with the given index, without regard to the cache. Generally this method is called by the getCached() method on cache misses.

Specified by:
uncachedValueFor in interface Cache<K,V>

clear

public void clear()
Description copied from interface: Cache
Clears all items from the cache, so that they must be reloaded.

Specified by:
clear in interface Cache<K,V>

listCachedValues

public java.util.List<V> listCachedValues()
Description copied from interface: Cache
Lists the values currently in cache.

Specified by:
listCachedValues in interface Cache<K,V>

getCapacity

public int getCapacity()
Description copied from interface: Cache
Gets the maximum number of cached values.

Specified by:
getCapacity in interface Cache<K,V>

setCapacity

public void setCapacity(int newSize)
Description copied from interface: Cache
Sets the maximum number of cached values.

Specified by:
setCapacity in interface Cache<K,V>

isManualFlushing

public boolean isManualFlushing()

setManualFlushing

public void setManualFlushing(boolean manualFlushing)
Setting this to true turns of automatic flushing done whenever new cached values are gotten. Use this when you need explicit control over when flushing happens. Use the flush() call to trigger a flush.


getUsingSeparateReaperThread

public boolean getUsingSeparateReaperThread()
If true, creates a separate thread which reaps old entries from the cache. If false, reaping is done whenever new items are added.


setUsingSeparateReaperThread

public void setUsingSeparateReaperThread(boolean tizit)

compareTo

public int compareTo(java.lang.Object nother)
Specified by:
compareTo in interface java.lang.Comparable

flush

public void flush()
Flushes the cache, removing values until the capacity is reached. This should generally only be used when manualFlushing = true.


remove

public boolean remove(java.lang.Object index)

getByteSize

public int getByteSize()
Description copied from interface: Cache
Gets an estimate of the size of the cached values, in bytes. If this cannot be determined, a -1 should be returned.

Specified by:
getByteSize in interface Cache<K,V>

size

public int size()
Description copied from interface: Cache
Returns the current cache size (number of cached values).

Specified by:
size in interface Cache<K,V>

getName

public java.lang.String getName()
Specified by:
getName in interface Cache<K,V>