org.fuin.utils4j
Class ChangeTrackingUniqueList

java.lang.Object
  extended by org.fuin.utils4j.ChangeTrackingUniqueList
All Implemented Interfaces:
Iterable, Collection, List, Taggable

public class ChangeTrackingUniqueList
extends Object
implements List, Taggable

A wrapper for lists that keeps track of all changes made to the list since construction. Only adding, replacing or deleting elements is tracked (not changes inside the objects). Duplicates elements are not allowed for the list - This is like a Set but at the same time ordered like a List . It's also possible to revert all changes.


Constructor Summary
ChangeTrackingUniqueList(List list)
          Constructor with covered list.
 
Method Summary
 void add(int index, Object o)
          
 boolean add(Object o)
          
 boolean addAll(Collection c)
          
 boolean addAll(int index, Collection c)
          
 void clear()
          
 boolean contains(Object o)
          
 boolean containsAll(Collection c)
          
 Object get(int index)
          
 List getAdded()
          Returns added elements.
 List getDeleted()
          Returns deleted elements.
 boolean hasChangedSinceTagging()
          Returns if the content of the object has changed since setting the tag.
 int indexOf(Object o)
          
 boolean isChanged()
          Returns if the list has changed.
 boolean isEmpty()
          
 boolean isTagged()
          Returns if the object is currently tagged.
 Iterator iterator()
          
 int lastIndexOf(Object o)
          
 ListIterator listIterator()
          
 ListIterator listIterator(int index)
          
 Object remove(int index)
          
 boolean remove(Object o)
          
 boolean removeAll(Collection c)
          
 boolean retainAll(Collection c)
          
 void revert()
          Roll back all changes made since construction.
 void revertToTag()
          Reverts all changes made since setting the tag and clears internal state.
 Object set(int index, Object o)
          
 int size()
          
 List subList(int fromIndex, int toIndex)
          
 void tag()
          Start memorizing changes.
 Object[] toArray()
          
 Object[] toArray(Object[] a)
          
 String toString()
          
 void untag()
          Stop memorizing changes and clear internal state.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
equals, hashCode
 

Constructor Detail

ChangeTrackingUniqueList

public ChangeTrackingUniqueList(List list)
Constructor with covered list. The list 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 list.

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

isChanged

public final boolean isChanged()
Returns if the list has changed. If the list 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.

getDeleted

public final List getDeleted()
Returns deleted elements. If the list is not in tag mode (this means isTagged() returns true) this method will always return an empty list.

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

getAdded

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

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

revert

public final void revert()
Roll back all changes made since construction. WARNING: The position of the elements is not guaranteed to be the same again! This is the same function ad revertToTag(). If the list is not in tag mode ( this means isTagged() returns true) this method will do nothing.


add

public final boolean add(Object o)

Specified by:
add in interface Collection
Specified by:
add in interface List

add

public final void add(int index,
                      Object o)

Specified by:
add in interface List

addAll

public final boolean addAll(Collection c)

Specified by:
addAll in interface Collection
Specified by:
addAll in interface List

addAll

public final boolean addAll(int index,
                            Collection c)

Specified by:
addAll in interface List

clear

public final void clear()

Specified by:
clear in interface Collection
Specified by:
clear in interface List

contains

public final boolean contains(Object o)

Specified by:
contains in interface Collection
Specified by:
contains in interface List

containsAll

public final boolean containsAll(Collection c)

Specified by:
containsAll in interface Collection
Specified by:
containsAll in interface List

get

public final Object get(int index)

Specified by:
get in interface List

indexOf

public final int indexOf(Object o)

Specified by:
indexOf in interface List

isEmpty

public final boolean isEmpty()

Specified by:
isEmpty in interface Collection
Specified by:
isEmpty in interface List

iterator

public final Iterator iterator()

Specified by:
iterator in interface Iterable
Specified by:
iterator in interface Collection
Specified by:
iterator in interface List

lastIndexOf

public final int lastIndexOf(Object o)

Specified by:
lastIndexOf in interface List

listIterator

public final ListIterator listIterator()

Specified by:
listIterator in interface List

listIterator

public final ListIterator listIterator(int index)

Specified by:
listIterator in interface List

remove

public final boolean remove(Object o)

Specified by:
remove in interface Collection
Specified by:
remove in interface List

remove

public final Object remove(int index)

Specified by:
remove in interface List

removeAll

public final boolean removeAll(Collection c)

Specified by:
removeAll in interface Collection
Specified by:
removeAll in interface List

retainAll

public final boolean retainAll(Collection c)

Specified by:
retainAll in interface Collection
Specified by:
retainAll in interface List

set

public final Object set(int index,
                        Object o)

Specified by:
set in interface List

size

public final int size()

Specified by:
size in interface Collection
Specified by:
size in interface List

subList

public final List subList(int fromIndex,
                          int toIndex)

Specified by:
subList in interface List

toArray

public final Object[] toArray()

Specified by:
toArray in interface Collection
Specified by:
toArray in interface List

toArray

public final Object[] toArray(Object[] a)

Specified by:
toArray in interface Collection
Specified by:
toArray in interface List

toString

public final String toString()

Overrides:
toString in class 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 © 2011 Future Invent Informationsmanagement GmbH. All Rights Reserved.