org.mmbase.util
Class LRUHashtable

java.lang.Object
  extended byjava.util.Dictionary
      extended byjava.util.Hashtable
          extended byorg.mmbase.util.LRUHashtable
All Implemented Interfaces:
CacheImplementationInterface, Cloneable, Map, Serializable, SizeMeasurable

public class LRUHashtable
extends Hashtable
implements Cloneable, CacheImplementationInterface, SizeMeasurable

A hashtable which has a maximum of entries. Old entries are removed when the maximum is reached. This table is used mostly to implement a simple caching system.

Version:
$Id: LRUHashtable.java,v 1.24 2006/02/23 17:37:23 michiel Exp $
Author:
Rico Jansen, Michiel Meeuwissen
See Also:
Cache, Serialized Form
Warning: Will be moved to:
consider moving to org.mmbase.cache

Nested Class Summary
 class LRUHashtable.LRUEntry
          Element used to store information from the LRUHashtable.
protected  class LRUHashtable.LRUEntrySet
          Used by 'entrySet' implementation, to make the Map modifiable.
protected  class LRUHashtable.LRUEntrySetIterator
          Used by 'entrySet' implementation, to make the Map modifiable.
 
Nested classes inherited from class java.util.Map
Map.Entry
 
Constructor Summary
LRUHashtable()
          Creates the URL Hashtable with maximum capacity 100, starting capacity 101, and growing capacity 0.75.
LRUHashtable(int size)
          Creates the URL Hashtable with starting capacity 101 and growing capacity 0.75.
LRUHashtable(int size, int cap)
          Creates the URL Hashtable with growing capacity 0.75.
LRUHashtable(int size, int cap, float lf)
          Creates the URL Hashtable.
 
Method Summary
 void clear()
          Clears the table.
 Object clone()
          NOT IMPLEMENTED
 void config(Map map)
          Configure the implementation with the given configuration values
 Enumeration elements()
          Returns an Enumeration on the table's element values.
 Set entrySet()
          Returns the entries of this Map.
 Object get(Object key)
          Retrieves an element from the table.
 int getByteSize()
          Determines the byte-size of this object
 int getByteSize(SizeOf sizeof)
          Determines the byte-size of this object using the given SizeOf instance.
 int getCount(Object key)
          Retrieves the count of the object with a certain key.
 Enumeration getOrderedElements()
          Deprecated. use getOrderedEntries
 Enumeration getOrderedElements(int maxnumber)
          Deprecated. use getOrderedEntries
 List getOrderedEntries()
          Returns an ordered list of Map.Entry's.
 List getOrderedEntries(int maxNumber)
          Returns an ordered list of Map.Entry's.
 Set keySet()
          You should only remove entries from LRUHashtable using the 'remove' function, or using the iterator of entrySet() otherwise the linked list gets messed up.
static void main(String[] argv)
           
 int maxSize()
          Return the maximum size of the table
 Object put(Object key, Object value)
          Store an element in the table.
 Object remove(Object key)
          Remove an element from the table.
 void setMaxSize(int size)
          Change the maximum size of the table.
 int size()
          Return the current size of the table
 String toString()
          Returns a description of the table.
 String toString(boolean which)
          Returns a description of the table.
 Collection values()
           
 
Methods inherited from class java.util.Hashtable
contains, containsKey, containsValue, equals, hashCode, isEmpty, keys, putAll, rehash
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
containsKey, containsValue, equals, hashCode, isEmpty, putAll
 

Constructor Detail

LRUHashtable

public LRUHashtable(int size,
                    int cap,
                    float lf)
Creates the URL Hashtable.

Parameters:
size - the maximum capacity
cap - the starting capacity (used to improve performance)
lf - the amount with which current capacity frows

LRUHashtable

public LRUHashtable(int size,
                    int cap)
Creates the URL Hashtable with growing capacity 0.75.

Parameters:
size - the maximum capacity
cap - the starting capacity (used to improve performance)

LRUHashtable

