org.egothor.cache
Class AbstractCache<K,V>

java.lang.Object
  extended by org.egothor.cache.AbstractCache<K,V>
Type Parameters:
K - type of the key
V - type of the value
All Implemented Interfaces:
Cache<K,V>
Direct Known Subclasses:
ArcCache, FifoCache, LfuCache, LruCache, LruKCache, MultiQueueCache, SlruCache, StaticCache, TwoQueueCache

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

An abstract implementation of Cache that provides some common methods for cache classes.


Field Summary
protected  int capacity
          Capacity of this cache.
static int DEFAULT_CAPACITY
          Default capacity of the cache if no explicit capacity is provided.
protected  long hits
          Number of successful get requests.
protected  long misses
          Number of unsuccessful get requests.
protected  Resolver<K,V> resolver
          Object implementing Resolver interface that can be used to retrieve the value for the specified key in case of a cache miss.
 
Constructor Summary
protected AbstractCache(Resolver<K,V> resolver, int capacity)
          Constructor for the AbstractCache object.
 
Method Summary
 int capacity()
          Returns the maximum number of key->value mappings for this cache.
abstract  boolean containsKey(K key)
          Checks whether the specified key is cached.
abstract  void evict()
          Removes the next key from the cache according to the current cache replacement policy.
 V get(K key)
          Checks whether key parameter is not null.
 Resolver<K,V> getResolver()
          Gets the current resolver associated with this cache.
 float hitRatio()
          Computes the hits/requests ratio in percents.
 long hits()
          Returns the number of all hits.
 long misses()
          Returns the number of all misses.
abstract  K nextEvicted()
          Gets the next key that would be removed from the cache according to the current cache replacement policy.
 V put(K key, V value)
          Checks whether key and value parameters are not null.
 V remove(K key)
          Checks whether key parameter is not null.
 long requests()
          Returns the number of all submitted requests
 void resetCounters()
          Sets hits and misses counters to zero.
 Resolver<K,V> setResolver(Resolver<K,V> resolver)
          Sets the resolver associated with this cache.
abstract  int size()
          Returns the number of key->value mappings in the cache.
abstract  java.lang.String toString()
           
 V update(K key, V value)
          Checks whether key and value parameters are not null.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.egothor.cache.Cache
clear, keySet
 

Field Detail

DEFAULT_CAPACITY

public static final int DEFAULT_CAPACITY
Default capacity of the cache if no explicit capacity is provided.

See Also:
Constant Field Values

capacity

protected int capacity
Capacity of this cache.


hits

protected long hits
Number of successful get requests.


misses

protected long misses
Number of unsuccessful get requests.


resolver

protected Resolver<K,V> resolver
Object implementing Resolver interface that can be used to retrieve the value for the specified key in case of a cache miss.

Constructor Detail

AbstractCache

protected AbstractCache(Resolver<K,V> resolver,
                        int capacity)
                 throws java.lang.IllegalArgumentException
Constructor for the AbstractCache object. Checks if the capacity argument is valid and set the resolver to NullResolver if no resolver was specified.

Parameters:
resolver - resolver that will be associated with the cache
capacity - capacity of the cache
Throws:
java.lang.IllegalArgumentException - when the capacity is smaller or equal to zero
Method Detail

put

public V put(K key,
             V value)
Checks whether key and value parameters are not null. Always returns null.

Specified by:
put in interface Cache<K,V>
Parameters:
key - key to add to the cache
value - value to map the key on
Returns:
null

get

public V get(K key)
Checks whether key parameter is not null. Always returns null.

Specified by:
get in interface Cache<K,V>
Parameters:
key - key to get the value for
Returns:
null

remove

public V remove(K key)
Checks whether key parameter is not null. Always returns null.

Specified by:
remove in interface Cache<K,V>
Parameters:
key - key to remove from the cache.
Returns:
null

update

public V update(K key,
                V value)
Checks whether key and value parameters are not null. Always returns null.

Specified by:
update in interface Cache<K,V>
Parameters:
key - key to change the value for
value - new value associated with the key
Returns:
null

containsKey

public abstract boolean containsKey(K key)
Description copied from interface: Cache
Checks whether the specified key is cached.

Specified by:
containsKey in interface Cache<K,V>
Parameters:
key - key to check
Returns:
true, if the key is currently cached, false otherwise

size

public abstract int size()
Description copied from interface: Cache
Returns the number of key->value mappings in the cache.

Specified by:
size in interface Cache<K,V>
Returns:
number of key->value mappings in the cache

nextEvicted

public abstract K nextEvicted()
Description copied from interface: Cache
Gets the next key that would be removed from the cache according to the current cache replacement policy.

Specified by:
nextEvicted in interface Cache<K,V>
Returns:
key, that would be removed next from the cache or null if the cache is empty

evict

public abstract void evict()
Description copied from interface: Cache
Removes the next key from the cache according to the current cache replacement policy.

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

toString

public abstract java.lang.String toString()
Overrides:
toString in class java.lang.Object

hitRatio

public float hitRatio()
Computes the hits/requests ratio in percents.

Specified by:
hitRatio in interface Cache<K,V>
Returns:
hit ratio of the cache

hits

public long hits()
Returns the number of all hits.

Specified by:
hits in interface Cache<K,V>
Returns:
number of all hits

misses

public long misses()
Returns the number of all misses.

Specified by:
misses in interface Cache<K,V>
Returns:
number of all misses

requests

public long requests()
Returns the number of all submitted requests

Specified by:
requests in interface Cache<K,V>
Returns:
number of all submitted requests

capacity

public int capacity()
Returns the maximum number of key->value mappings for this cache.

Specified by:
capacity in interface Cache<K,V>
Returns:
maximum capacity of the cache

resetCounters

public void resetCounters()
Sets hits and misses counters to zero.

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

getResolver

public Resolver<K,V> getResolver()
Gets the current resolver associated with this cache.

Specified by:
getResolver in interface Cache<K,V>
Returns:
resolver associated with the cache

setResolver

public Resolver<K,V> setResolver(Resolver<K,V> resolver)
Sets the resolver associated with this cache.

Specified by:
setResolver in interface Cache<K,V>
Parameters:
resolver - new resolver we want to associate with this cache.
Returns:
replaced resolver