com.waveset.util
Class LRUCache

java.lang.Object
  extended bycom.waveset.util.LRUCache
All Implemented Interfaces:
java.util.Map

public class LRUCache
extends java.lang.Object
implements java.util.Map

A simple implementation of Map that limits the number of entries and, when the limit is reached, scavenges the least recently used item to replace with the new item. This is an unsynchronized collection. If you use this class from multiple threads, you must do it in a synchronized context, or use Collections.synchronized map. For example: LRUCache lruCache = new LRUCache(1000); Map syncMap = Collections.synchronizedMap(lruCache); After this, you would use syncMap for all map access. If you need to get at an LRUCache specific method, first synchronize on the syncMap, eg: synchronized(syncMap) { int listSize = lruCache.getMRUOrderedKeyList().size(); int mapSize = lruCache.size(); }


Nested Class Summary
static class LRUCache.CacheReport
           
 
Nested classes inherited from class java.util.Map
java.util.Map.Entry
 
Field Summary
static java.lang.String code_id
           
 
Constructor Summary
LRUCache(int size)
          Create a cache limited to @param size elements
LRUCache(int size, java.util.Map map)
          Create a cache limited to @param size elements.
 
Method Summary
 void clear()
           
 boolean containsKey(java.lang.Object key)
           
 boolean containsValue(java.lang.Object value)
           
 java.util.Set entrySet()
           
 boolean equals(java.lang.Object o)
           
 java.lang.Object get(java.lang.Object key)
          Get the requested object.
 LRUCache.CacheReport getCacheReport()
           
 java.util.LinkedList getLRUOrderedKeyList()
          Return a list of keys in MRU order.
 java.util.LinkedList getMRUOrderedKeyList()
          Return a list of keys in MRU order.
 int getNumAdds()
           
 int getNumHits()
           
 int getNumMisses()
           
 int getNumReplacements()
           
 int getSizeLimit()
           
 boolean isEmpty()
           
 java.util.Set keySet()
           
static void main(java.lang.String[] args)
           
 java.lang.Object peek(java.lang.Object key)
          Get the requested object.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
           
 void putAll(java.util.Map t)
           
 java.lang.Object remove(java.lang.Object key)
           
 int size()
           
 java.util.Collection values()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Map
hashCode
 

Field Detail

code_id

public static final java.lang.String code_id
See Also:
Constant Field Values
Constructor Detail

LRUCache

public LRUCache(int size)
Create a cache limited to @param size elements


LRUCache

public LRUCache(int size,
                java.util.Map map)
Create a cache limited to @param size elements. Use the Map provided, adding existing elements if needed.

Method Detail

size

public int size()
Specified by:
size in interface java.util.Map

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface java.util.Map

containsKey

public boolean containsKey(java.lang.Object key)
Specified by:
containsKey in interface java.util.Map

containsValue

public boolean containsValue(java.lang.Object value)
Specified by:
containsValue in interface java.util.Map

get

public java.lang.Object get(java.lang.Object key)
Get the requested object. put it at the head of the mru list if found

Specified by:
get in interface java.util.Map

peek

public java.lang.Object peek(java.lang.Object key)
Get the requested object. DO NOT change the mru list if found


put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Specified by:
put in interface java.util.Map

remove

public java.lang.Object remove(java.lang.Object key)
Specified by:
remove in interface java.util.Map

putAll

public void putAll(java.util.Map t)
Specified by:
putAll in interface java.util.Map

clear

public void clear()
Specified by:
clear in interface java.util.Map

keySet

public java.util.Set keySet()
Specified by:
keySet in interface java.util.Map

values

public java.util.Collection values()
Specified by:
values in interface java.util.Map

entrySet

public java.util.Set entrySet()
Specified by:
entrySet in interface java.util.Map

getMRUOrderedKeyList

public java.util.LinkedList getMRUOrderedKeyList()
Return a list of keys in MRU order. This just returns a reference to the internal list and so is the fastest. To get the oldest element you can call peek(getMRUOrderedKeyList().getLast())


getLRUOrderedKeyList

public java.util.LinkedList getLRUOrderedKeyList()
Return a list of keys in MRU order. This copies the internal MRU list, reverses it, and returns it. If you are looking for efficiency, call getMRUOrderedKeyList() instead.


equals

public boolean equals(java.lang.Object o)
Specified by:
equals in interface java.util.Map

getNumAdds

public int getNumAdds()

getNumReplacements

public int getNumReplacements()

getNumHits

public int getNumHits()

getNumMisses

public int getNumMisses()

getSizeLimit

public int getSizeLimit()

getCacheReport

public LRUCache.CacheReport getCacheReport()

main

public static void main(java.lang.String[] args)