public LRUHashtable(int size)
Creates the URL Hashtable with starting capacity 101 and growing capacity 0.75.

Parameters:
size - the maximum capacity

LRUHashtable

public LRUHashtable()
Creates the URL Hashtable with maximum capacity 100, starting capacity 101, and growing capacity 0.75.

Method Detail

put

public Object put(Object key,
                  Object value)
Store an element in the table.

Specified by:
put in interface Map
Parameters:
key - the key of the element
value - the value of the element
Returns:
the original value of the element if it existed, null if it could not be found

getCount

public int getCount(Object key)
Retrieves the count of the object with a certain key.

Specified by:
getCount in interface CacheImplementationInterface
Parameters:
key - the key of the element
Returns:
the times the key has been requested

get

public Object get(Object key)
Retrieves an element from the table.

Specified by:
get in interface Map
Parameters:
key - the key of the element
Returns:
the value of the element, or null if it could not be found

remove

public Object remove(Object key)
Remove an element from the table.

Specified by:
remove in interface Map
Parameters:
key - the key of the element
Returns:
the original value of the element if it existed, null if it could not be found

keySet

public Set keySet()
You should only remove entries from LRUHashtable using the 'remove' function, or using the iterator of entrySet() otherwise the linked list gets messed up. The keySet of LRUHashtable therefore returns an unmodifiable set.

Specified by:
keySet in interface Map
Since:
MMBase-1.6.3

entrySet

public Set entrySet()
Returns the entries of this Map. Modification are reflected.

Specified by:
entrySet in interface Map
Since:
MMBase-1.6.3

values

public Collection values()
Specified by:
values in interface Map
Since:
MMBase-1.6.3
See Also:
keySet()

size

public int size()
Return the current size of the table

Specified by:
size in interface Map

setMaxSize

public void setMaxSize(int size)
Change the maximum size of the table. This may result in removal of entries in the table.

Specified by:
setMaxSize in interface CacheImplementationInterface
Parameters:
size - the new desired size

maxSize

public int maxSize()
Return the maximum size of the table

Specified by:
maxSize in interface CacheImplementationInterface

toString

public String toString()
Returns a description of the table. The information shown includes current size, maximum size, ratio of misses and hits, and a description of the underlying hashtable


toString

public String toString(boolean which)
Returns a description of the table. The information shown includes current size, maximum size, ratio of misses and hits, and either a description of the underlying hashtable, or a list of all stored values.

Parameters:
which - if true, the stored values are described.
Returns:
a description of the table.

clear

public void clear()
Clears the table.

Specified by:
clear in interface Map

clone

public Object clone()
NOT IMPLEMENTED


elements

public Enumeration elements()
Returns an Enumeration on the table's element values.


getOrderedElements

public Enumeration getOrderedElements()
Deprecated. use getOrderedEntries


getOrderedElements

public Enumeration getOrderedElements(int maxnumber)
Deprecated. use getOrderedEntries


getOrderedEntries

public List getOrderedEntries()
Returns an ordered list of Map.Entry's.

Since:
MMBase-1.6

getOrderedEntries

public List getOrderedEntries(int maxNumber)
Returns an ordered list of Map.Entry's. This can be used to present the contents of the LRU Map.

Since:
MMBase-1.6

config

public void config(Map map)
Description copied from interface: CacheImplementationInterface
Configure the implementation with the given configuration values

Specified by:
config in interface CacheImplementationInterface

getByteSize

public int getByteSize()
Description copied from interface: SizeMeasurable
Determines the byte-size of this object

Specified by:
getByteSize in interface SizeMeasurable

getByteSize

public int getByteSize(SizeOf sizeof)
Description copied from interface: SizeMeasurable
Determines the byte-size of this object using the given SizeOf instance. A SizeOf instance stores a Set already counted objects. So this method is typically called by SizeOf itself (recursion).

Specified by:
getByteSize in interface SizeMeasurable

main

public static void main(String[] argv)


MMBase build 1.8.1.20060716