com.go.trove.util
Class WrappedCache

java.lang.Object
  |
  +--java.util.AbstractMap
        |
        +--com.go.trove.util.WrappedCache
All Implemented Interfaces:
java.util.Map

public class WrappedCache
extends java.util.AbstractMap
implements java.util.Map

A Map that implements a write-through cache to another map. Two maps are supplied: one for caching and one for main storage. WrappedCache is not thread-safe and must be wrapped with Collections.synchronizedMap to be made thread-safe.

Version:
2 , 01/07/31
Author:
Brian S O'Neill

Inner classes inherited from class java.util.Map
java.util.Map.Entry
 
Constructor Summary
WrappedCache(java.util.Map cacheMap, java.util.Map backingMap)
           
 
Method Summary
 void clear()
          Clears both the cache and backing map.
 boolean containsKey(java.lang.Object key)
          Returns true if the cache contains the key or else if the backing map contains the key.
 boolean containsValue(java.lang.Object value)
          Returns true if the cache contains the value or else if the backing map contains the value.
 java.util.Set entrySet()
          Returns the entry set of the backing map.
 java.lang.Object get(java.lang.Object key)
          Returns the value from the cache, or if not found, the backing map.
 boolean isEmpty()
          Returns the empty status of the backing map.
 java.util.Set keySet()
          Returns the key set of the backing map.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Puts the entry into both the cache and backing map.
 java.lang.Object remove(java.lang.Object key)
          Removes the key from both the cache and backing map.
 int size()
          Returns the size of the backing map.
 java.util.Collection values()
          Returns the values of the backing map.
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, putAll, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode, putAll
 

Constructor Detail

WrappedCache

public WrappedCache(java.util.Map cacheMap,
                    java.util.Map backingMap)
Parameters:
cacheMap - the cache map should offer fast access, but it should automatically limit its maximum size
backingMap - the backingMap will be read from only if the requested entry isn't in the cache
Method Detail

size

public int size()
Returns the size of the backing map.
Specified by:
size in interface java.util.Map
Overrides:
size in class java.util.AbstractMap

isEmpty

public boolean isEmpty()
Returns the empty status of the backing map.
Specified by:
isEmpty in interface java.util.Map
Overrides:
isEmpty in class java.util.AbstractMap

containsKey

public boolean containsKey(java.lang.Object key)
Returns true if the cache contains the key or else if the backing map contains the key.
Specified by:
containsKey in interface java.util.Map
Overrides:
containsKey in class java.util.AbstractMap

containsValue

public boolean containsValue(java.lang.Object value)
Returns true if the cache contains the value or else if the backing map contains the value.
Specified by:
containsValue in interface java.util.Map
Overrides:
containsValue in class java.util.AbstractMap

get

public java.lang.Object get(java.lang.Object key)
Returns the value from the cache, or if not found, the backing map. If the backing map is accessed, the value is saved in the cache for future gets.
Specified by:
get in interface java.util.Map
Overrides:
get in class java.util.AbstractMap

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Puts the entry into both the cache and backing map. The old value in the backing map is returned.
Specified by:
put in interface java.util.Map
Overrides:
put in class java.util.AbstractMap

remove

public java.lang.Object remove(java.lang.Object key)
Removes the key from both the cache and backing map. The old value in the backing map is returned.
Specified by:
remove in interface java.util.Map
Overrides:
remove in class java.util.AbstractMap

clear

public void clear()
Clears both the cache and backing map.
Specified by:
clear in interface java.util.Map
Overrides:
clear in class java.util.AbstractMap

keySet

public java.util.Set keySet()
Returns the key set of the backing map.
Specified by:
keySet in interface java.util.Map
Overrides:
keySet in class java.util.AbstractMap

values

public java.util.Collection values()
Returns the values of the backing map.
Specified by:
values in interface java.util.Map
Overrides:
values in class java.util.AbstractMap

entrySet

public java.util.Set entrySet()
Returns the entry set of the backing map.
Specified by:
entrySet in interface java.util.Map
Overrides:
entrySet in class java.util.AbstractMap