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

java.lang.Object
  extended by sk.baka.tools.concurrent.InMemoryLockingStorage<K,V>
Type Parameters:
K - the key type.
V - the object type.
All Implemented Interfaces:
ILockingStorage<K,V>

@ThreadSafe
public final class InMemoryLockingStorage<K,V>
extends Object
implements ILockingStorage<K,V>

This implementation uses a simple and efficient in-memory object storage.

Author:
Martin Vysny

Constructor Summary
InMemoryLockingStorage(IValueProducer<? super K,? extends V> producer)
          Creates a new locking storage.
 
Method Summary
 V lockAndGet(K key)
          Performs an atomic lock on given key and atomically retrieves a value stored under given ID.
 void unlock(K key)
          Atomically unlocks a lock on given key.
 void unlockAndRemove(K key)
          Atomically unlocks a lock with given key and deletes the value object stored under the key.
 List<V> values()
          Returns a snapshot of all values currently stored in the storage.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

InMemoryLockingStorage

public InMemoryLockingStorage(IValueProducer<? super K,? extends V> producer)
Creates a new locking storage.

Parameters:
producer - produces instances of objects which will be available as values.
Method Detail

lockAndGet

public V lockAndGet(K key)
Description copied from interface: ILockingStorage
Performs an atomic lock on given key and atomically retrieves a value stored under given ID. The value is constructed if it is not yet stored in the storage.

Specified by:
lockAndGet in interface ILockingStorage<K,V>
Parameters:
key - the key, must not be null. The key must be suitable for insertion into a Map: for example, the Object.equals(java.lang.Object) and Object.hashCode() must be implemented correctly.
Returns:
an object instance.

unlock

public void unlock(K key)
Description copied from interface: ILockingStorage
Atomically unlocks a lock on given key. The value object is left in the storage.

Specified by:
unlock in interface ILockingStorage<K,V>
Parameters:
key - the key to unlock, must not be null.

unlockAndRemove

public void unlockAndRemove(K key)
Description copied from interface: ILockingStorage
Atomically unlocks a lock with given key and deletes the value object stored under the key.

Specified by:
unlockAndRemove in interface ILockingStorage<K,V>
Parameters:
key - the key, must not be null.

values

public List<V> values()
Returns a snapshot of all values currently stored in the storage. The returned list is not a live snapshot and may already be obsolete. Only for informational purposes. Marker mappings are filtered out.

Returns:
all values.


Copyright © 2009-2010. All Rights Reserved.