JTemporal Home
Tutorial

net.sf.jtemporal.util
Class LightLRUCache<K,V>

java.lang.Object
  extended by net.sf.jtemporal.util.LightLRUCache<K,V>
Type Parameters:
K - the type of the key
V - the type of the value

public class LightLRUCache<K,V>
extends java.lang.Object

A lightweight fast LRU cache, based on a HashMap and a linked list.
Reading a value, updates the LRU order. This class is NOT thread-safe.
Note the the performance is directly dependent on the proformance of the hashcode() implementation of your key objects.

Version:
$Id: LightLRUCache.java,v 1.5 2007/09/09 07:50:28 tabeck Exp $

Constructor Summary
LightLRUCache(int size)
          Creates a new LRU cache, of the given size.
LightLRUCache(int size, float loadFactor)
          Creates a new LRU cache, of the given size, whose internal HashMap has the given load factor
 
Method Summary
 void clear()
          Empties the cache.
 int currentSize()
          The number of entries currently in the cache.
 V get(java.lang.Object key)
          Retrieves an entry.
 int maxSize()
          The maximum number of entries.
 V put(K key, V value)
          Adds an new entry to this cache.
 V remove(java.lang.Object key)
          Removes an entry.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LightLRUCache

public LightLRUCache(int size)
Creates a new LRU cache, of the given size.

Parameters:
size - the maximum number of entries kept in this cache

LightLRUCache

public LightLRUCache(int size,
                     float loadFactor)
Creates a new LRU cache, of the given size, whose internal HashMap has the given load factor

Parameters:
size - the maximum number of entries kept in this cache
loadFactor - as defined in HashMap#. Default is 0.75, higher saves space, lower is faster.
Method Detail

put

public V put(K key,
             V value)
Adds an new entry to this cache. If the cache has already reached it's maximum size, the Least Recently Used entry is deleted.

Parameters:
key -
value -
Returns:
the previous values associated to this key, or null

get

public V get(java.lang.Object key)
Retrieves an entry. Additionally, the retrieved entry becomes the mru.

Parameters:
key -
Returns:
the value currently associated to this key, or null if not found.

remove

public V remove(java.lang.Object key)
Removes an entry.

Parameters:
key -
Returns:
the value currently associated to this key, or null if not found.

clear

public void clear()
Empties the cache.


currentSize

public int currentSize()
The number of entries currently in the cache.

Returns:
The number of entries currently in the cache.

maxSize

public int maxSize()
The maximum number of entries.

Returns:
The maximum number of entries.

toString

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

JTemporal Home
Tutorial