net.sourceforge.rcache.decorator
Class CacheProfiler<K,V>

java.lang.Object
  extended by net.sourceforge.rcache.decorator.CacheProfiler<K,V>
Type Parameters:
K - the type of keys maintained by this cache
V - the type of cached values
All Implemented Interfaces:
Cache<K,V>

public final class CacheProfiler<K,V>
extends java.lang.Object
implements Cache<K,V>

Keeps track of the average execution time for each operation.

Author:
Rodrigo Ruiz

Nested Class Summary
 
Nested classes/interfaces inherited from interface net.sourceforge.rcache.Cache
Cache.Operation
 
Field Summary
 
Fields inherited from interface net.sourceforge.rcache.Cache
DEFAULT_CONCURRENCY_LEVEL, DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR
 
Constructor Summary
CacheProfiler(Cache<K,V> cache)
          Creates an instance.
 
Method Summary
 void clear()
          Removes all entries from this cache.
 V get(java.lang.Object key)
          Returns the value to which this cache maps the specified key.
 double getAverage(Cache.Operation op)
          Gets the average execution time of an operation.
 double[] getAverages()
          Gets the average execution time of all this cache operations.
 long getNum(Cache.Operation op)
          Gets the number of registered invocations to the specified Cache operation.
 long[] getNums()
          Gets the number of invocations to each Cache operation.
 V put(K key, V value)
          Associates the specified value with the specified key in this cache, but only if no value is associated yet.
 V remove(java.lang.Object key)
          Removes the entry for this key from this cache if present.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CacheProfiler

public CacheProfiler(Cache<K,V> cache)
Creates an instance.

Parameters:
cache - The cache instance to wrap
Method Detail

get

public V get(java.lang.Object key)

Returns the value to which this cache maps the specified key. Returns null if the cache contains no entry for this key. A return value of null does not necessarily indicate that the cache contains no entry for the key; it's also possible that the cache explicitly maps the key to null

.

Although this method should use K for the key type, it is left as Object to keep this interface compatible with Map.

Specified by:
get in interface Cache<K,V>
Parameters:
key - key whose associated value is to be returned.
Returns:
the value to which this cache associates the specified key.

put

public V put(K key,
             V value)

Associates the specified value with the specified key in this cache, but only if no value is associated yet. If the cache previously contained an entry for this key, the old value is returned, but not replaced.

This method is equivalent to the putIfAbsent from the ConcurrentMap class, as it is more appropriate for a cache than the original one from Map.

Specified by:
put in interface Cache<K,V>
Parameters:
key - key with which the specified value is to be associated.
value - value to be associated with the specified key.
Returns:
the value associated with specified key. If the key was already associated with a value, the old value is returned. Otherwise, value is returned

remove

public V remove(java.lang.Object key)

Removes the entry for this key from this cache if present.

Although this method should use K for the key type, it is left as Object to keep this interface compatible with Map.

Specified by:
remove in interface Cache<K,V>
Parameters:
key - key whose entry is to be removed from the cache.
Returns:
previous value associated with specified key, or null if there was no entry for key. (A null return can also indicate that the cache previously associated null with the specified key)

clear

public void clear()

Removes all entries from this cache.

This method must be handled very carefully, as it will break the assumption of unique instances for cached factories.

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

getAverage

public double getAverage(Cache.Operation op)
Gets the average execution time of an operation.

Parameters:
op - The operation
Returns:
The operation average execution time

getAverages

public double[] getAverages()
Gets the average execution time of all this cache operations.

Returns:
An array containing the average execution times in the same order as the elements of the Operation enum

getNum

public long getNum(Cache.Operation op)
Gets the number of registered invocations to the specified Cache operation.

Parameters:
op - Operation
Returns:
Number of tracked invocations

getNums

public long[] getNums()
Gets the number of invocations to each Cache operation.

Returns:
An array containing the invocation tracking


© 2007-2009 Rodrigo Ruiz
This site is hosted by