org.fuin.utils4j
Class ChangeTrackingMap

java.lang.Object
  extended by org.fuin.utils4j.ChangeTrackingMap
All Implemented Interfaces:
java.util.Map, Taggable

public class ChangeTrackingMap
extends java.lang.Object
implements java.util.Map, Taggable

A wrapper for maps that keeps track of all changes made to the map since construction. Only adding, replacing or deleting elements is tracked (not changes inside the objects). It's also possible to revert all changes.


Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Constructor Summary
ChangeTrackingMap(java.util.Map map)
          Constructor with covered map.
 
Method Summary
 void clear()
          
 boolean containsKey(java.lang.Object key)
          
 boolean containsValue(java.lang.Object value)
          
 java.util.Set entrySet()
          
 java.lang.Object get(java.lang.Object key)
          
 java.util.Map getAdded()
          Returns added elements.
 java.util.Map getChanged()
          Returns changed elements.
 java.util.Map getRemoved()
          Returns removed elements.
 boolean hasChangedSinceTagging()
          Returns if the content of the object has changed since setting the tag.
 boolean isChanged()
          Returns if the list has changed.
 boolean isEmpty()
          
 boolean isTagged()
          Returns if the object is currently tagged.
 java.util.Set keySet()
          
 java.lang.Object put(java.lang.Object key, java.lang.Object newValue)
          
 void putAll(java.util.Map newMap)
          
 java.lang.Object remove(java.lang.Object key)
          
 void revert()
          Roll back all changes made since construction.
 void revertToTag()
          Reverts all changes made since setting the tag and clears internal state.
 int size()
          
 void tag()
          Start memorizing changes.
 java.lang.String toString()
          
 void untag()
          Stop memorizing changes and clear internal state.
 java.util.Collection 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
 

Constructor Detail

ChangeTrackingMap

public ChangeTrackingMap(java.util.Map map)
Constructor with covered map. The map is tagged at construction time - This means isTagged() will return true without calling tag() first. If this behavior is not wanted you can call untag() after constructing the map.

Parameters:
map - Wrapped map - Be aware that this map will be changed by this class. There is no internal copy of the map - The reference itself is used.
Method Detail

isChanged

public final boolean isChanged()
Returns if the list has changed. If the map is not in tag mode (this means isTagged() returns true) this method will always return false.

Returns:
If elements have been added or deleted true else false.

getRemoved

public final java.util.Map getRemoved()
Returns removed elements. If the map is not in tag mode (this means isTagged() returns true) this method will always return an empty map.

Returns:
Elements that have been deleted since construction of this instance - Unmodifiable map!

getChanged

public final java.util.Map getChanged()
Returns changed elements. If the map is not in tag mode (this means isTagged() returns true) this method will always return an empty map.

Returns:
Elements that have been changed since construction of this instance - Unmodifiable map!

revert

public final void revert()
Roll back all changes made since construction. This is the same function ad revertToTag(). If the map is not in tag mode ( this means isTagged() returns true) this method will do nothing.


getAdded

public final java.util.Map getAdded()
Returns added elements. If the map is not in tag mode (this means isTagged() returns true) this method will always return an empty map.

Returns:
Elements that have been added since construction of this instance - Unmodifiable map!

clear

public final void clear()

Specified by:
clear in interface java.util.Map

containsKey

public final boolean containsKey(java.lang.Object key)

Specified by:
containsKey in interface java.util.Map

containsValue

public final boolean containsValue(java.lang.Object value)

Specified by:
containsValue in interface java.util.Map

entrySet

public final java.util.Set entrySet()

Specified by:
entrySet in interface java.util.Map

get

public final java.lang.Object get(java.lang.Object key)

Specified by:
get in interface java.util.Map

isEmpty

public final boolean isEmpty()

Specified by:
isEmpty in interface java.util.Map

keySet

public final java.util.Set keySet()

Specified by:
keySet in interface java.util.Map

put

public final java.lang.Object put(java.lang.Object key,
                                  java.lang.Object newValue)

Specified by:
put in interface java.util.Map

putAll

public final void putAll(java.util.Map newMap)

Specified by:
putAll in interface java.util.Map

remove

public final java.lang.Object remove(java.lang.Object key)

Specified by:
remove in interface java.util.Map

size

public final int size()

Specified by:
size in interface java.util.Map

values

public final java.util.Collection values()

Specified by:
values in interface java.util.Map

toString

public final java.lang.String toString()

Overrides:
toString in class java.lang.Object

hasChangedSinceTagging

public final boolean hasChangedSinceTagging()
Returns if the content of the object has changed since setting the tag.

Specified by:
hasChangedSinceTagging in interface Taggable
Returns:
If something has changed true else false.

isTagged

public final boolean isTagged()
Returns if the object is currently tagged.

Specified by:
isTagged in interface Taggable
Returns:
If a tag is set true else false.

revertToTag

public final void revertToTag()
Reverts all changes made since setting the tag and clears internal state.

Specified by:
revertToTag in interface Taggable

tag

public final void tag()
Start memorizing changes.

Specified by:
tag in interface Taggable

untag

public final void untag()
Stop memorizing changes and clear internal state. It's not reverting any change! It's simply a "forget all changes".

Specified by:
untag in interface Taggable


Copyright © 2009 Future Invent Informationsmanagement GmbH. All Rights Reserved.