|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectmirrormap.collections.NotifyingMap<K,V>
K - The key type for the mapV - The value type for the mappublic class NotifyingMap<K,V>
A NotifyingMap wraps a Map and notifies IMapListener
instances when entries in the underlying map are added, updated or removed.
The IMapListener instances are registered with the NotifyingMap via
the addListener(IMapListener) method. This is the only
activity that requires a NotifyingMap reference, all other activities can be
handled via the Map interface.
In general, the best practice is to create the underlying map, construct the notifying map with the underlying map and then use the notifying map as the map reference. Mutating the underlying map outside of the notifying map will lead to inconsistent behaviour.
This implementation is thread safe. All methods are synchronized.
However the usual semantics for iterating over a HashMap apply to
this (i.e. synchronized on the map during iteration to prevent
ConcurrentModificationException). In particular see the note in the
put(Object, Object) method.
| Nested Class Summary |
|---|
| Nested classes/interfaces inherited from interface java.util.Map |
|---|
Map.Entry<K,V> |
| Constructor Summary | |
|---|---|
NotifyingMap(Map<K,V> data)
Construct the NotifyingMap with the underlying data map. |
|
| Method Summary | |
|---|---|
boolean |
addListener(IMapListener<K,V> listener)
Add a listener that will be notified when entries in the underlying Map are added, updated or removed. |
void |
clear()
Clear the underlying map. |
boolean |
containsKey(Object key)
|
boolean |
containsValue(Object value)
|
Set<Map.Entry<K,V>> |
entrySet()
|
boolean |
equals(Object o)
|
V |
get(Object key)
|
int |
hashCode()
|
boolean |
isEmpty()
|
Set<K> |
keySet()
|
V |
put(K key,
V value)
If the mapping is new, this will: call IMapListener.willAdd(Object, Object) on all listeners
put the mapping call IMapListener.didAdd(Object, Object)
on all listeners
Otherwise this will:
call IMapListener.willUpdate(Object, Object, Object) on all
listeners put the mapping (overwriting) call
IMapListener.didUpdate(Object, Object, Object) on all listeners
|
void |
putAll(Map<? extends K,? extends V> t)
Calls put(Object, Object) for each entry added from the argument
map. |
V |
remove(Object key)
If the mapping exists, this will: call IMapListener.willRemove(Object) on all listeners
remove the mapping call
IMapListener.didRemove(Object, Object) on all listeners
|
boolean |
removeListener(IMapListener<?,?> listener)
Remove the listener from this NotifyingMap instance. |
int |
size()
|
String |
toString()
|
Collection<V> |
values()
|
| Methods inherited from class java.lang.Object |
|---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
public NotifyingMap(Map<K,V> data)
data - the map that holds the actual data| Method Detail |
|---|
public boolean addListener(IMapListener<K,V> listener)
Map are added, updated or removed.
On adding the listener, it will be notified with the current content of
the underlying map. This will be done by calling the
IMapListener.didAdd(Object, Object) method of the listener.
Note: there may be event interleaving if an update occurs whilst
the existing map content is being notified to the newly added listener.
This is an idempotent operation and is synchronized to ensure this.
listener - the listener to add
true if the listener was added, false
if it already contained the listenerpublic boolean removeListener(IMapListener<?,?> listener)
listener - the listener to remove
public void clear()
clear in interface Map<K,V>Map.clear()public boolean containsKey(Object key)
containsKey in interface Map<K,V>Map.containsKey(java.lang.Object)public boolean containsValue(Object value)
containsValue in interface Map<K,V>Map.containsValue(java.lang.Object)public Set<Map.Entry<K,V>> entrySet()
entrySet in interface Map<K,V>Set of Entry instances contained in the
underlying Map. Removing entries from the set via the
Iterator returned from Set.iterator() will
trigger will/did remove events.Map.entrySet()public boolean equals(Object o)
equals in interface Map<K,V>equals in class ObjectMap.equals(java.lang.Object)public V get(Object key)
get in interface Map<K,V>Map.get(java.lang.Object)public int hashCode()
hashCode in interface Map<K,V>hashCode in class ObjectMap.hashCode()public boolean isEmpty()
isEmpty in interface Map<K,V>Map.isEmpty()public Set<K> keySet()
keySet in interface Map<K,V>Set of the keys contained in the underlying Map
. Removing entries from the set via the Iterator returned
from Set.iterator() will trigger will/did remove events.Map.keySet()
public V put(K key,
V value)
IMapListener.willAdd(Object, Object) on all listeners
IMapListener.didAdd(Object, Object)
on all listeners
IMapListener.willUpdate(Object, Object, Object) on all
listeners IMapListener.didUpdate(Object, Object, Object) on all listeners
Note: this method calls Map.get(Object) followed by
Map.put(Object, Object) on the underlying map. For this reason
the class is required to be thread safe.
put in interface Map<K,V>Map.put(java.lang.Object, java.lang.Object)public void putAll(Map<? extends K,? extends V> t)
put(Object, Object) for each entry added from the argument
map.
putAll in interface Map<K,V>Map.putAll(java.util.Map)public V remove(Object key)
IMapListener.willRemove(Object) on all listeners IMapListener.didRemove(Object, Object) on all listeners
remove in interface Map<K,V>Map.remove(java.lang.Object)public int size()
size in interface Map<K,V>Map.size()public Collection<V> values()
values in interface Map<K,V>Collection of the values contained in the underlying
Map. Removing entries from the set via the
Iterator returned from Collection.iterator() will
trigger will/did remove events.Map.values()public String toString()
toString in class Object
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||