org.apache.mina.util
Class LazyInitializedCacheMap<K,V>

java.lang.Object
  extended by org.apache.mina.util.LazyInitializedCacheMap<K,V>
All Implemented Interfaces:
java.util.Map<K,V>

public class LazyInitializedCacheMap<K,V>
extends java.lang.Object
implements java.util.Map<K,V>

This map is specially useful when reads are much more frequent than writes and if the cost of instantiating the values is high like allocating an IoBuffer for example. Based on the final implementation of Memoizer written by Brian Goetz and Tim Peierls. This implementation will return an UnsupportedOperationException on each method that is not intended to be called by user code for performance reasons.

Since:
MINA 2.0.0-M2
Author:
Apache MINA Project

Nested Class Summary
 class LazyInitializedCacheMap.NoopInitializer
          This class provides a noop LazyInitializer meaning it will return the same object it received when instantiated.
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Constructor Summary
LazyInitializedCacheMap()
          Default constructor.
LazyInitializedCacheMap(java.util.concurrent.ConcurrentHashMap<K,LazyInitializer<V>> map)
          This constructor allows to provide a fine tuned ConcurrentHashMap to stick with each special case the user needs.
 
Method Summary
 void clear()
          
 boolean containsKey(java.lang.Object key)
          
 boolean containsValue(java.lang.Object value)
           
 java.util.Set<java.util.Map.Entry<K,V>> entrySet()
           
 V get(java.lang.Object key)
          
 java.util.Collection<LazyInitializer<V>> getValues()
          
 boolean isEmpty()
          
 java.util.Set<K> keySet()
          
 V put(K key, V value)
          
 void putAll(java.util.Map<? extends K,? extends V> m)
          
 V putIfAbsent(K key, LazyInitializer<V> value)
          If the specified key is not already associated with a value, associate it with the given value.
 V remove(java.lang.Object key)
          
 int size()
          
 java.util.Collection<V> values()
           
 
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
 

Constructor Detail

LazyInitializedCacheMap

public LazyInitializedCacheMap()
Default constructor. Uses the default parameters to initialize its internal ConcurrentHashMap.


LazyInitializedCacheMap

public LazyInitializedCacheMap(java.util.concurrent.ConcurrentHashMap<K,LazyInitializer<V>> map)
This constructor allows to provide a fine tuned ConcurrentHashMap to stick with each special case the user needs.

Method Detail

get

public V get(java.lang.Object key)

Specified by:
get in interface java.util.Map<K,V>

remove

public V remove(java.lang.Object key)

Specified by:
remove in interface java.util.Map<K,V>

putIfAbsent

public V putIfAbsent(K key,
                     LazyInitializer<V> value)
If the specified key is not already associated with a value, associate it with the given value. This is equivalent to
   if (!map.containsKey(key))
       return map.put(key, value);
   else
       return map.get(key);
except that the action is performed atomically.

Parameters:
key - key with which the specified value is to be associated
value - a lazy initialized value object.
Returns:
the previous value associated with the specified key, or null if there was no mapping for the key

put

public V put(K key,
             V value)

Specified by:
put in interface java.util.Map<K,V>

containsValue

public boolean containsValue(java.lang.Object value)
Specified by:
containsValue in interface java.util.Map<K,V>
Throws:
{@link - UnsupportedOperationException} as this method would imply performance drops.

values

public java.util.Collection<V> values()
Specified by:
values in interface java.util.Map<K,V>
Throws:
{@link - UnsupportedOperationException} as this method would imply performance drops.

entrySet

public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
Specified by:
entrySet in interface java.util.Map<K,V>
Throws:
{@link - UnsupportedOperationException} as this method would imply performance drops.

putAll

public void putAll(java.util.Map<? extends K,? extends V> m)

Specified by:
putAll in interface java.util.Map<K,V>

getValues

public java.util.Collection<LazyInitializer<V>> getValues()


clear

public void clear()

Specified by:
clear in interface java.util.Map<K,V>

containsKey

public boolean containsKey(java.lang.Object key)

Specified by:
containsKey in interface java.util.Map<K,V>

isEmpty

public boolean isEmpty()

Specified by:
isEmpty in interface java.util.Map<K,V>

keySet

public java.util.Set<K> keySet()

Specified by:
keySet in interface java.util.Map<K,V>

size

public int size()

Specified by:
size in interface java.util.Map<K,V>


Copyright © 2004-2011 Apache MINA Project. All Rights Reserved.