org.salespointframework.core.data
Class PersistentMap<K,V>

java.lang.Object
  extended by org.salespointframework.core.data.PersistentMap<K,V>
Type Parameters:
K - generic key type
V - generic value type
All Implemented Interfaces:
java.util.Map<K,V>, DatasourceChangeListener, DatasourceOnChangeListener, ExternalModificationListener

public class PersistentMap<K,V>
extends java.lang.Object
implements java.util.Map<K,V>, DatasourceChangeListener, DatasourceOnChangeListener, ExternalModificationListener

A PersistentMap is a Map that persists its key-value pairs to the data source. most functions are thread safe. make sure that key type is a basic one. make sure that object you put in here are persistable via PersistenceManager.persist(Object). It is Possible to use Lists as value type, but you should avoid this. The map uses an internal cache for performance reasons. Each map needs a unique id which is composed of host and dbid

Since:
4.0
Author:
Thomas Kissinger

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Field Summary
private  java.util.Map<K,V> cache
          cached map items
private  long cacheLifetime
          cache life time
private  java.util.Map<K,java.lang.Long> cacheTS
          timestamps of each cache item
private  java.lang.Class ck
          class of keys
private  java.lang.Class cv
          class of values
private  java.lang.String dbid
          id of map
static long DEFAULT_CACHE_LIFETIME
          default cache life time
private  java.util.List<ExternalModificationListener> externalModificationListeners
          list of ExternalModificationListeners
private  boolean forceMultiKey
          allow multiple keys of same value. if true, the map will return Lists.
private  java.lang.Object host
          hosting object. mostly the object in which this map us declared
private  long largeLoadTS
          timestamp of last large load. applies only if PersistenceManager.isPerformanceMode()
private  java.lang.Object lock
          locking object
private  java.lang.Object recoveryContext
          recovery context, passed to objects on recovery
private  int size
          cached size
private  long sizeTS
          timestamp if cahced size
 
Constructor Summary
PersistentMap(java.lang.Class k, java.lang.Class v)
          default constructor.
PersistentMap(java.lang.Object host, java.lang.String id, java.lang.Class k, java.lang.Class v)
          default constructor.
PersistentMap(java.lang.Object host, java.lang.String id, java.lang.Class k, java.lang.Class v, long cacheLifetime, java.lang.Object recoveryContext, boolean forceMultiKey)
          constructor.
PersistentMap(java.lang.Object host, java.lang.String id, java.lang.Class k, java.lang.Class v, java.lang.Object recoveryContext)
          constructor.
PersistentMap(java.lang.Object host, java.lang.String id, java.lang.Class k, java.lang.Class v, java.lang.Object recoveryContext, boolean forceMultiKey)
          constructor.
 
Method Summary
 boolean addExternalModificationListener(ExternalModificationListener eml)
          add ExternalModificationListener
 void addList(K key, java.util.List<java.lang.Object> list)
          add list to cache
 void checkLargeLoad()
          check if cache is out-dated.
 void clear()
           
 boolean containsKey(java.lang.Object key)
           
 boolean containsValue(java.lang.Object value)
          this method is not implemented
 void DatasourceChanged(boolean pumpUp)
          invalidate caches and pump up the event
 void DatasourceOnChange()
          pump up datasource on change event
 java.util.Set<java.util.Map.Entry<K,V>> entrySet()
          this method is not implemented
 void externalModificationOccurred()
          invalidate caches and pump up the event
 V get(java.lang.Object key)
           
 java.util.List<java.lang.Object> getList(java.lang.Object key)
          get list
 java.lang.String getListId()
          get encoded list id
 java.lang.String getListId(boolean raw)
          get list id
 void invalidateCaches()
          invalidate all caches
 boolean isEmpty()
           
 java.util.Set<K> keySet()
           
 V put(K key, V value)
           
 void putAll(java.util.Map<? extends K,? extends V> m)
           
 V remove(java.lang.Object key)
           
 void removeExternalModificationListener(ExternalModificationListener eml)
          remove ExternalModificationListener
 void setCacheLifetime(long cacheLifetime)
          set cacheLifetime
 void setRecoveryContext(java.lang.Object recoveryContext)
          set recoveryContext
 int size()
           
 java.lang.String toString()
           
 void unsubscribe()
          unsubscribe map from PersistenceManager
 java.util.Collection<V> values()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Field Detail

DEFAULT_CACHE_LIFETIME

public static long DEFAULT_CACHE_LIFETIME
default cache life time


dbid

private java.lang.String dbid
id of map


ck

private java.lang.Class ck
class of keys


cv

private java.lang.Class cv
class of values


recoveryContext

private java.lang.Object recoveryContext
recovery context, passed to objects on recovery


host

private java.lang.Object host
hosting object. mostly the object in which this map us declared


forceMultiKey

private boolean forceMultiKey
allow multiple keys of same value. if true, the map will return Lists.


lock

private java.lang.Object lock
locking object


cacheLifetime

private long cacheLifetime
cache life time


size

private int size
cached size


sizeTS

private long sizeTS
timestamp if cahced size


cache

private java.util.Map<K,V> cache
cached map items


cacheTS

private java.util.Map<K,java.lang.Long> cacheTS
timestamps of each cache item


largeLoadTS

private long largeLoadTS
timestamp of last large load. applies only if PersistenceManager.isPerformanceMode()


externalModificationListeners

