sk.baka.tools.concurrent
Class EffectivePutMap<K,V>

java.lang.Object
  extended by sk.baka.tools.concurrent.EffectivePutMap<K,V>
Type Parameters:
K - the map key
V - the map value

@ThreadSafe
public final class EffectivePutMap<K,V>
extends Object

Adds a possibility to effectively put a key-value mapping to a ConcurrentMap if such mapping is missing. The main difference between this map and ConcurrentMap.putIfAbsent(java.lang.Object, java.lang.Object) is that this object uses a producer to produce values. This producer is expected to be used effectively - the value production is performed only when the object is really added to the map. Note that in several cases the object may be produced but not actually stored in the map.

Note that the original map may randomly contain the marker items as values - you'll have to skip such entries manually, e.g. when iterating the map contents.


Field Summary
 ConcurrentMap<K,V> map
          The map itself.
 
Constructor Summary
EffectivePutMap(ConcurrentMap<K,V> map, V marker, IValueProducer<? super K,? extends V> producer)
          Constructor.
 
Method Summary
 void clear()
           
 Map<K,V> copy()
          Returns a list of all map entries with the marker values filtered out.
 V get(K key)
          Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
 V putIfAbsent(K key)
          Similar to ConcurrentMap.putIfAbsent(java.lang.Object, java.lang.Object) except that it uses #produce() to produce value objects.
 V remove(K key)
          Removes the mapping for a key from this map if it is present (optional operation).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

map

public final ConcurrentMap<K,V> map
The map itself.

Constructor Detail

EffectivePutMap

public EffectivePutMap(ConcurrentMap<K,V> map,
                       V marker,
                       IValueProducer<? super K,? extends V> producer)
Constructor.

Parameters:
map - the map to use
marker - a "marker" object, used in utility methods. The marker must not be null and must not be equal to any other instances of V.
producer - the value producer, must not be null. Must not produce null values.
Method Detail

putIfAbsent

public V putIfAbsent(K key)
Similar to ConcurrentMap.putIfAbsent(java.lang.Object, java.lang.Object) except that it uses #produce() to produce value objects. #produce() is called only when the value object is really added to the map.

Parameters:
key - the map key
Returns:
a value currently mapped to the key.

copy

public Map<K,V> copy()
Returns a list of all map entries with the marker values filtered out.

Returns:
the entry set snapshot.

clear

public void clear()

get

public V get(K key)
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Parameters:
key - the key whose associated value is to be returned
Returns:
the value to which the specified key is mapped, or null if this map contains no mapping for the key. Never returns the marker.

remove

public V remove(K key)
Removes the mapping for a key from this map if it is present (optional operation). More formally, if this map contains a mapping from key k to value v such that (key==null ? k==null : key.equals(k)), that mapping is removed. (The map can contain at most one such mapping.)

Returns the value to which this map previously associated the key, or null if the map contained no mapping for the key.

If this map permits null values, then a return value of null does not necessarily indicate that the map contained no mapping for the key; it's also possible that the map explicitly mapped the key to null.

The map will not contain a mapping for the specified key once the call returns.

Parameters:
key - key whose mapping is to be removed from the map
Returns:
the previous value associated with key, or null if there was no mapping for key. Returns null instead of the marker element.
Throws:
UnsupportedOperationException - if the remove operation is not supported by this map
NullPointerException - if the specified key is null and this map does not permit null keys (optional)


Copyright © 2009-2010. All Rights Reserved.