org.priha.util
Class ChangeStore

java.lang.Object
  extended by org.priha.util.ChangeStore
All Implemented Interfaces:
java.lang.Iterable<ChangeStore.Change>

public class ChangeStore
extends java.lang.Object
implements java.lang.Iterable<ChangeStore.Change>

Provides a list of changes, which can be both played back one by one, as well as searched rapidly.

Internally, this class stores both a List of Change objects, as well as a HashMap pointing at the latest change. It can be iterated both forwards (using peek() and remove() and iterator()) as well as backwards (using values()).

In addition, it stores a separate reference to all the changes relating to a particular parent path (making it fairly fast to filter based on a path).


Nested Class Summary
static class ChangeStore.Change
          Stores a single change.
 
Constructor Summary
ChangeStore()
          Create a ChangeStore without the HashMap.
 
Method Summary
 boolean add(ChangeStore.Change c)
          Adds a whole Change object at the end of the Change List.
 void add(ItemState newState, ItemImpl ii)
          Adds a new Item with given ItemState to the end of the Change List.
 int clear()
          Clears the changes.
 void dump()
          Dumps the store contents for debugging to System.out.
 ChangeStore.Change getLatestChange(Path path)
          Finds the latest change.
 java.util.List<ChangeStore.Change> getLatestChanges()
          Get all the latest changes, ignoring any duplicate changes.
 java.util.List<ChangeStore.Change> getLatestChangesForParent(Path parent)
          Returns a list of Changes filtered by a parent path.
 ItemImpl getLatestItem(Path path)
          Returns the newest ItemImpl that corresponds to the Path given.
 boolean isEmpty()
          Returns true, if there are no changes.
 java.util.Iterator<ChangeStore.Change> iterator()
          Returns a forward iterator for the Changes.
 ChangeStore.Change peek()
          Gets the first change from the change list.
 ChangeStore.Change remove()
          Removes the first change from the change list.
 java.lang.String toString()
          Outputs a human-readable description of the contents of the ChangeStore.
 java.util.Iterator<ItemImpl> values()
          Returns a backward iterator for the Items in the change list.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ChangeStore

public ChangeStore()
Create a ChangeStore without the HashMap.

Method Detail

getLatestItem

public ItemImpl getLatestItem(Path path)
Returns the newest ItemImpl that corresponds to the Path given.

Parameters:
path - Path to search for
Returns:
Newest Item or null, if no such thing is found.

getLatestChange

public ChangeStore.Change getLatestChange(Path path)
Finds the latest change.

Parameters:
path - Path to search for
Returns:
The Change or null, if no such thing found.

getLatestChanges

public java.util.List<ChangeStore.Change> getLatestChanges()
Get all the latest changes, ignoring any duplicate changes.

Returns:
A list of latest changes.

getLatestChangesForParent

public java.util.List<ChangeStore.Change> getLatestChangesForParent(Path parent)
Returns a list of Changes filtered by a parent path.

Parameters:
parent - The path to filter against
Returns:
A list of Changes.

add

public void add(ItemState newState,
                ItemImpl ii)
Adds a new Item with given ItemState to the end of the Change List.

Parameters:
newState - New state
ii - The Item

add

public boolean add(ChangeStore.Change c)
Adds a whole Change object at the end of the Change List.

Parameters:
c - The Change to add
Returns:
True, at the moment.

peek

public ChangeStore.Change peek()
Gets the first change from the change list. Returns null, if there are no more changes.

Returns:
The first change from the list, or null, if the list was empty.

remove

public ChangeStore.Change remove()
Removes the first change from the change list.

Returns:
The first change from the list, or null, if the list was empty.

clear

public int clear()
Clears the changes.

Returns:
The number of changes removed from the queue.

iterator

public java.util.Iterator<ChangeStore.Change> iterator()
Returns a forward iterator for the Changes.

Specified by:
iterator in interface java.lang.Iterable<ChangeStore.Change>
Returns:
A forward iterator for the Changes.

values

public java.util.Iterator<ItemImpl> values()
Returns a backward iterator for the Items in the change list. The first value you get is the newest value on the stack. This means that the iteration order for iterator() and values() is reversed.

Returns:
A reverse iterator for the values.

isEmpty

public final boolean isEmpty()
Returns true, if there are no changes.

Returns:
True, if there are no changes.

dump

public void dump()
Dumps the store contents for debugging to System.out.


toString

public java.lang.String toString()
Outputs a human-readable description of the contents of the ChangeStore.

Overrides:
toString in class java.lang.Object
Returns:
Somethign human-readable.