private java.util.List<ExternalModificationListener> externalModificationListeners
list of ExternalModificationListeners

Constructor Detail

PersistentMap

public PersistentMap(java.lang.Class k,
                     java.lang.Class v)
default constructor. because of java language weakness you need to pass types of both generic arguments The id is generated by random.

Parameters:
k - ck
v - cv

PersistentMap

public PersistentMap(java.lang.Object host,
                     java.lang.String id,
                     java.lang.Class k,
                     java.lang.Class v)
default constructor. because of java language weakness you need to pass types of both generic arguments .

Parameters:
host - host
id - dbid
k - ck
v - cv

PersistentMap

public PersistentMap(java.lang.Object host,
                     java.lang.String id,
                     java.lang.Class k,
                     java.lang.Class v,
                     java.lang.Object recoveryContext)
constructor. because of java language weakness you need to pass types of both generic arguments .

Parameters:
host - host
id - dbid
k - ck
v - cv
recoveryContext - recoveryContext

PersistentMap

public PersistentMap(java.lang.Object host,
                     java.lang.String id,
                     java.lang.Class k,
                     java.lang.Class v,
                     java.lang.Object recoveryContext,
                     boolean forceMultiKey)
constructor. because of java language weakness you need to pass types of both generic arguments Warning: set forceMultiKey only to true, if you know what you are doing! .

Parameters:
host - host
id - dbid
k - ck
v - cv
recoveryContext - recoveryContext
forceMultiKey - forceMultiKey

PersistentMap

public PersistentMap(java.lang.Object host,
                     java.lang.String id,
                     java.lang.Class k,
                     java.lang.Class v,
                     long cacheLifetime,
                     java.lang.Object recoveryContext,
                     boolean forceMultiKey)
constructor. because of java language weakness you need to pass types of both generic arguments Warning: set forceMultiKey only to true, if you know what you are doing! .

Parameters:
host - host
id - dbid
k - ck
v - cv
recoveryContext - recoveryContext
forceMultiKey - forceMultiKey
cacheLifetime - cacheLifetime
Method Detail

setCacheLifetime

public void setCacheLifetime(long cacheLifetime)
set cacheLifetime

Parameters:
cacheLifetime -

setRecoveryContext

public void setRecoveryContext(java.lang.Object recoveryContext)
set recoveryContext

Parameters:
recoveryContext -

getListId

public java.lang.String getListId()
get encoded list id

Returns:

getListId

public java.lang.String getListId(boolean raw)
get list id

Parameters:
raw - if true, list id is not encoded
Returns:

clear

public void clear()
Specified by:
clear in interface java.util.Map<K,V>
See Also:
Map.clear()

invalidateCaches

public void invalidateCaches()
invalidate all caches


containsKey

public boolean containsKey(java.lang.Object key)
Specified by:
containsKey in interface java.util.Map<K,V>
See Also:
Map.containsKey(Object)

containsValue

public boolean containsValue(java.lang.Object value)
this method is not implemented

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

entrySet

public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
this method is not implemented

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

checkLargeLoad

public void checkLargeLoad()
check if cache is out-dated.


addList

public void addList(K key,
                    java.util.List<java.lang.Object> list)
add list to cache

Parameters:
key -
list -

getList

public java.util.List<java.lang.Object> getList(java.lang.Object key)
get list

Parameters:
key -
Returns:

get

public V get(java.lang.Object key)
Specified by:
get in interface java.util.Map<K,V>
See Also:
Map.get(Object)

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface java.util.Map<K,V>
See Also:
Map.isEmpty()

keySet

public java.util.Set<K> keySet()
Specified by:
keySet in interface java.util.Map<K,V>
See Also:
Map.keySet()

put

public V put(K key,
             V value)
Specified by:
put in interface java.util.Map<K,V>
See Also:
Map.put(Object, Object)

putAll

public void putAll(java.util.Map<? extends K,? extends V> m)
Specified by:
putAll in interface java.util.Map<K,V>
See Also:
Map.putAll(Map)

remove

public V remove(java.lang.Object key)
Specified by:
remove in interface java.util.Map<K,V>
See Also:
Map.remove(Object)

size

public int size()
Specified by:
size in interface java.util.Map<K,V>
See Also:
Map.size()

values

public java.util.Collection<V> values()
Specified by:
values in interface java.util.Map<K,V>
See Also:
Map.values()

DatasourceChanged

public void DatasourceChanged(boolean pumpUp)
invalidate caches and pump up the event

Specified by:
DatasourceChanged in interface DatasourceChangeListener
Parameters:
pumpUp - true if the event should be pumped up to hosts, because this event is mostly triggered twice. first time to invalidate caches. second time for load new data

unsubscribe

public void unsubscribe()
unsubscribe map from PersistenceManager


DatasourceOnChange

public void DatasourceOnChange()
pump up datasource on change event

Specified by:
DatasourceOnChange in interface DatasourceOnChangeListener

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

addExternalModificationListener

public boolean addExternalModificationListener(ExternalModificationListener eml)
add ExternalModificationListener

Parameters:
eml - the listener
Returns:
true on success

removeExternalModificationListener

public void removeExternalModificationListener(ExternalModificationListener eml)
remove ExternalModificationListener

Parameters:
eml - the listener

externalModificationOccurred

public void externalModificationOccurred()
invalidate caches and pump up the event

Specified by:
externalModificationOccurred in interface ExternalModificationListener