org.mmbase.util
Class LRUHashtable

java.lang.Object
  extended byjava.util.Dictionary
      extended byjava.util.Hashtable
          extended byorg.mmbase.util.LRUHashtable
All Implemented Interfaces:
java.lang.Cloneable, java.util.Map, java.io.Serializable
Direct Known Subclasses:
Cache, MultilevelCacheHandler

public class LRUHashtable
extends java.util.Hashtable
implements java.lang.Cloneable

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.17.2.1 2004/09/07 14:37:24 michiel Exp $
Author:
Rico Jansen, Michiel Meeuwissen
See Also:
Cache, Serialized Form

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.
 
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.
 java.lang.Object clone()
          NOT IMPLEMENTED
 java.util.Enumeration elements()
          Returns an Enumeration on the table's element values.
 java.util.Set entrySet()
          Returns the entries of this Map.
 java.lang.Object get(java.lang.Object key)
          Retrieves an element from the table.
 int getCount(java.lang.Object key)
          Retrieves the count of the object with a certain key.
 int getHits()
          Returns the number of times an element was succesfully retrieved from the table.
 int getMisses()
          Returns the number of times an element cpould not be retrieved from the table.
 java.util.Enumeration getOrderedElements()
          Deprecated. use getOrderedEntries
 java.util.Enumeration getOrderedElements(int maxnumber)
          Deprecated. use getOrderedEntries
 java.util.List getOrderedEntries()
          Returns an ordered list of Map.Entry's.
 java.util.List getOrderedEntries(int maxNumber)
          Returns an ordered list of Map.Entry's.
 int getPuts()
          Returns the number of times an element was committed to the table.
 double getRatio()
          Returns the ratio of hits and misses.
 int getSize()
          Return the maximum size of the table
 java.lang.String getStats()
          Returns statistics on this table.
 java.util.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.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Store an element in the table.
 java.lang.Object remove(java.lang.Object key)
          Remove an element from the table.
 void setSize(int size)
          Change the maximum size of the table.
 int size()
          Return the current size of the table
 java.lang.String toString()
          Returns a description of the table.
 java.lang.String toString(boolean which)
          Returns a description of the table.
 java.util.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
 

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 java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Store an element in the table.

Specified by:
put in interface java.util.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(java.lang.Object key)
Retrieves the count of the object with a certain key.

Parameters:
key - the key of the element
Returns:
the times the key has been requested

get

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

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

remove

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

Specified by:
remove in interface java.util.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 java.util.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 java.util.Map
Since:
MMBase-1.6.3

entrySet

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

Specified by:
entrySet in interface java.util.Map
Since:
MMBase-1.6.3

values

public java.util.Collection values()
Specified by:
values in interface java.util.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 java.util.Map

setSize

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

Parameters:
size - the new desired size

getSize

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


toString

public java.lang.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 java.lang.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 java.util.Map

clone

public java.lang.Object clone()
NOT IMPLEMENTED


elements

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


getRatio

public double getRatio()
Returns the ratio of hits and misses. The higher the ratio, the more succesfull the table retrieval is. A value of 1 means every attempt to retrieve data is succesfull, while a value nearing 0 means most times the object requested it is not available. Generally a high ratio means the table can be shrunk, while a low ratio means its size needs to be increased.

Returns:
A double between 0 and 1 or NaN.

getHits

public int getHits()
Returns the number of times an element was succesfully retrieved from the table.


getMisses

public int getMisses()
Returns the number of times an element cpould not be retrieved from the table.


getPuts

public int getPuts()
Returns the number of times an element was committed to the table.


getStats

public java.lang.String getStats()
Returns statistics on this table. The information shown includes number of accesses, ratio of misses and hits, current size, and number of puts.


getOrderedElements

public java.util.Enumeration getOrderedElements()
Deprecated. use getOrderedEntries


getOrderedElements

public java.util.Enumeration getOrderedElements(int maxnumber)
Deprecated. use getOrderedEntries


getOrderedEntries

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

Since:
MMBase-1.6

getOrderedEntries

public java.util.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


MMBase build 1.7.1.20041002