Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.apache.myfaces.util
Class BiLevelCacheMap  view BiLevelCacheMap download BiLevelCacheMap.java

java.lang.Object
  extended byorg.apache.myfaces.util.BiLevelCacheMap
All Implemented Interfaces:
java.util.Map

public abstract class BiLevelCacheMap
extends java.lang.Object
implements java.util.Map

A bi-level cache based on HashMap for caching objects with minimal sychronization overhead. The limitation is that remove() is very expensive.

Access to L1 map is not sychronized, to L2 map is synchronized. New values are first stored in L2. Once there have been more that a specified mumber of misses on L1, L1 and L2 maps are merged and the new map assigned to L1 and L2 cleared.

IMPORTANT:entrySet(), keySet(), and values() return unmodifiable snapshot collections.

Version:
$Revision: 169655 $ $Date: 2005-05-11 12:45:06 -0400 (Wed, 11 May 2005) $

Nested Class Summary
 
Nested classes inherited from class java.util.Map
java.util.Map.Entry
 
Field Summary
protected  java.util.Map _cacheL1
          To preinitialize _cacheL1 with default values use an initialization block
private  java.util.Map _cacheL2
          Must be final because it is used for synchronization
private  int _mergeThreshold
           
private  int _missCount
           
private static int INITIAL_SIZE_L1
           
 
Constructor Summary
BiLevelCacheMap(int mergeThreshold)
           
 
Method Summary
 void clear()
          Remove all entries from this Map (optional operation).
 boolean containsKey(java.lang.Object key)
          Returns true if this contains a mapping for the given key.
 boolean containsValue(java.lang.Object value)
          Returns true if this contains at least one mapping with the given value.
 java.util.Set entrySet()
          Returns a set view of the mappings in this Map.
 java.lang.Object get(java.lang.Object key)
          Returns the value mapped by the given key.
 boolean isEmpty()
          Returns true if the map contains no mappings.
 java.util.Set keySet()
          Returns a set view of the keys in this Map.
private  void merge(java.util.Map map)
           
private  void mergeIfL2NotEmpty()
           
private  void mergeIfNeeded()
           
protected abstract  java.lang.Object newInstance(java.lang.Object key)
          Subclasses must implement to have automatic creation of new instances or alternatively can use put to add new items to the cache.
Implementing this method is prefered to guarantee that there will be only one instance per key ever created.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          If key is already in cacheL1, the new value will show with a delay, since merge L2->L1 may not happen immediately.
 void putAll(java.util.Map map)
          Copies all entries of the given map to this one (optional operation).
 java.lang.Object remove(java.lang.Object key)
          This operation is very expensive.
 int size()
          Returns the number of key-value mappings in the map.
 java.util.Collection values()
          Returns a collection (or bag) view of the values in this Map.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Field Detail

INITIAL_SIZE_L1

private static final int INITIAL_SIZE_L1
See Also:
Constant Field Values

_cacheL1

protected java.util.Map _cacheL1
To preinitialize _cacheL1 with default values use an initialization block


_cacheL2

private final java.util.Map _cacheL2
Must be final because it is used for synchronization


_mergeThreshold

private final int _mergeThreshold

_missCount

private int _missCount
Constructor Detail

BiLevelCacheMap

public BiLevelCacheMap(int mergeThreshold)
Method Detail

isEmpty

public boolean isEmpty()
Description copied from interface: java.util.Map
Returns true if the map contains no mappings.

Specified by:
isEmpty in interface java.util.Map

clear

public void clear()
Description copied from interface: java.util.Map
Remove all entries from this Map (optional operation).

Specified by:
clear in interface java.util.Map

containsKey

public boolean containsKey(java.lang.Object key)
Description copied from interface: java.util.Map
Returns true if this contains a mapping for the given key.

Specified by:
containsKey in interface java.util.Map

containsValue

public boolean containsValue(java.lang.Object value)
Description copied from interface: java.util.Map
Returns true if this contains at least one mapping with the given value. In other words, returns true if a value v exists where (value == null ? v == null : value.equals(v)). This usually requires linear time.

Specified by:
containsValue in interface java.util.Map

entrySet

public java.util.Set entrySet()
Description copied from interface: java.util.Map
Returns a set view of the mappings in this Map. Each element in the set is a Map.Entry. The set is backed by the map, so that changes in one show up in the other. Modifications made while an iterator is in progress cause undefined behavior. If the set supports removal, these methods remove the underlying mapping from the map: Iterator.remove, Set.remove, removeAll, retainAll, and clear. Element addition, via add or addAll, is not supported via this set.

Specified by:
entrySet in interface java.util.Map

get

public java.lang.Object get(java.lang.Object key)
Description copied from interface: java.util.Map
Returns the value mapped by the given key. Returns null if there is no mapping. However, in Maps that accept null values, you must rely on containsKey to determine if a mapping exists.

Specified by:
get in interface java.util.Map

keySet

public java.util.Set keySet()
Description copied from interface: java.util.Map
Returns a set view of the keys in this Map. The set is backed by the map, so that changes in one show up in the other. Modifications made while an iterator is in progress cause undefined behavior. If the set supports removal, these methods remove the underlying mapping from the map: Iterator.remove, Set.remove, removeAll, retainAll, and clear. Element addition, via add or addAll, is not supported via this set.

Specified by:
keySet in interface java.util.Map

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
If key is already in cacheL1, the new value will show with a delay, since merge L2->L1 may not happen immediately. To force the merge sooner, call size().

Specified by:
put in interface java.util.Map

putAll

public void putAll(java.util.Map map)
Description copied from interface: java.util.Map
Copies all entries of the given map to this one (optional operation). If the map already contains a key, its value is replaced.

Specified by:
putAll in interface java.util.Map

remove

public java.lang.Object remove(java.lang.Object key)
This operation is very expensive. A full copy of the Map is created

Specified by:
remove in interface java.util.Map

size

public int size()
Description copied from interface: java.util.Map
Returns the number of key-value mappings in the map. If there are more than Integer.MAX_VALUE mappings, return Integer.MAX_VALUE.

Specified by:
size in interface java.util.Map

values

public java.util.Collection values()
Description copied from interface: java.util.Map
Returns a collection (or bag) view of the values in this Map. The collection is backed by the map, so that changes in one show up in the other. Modifications made while an iterator is in progress cause undefined behavior. If the collection supports removal, these methods remove the underlying mapping from the map: Iterator.remove, Collection.remove, removeAll, retainAll, and clear. Element addition, via add or addAll, is not supported via this collection.

Specified by:
values in interface java.util.Map

mergeIfL2NotEmpty

private void mergeIfL2NotEmpty()

mergeIfNeeded

private void mergeIfNeeded()

merge

private void merge(java.util.Map map)

newInstance

protected abstract java.lang.Object newInstance(java.lang.Object key)
Subclasses must implement to have automatic creation of new instances or alternatively can use put to add new items to the cache.
Implementing this method is prefered to guarantee that there will be only one instance per key ever created. Calling put() to add items in a multi- threaded situation will require external synchronization to prevent two instances for the same key, which defeats the purpose of this cache (put() is useful when initialization is done during startup and items are not added during execution or when (temporarily) having possibly two or more instances of the same key is not of concern).