org.mmbase.util
Class LRUHashtable

java.lang.Object
  |
  +--java.util.Dictionary
        |
        +--java.util.Hashtable
              |
              +--org.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.11.2.1 2003/04/03 15:58:22 michiel Exp $
Author:
Rico Jansen, Michiel Meeuwissen
See Also:
Cache, Serialized Form

Inner Class Summary
static class LRUHashtable.LRUEntry
          Element used to store information from the LRUHashtable.
 
Inner classes inherited from class java.util.Map
java.util.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.
 java.lang.Object clone()
          NOT IMPLEMENTED
 java.util.Enumeration elements()
          Returns an Enumeration on the table's element values.
 java.util.Set entrySet()
           
 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.
 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.
Overrides:
put in class java.util.Hashtable
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.
Overrides:
get in class java.util.Hashtable
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.
Overrides:
remove in class java.util.Hashtable
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. otherwise the linked list gets messed up. The keySet of LRUHashtable therefore returns an unmodifiable set.
Overrides:
keySet in class java.util.Hashtable
Since:
MMBase-1.6.3

entrySet

public java.util.Set entrySet()
Overrides:
entrySet in class java.util.Hashtable
Since:
MMBase-1.6.3
See Also:
keySet()

values

public java.util.Collection values()
Overrides:
values in class java.util.Hashtable
Since:
MMBase-1.6.3
See Also:
keySet()

size

public int size()
Return the current size of the table
Overrides:
size in class java.util.Hashtable

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
Overrides:
toString in class java.util.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.
Overrides:
clear in class java.util.Hashtable

clone

public java.lang.Object clone()
NOT IMPLEMENTED
Overrides:
clone in class java.util.Hashtable

elements

public java.util.Enumeration elements()
Returns an Enumeration on the table's element values.
Overrides:
elements in class java.util.Hashtable

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.6.5.20030923