org.jagatoo.util.cache
Class ResourceCache<K,T>

java.lang.Object
  extended by org.jagatoo.util.cache.ResourceCache<K,T>
Type Parameters:
K - the cache-key type
T - the cached item type

public final class ResourceCache<K,T>
extends java.lang.Object

A hashmap based cache that holds it's values with a SoftReference. The cache has an initial capacity for it's hash table. It will grow if more then the load factor (entries / capacity > load factor) is in use.

See Also:
SoftReference

Constructor Summary
ResourceCache()
          Constructs a new Cache object with default initial capacity and default load factor.
ResourceCache(int initialCapacity)
          Constructs a new Cache object with a given initial capacity and default load factor.
ResourceCache(int initialCapacity, float loadFactor)
          Constructs a new Cache object with a given initial capacity and load factor.
 
Method Summary
 void clear()
          Clears the caches
 T get(java.lang.String key)
          Retrives a object from the cache.
 int getCacheEntries()
           
 int getCacheHits()
          Returns the number of cache hits.
 int getCacheMisses()
          Returns the number of cache misses.
 T put(K key, T value)
          Puts a entry in the cache.
 void removeByKey(K key)
           
 void removeByObject(T obj)
           
 void resetStatistic()
          Resets the hit / miss statistik to zero.
 java.lang.String toString()
          Creates a String that contains name of the cache and the statistic.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ResourceCache

public ResourceCache(int initialCapacity,
                     float loadFactor)
Constructs a new Cache object with a given initial capacity and load factor.

Parameters:
initialCapacity - The initial capacity of the cache
loadFactor - The load factor.

ResourceCache

public ResourceCache(int initialCapacity)
Constructs a new Cache object with a given initial capacity and default load factor.

Parameters:
initialCapacity - The initial capacity of the cache

ResourceCache

public ResourceCache()
Constructs a new Cache object with default initial capacity and default load factor.

Method Detail

clear

public void clear()
Clears the caches


get

public T get(java.lang.String key)
Retrives a object from the cache.

Parameters:
key - The key under which the Texture is to be stored.
Returns:
The Texture or null if it is not in the cache.

put

public T put(K key,
             T value)
Puts a entry in the cache. If an entry is already registered under the given key, then the entry will be replaced.

Parameters:
key - The key under which the object should be stored. May not be null.
value - The object that should be stored.
Returns:
value. (For simplyfied calling)

removeByKey

public void removeByKey(K key)
Parameters:
key -

removeByObject

public void removeByObject(T obj)
Parameters:
obj -

getCacheHits

public int getCacheHits()
Returns the number of cache hits. A cache hit is a call to get() that

Returns:
a non-null reference.

getCacheMisses

public int getCacheMisses()
Returns the number of cache misses. A cache miss is a call to get() that returned a null reference.

Returns:
The number of cache misses.

getCacheEntries

public int getCacheEntries()
Returns:
The number of entries currently in the cache.

resetStatistic

public void resetStatistic()
Resets the hit / miss statistik to zero.


toString

public java.lang.String toString()
Creates a String that contains name of the cache and the statistic.

Overrides:
toString in class java.lang.Object
Returns:
A textual representation of the cache statistik.