goldman.collection.positional
Class SinglyLinkedList.Tracker

java.lang.Object
  extended by goldman.collection.AbstractCollection.AbstractLocator<E>
      extended by goldman.collection.positional.SinglyLinkedList.Tracker
All Implemented Interfaces:
Locator<E>, PositionalCollectionLocator<E>, Cloneable, Iterator<E>
Enclosing class:
SinglyLinkedList<E>

protected class SinglyLinkedList.Tracker
extends AbstractCollection.AbstractLocator<E>
implements PositionalCollectionLocator<E>


Field Summary
 
Fields inherited from class goldman.collection.AbstractCollection.AbstractLocator
versionNumber
 
Constructor Summary
protected SinglyLinkedList.Tracker(SinglyLinkedList.ListItem<E> startLoc)
           
 
Method Summary
 PositionalCollectionLocator<E> addAfter(E value)
          Adds the new element after the tracked element
 boolean advance()
          Moves the tracker to the next location
 E get()
          Returns the element associated with this locator.
 int getCurrentPosition()
          This method takes constant time for an element not in the collection.
 E getNextElement()
           
 boolean hasNext()
           
 boolean inCollection()
          Returns true if and only if the locator is at an element of the collection.
 void remove()
          Removes the tracked element from the collection.
 void removeNextElement()
          Removes the element that follows the tracked element
 boolean retreat()
          Updates the tracker to the previous element in the collection
 E set(E value)
           
protected  SinglyLinkedList.ListItem<E> skipRemovedElements(SinglyLinkedList.ListItem<E> ptr)
           
 
Methods inherited from class goldman.collection.AbstractCollection.AbstractLocator
checkValidity, ignoreConcurrentModifications, ignorePriorConcurrentModifications, next, updateVersion
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface goldman.collection.Locator
ignoreConcurrentModifications, ignorePriorConcurrentModifications
 
Methods inherited from interface java.util.Iterator
next
 

Constructor Detail

SinglyLinkedList.Tracker

protected SinglyLinkedList.Tracker(SinglyLinkedList.ListItem<E> startLoc)
Parameters:
startLoc - a reference to the list item holding the element to track.
Method Detail

inCollection

public boolean inCollection()
Description copied from interface: Locator
Returns true if and only if the locator is at an element of the collection.

Specified by:
inCollection in interface Locator<E>
Returns:
true if and only if the tracked element is currently in the collection

get

public E get()
Description copied from interface: Locator
Returns the element associated with this locator. When a tracker is used, the element associated with the locator might no longer be in the collection. If desired, the inCollection method can be used to determine if a tracked element is currently in the collection. If the locator is at FORE or AFT then a NoSuchElementException is thrown.

Specified by:
get in interface Locator<E>
Returns:
the tracked element
Throws:
NoSuchElementException - the tracker is not at an element in the collection.

set

public E set(E value)
Parameters:
value - the element to put at the current tracker location.
Returns:
the element that had been stored at the tracker location
Throws:
NoSuchElementException - tracker is not at an element in the collection.

getCurrentPosition

public int getCurrentPosition()
This method takes constant time for an element not in the collection. For an element in the collection, this method takes time proportional to the position of the tracked element, which in the worst-case is linear time.

Specified by:
getCurrentPosition in interface PositionalCollectionLocator<E>
Returns:
the position of the tracker within the collection.
Throws:
NoSuchElementException - the tracker is not at an element in the collection.

skipRemovedElements

protected SinglyLinkedList.ListItem<E> skipRemovedElements(SinglyLinkedList.ListItem<E> ptr)
Parameters:
ptr - a pointer to a list item
Returns:
a pointer to the first reachable list item, possibly ptr itself if it is reachable.

advance

public boolean advance()
                throws ConcurrentModificationException
Moves the tracker to the next location

Specified by:
advance in interface Locator<E>
Returns:
true if and only if the tracker moves to a valid position.
Throws:
AtBoundaryException - the tracker is already at AFT since there is no place to advance.
ConcurrentModificationException

retreat

public boolean retreat()
                throws ConcurrentModificationException
Updates the tracker to the previous element in the collection

Specified by:
retreat in interface Locator<E>
Returns:
true if and only if the tracker is at an element in the collection after retreating.
Throws:
AtBoundaryException - the tracker is at FORE.
ConcurrentModificationException

hasNext

public boolean hasNext()
                throws ConcurrentModificationException
Specified by:
hasNext in interface Iterator<E>
Returns:
true if there is some element after the current tracker position.
Throws:
ConcurrentModificationException

addAfter

public PositionalCollectionLocator<E> addAfter(E value)
                                        throws ConcurrentModificationException
Adds the new element after the tracked element

Specified by:
addAfter in interface PositionalCollectionLocator<E>
Parameters:
value - the element to add
Returns:
a tracker to the new element
Throws:
ConcurrentModificationException

remove

public void remove()
            throws ConcurrentModificationException
Removes the tracked element from the collection. Since there are no previous pointers, this method takes O(p) time, where p is the position of the tracked element.

Specified by:
remove in interface Iterator<E>
Throws:
NoSuchElementException - the tracked element has already been removed.
ConcurrentModificationException

getNextElement

public E getNextElement()
                 throws ConcurrentModificationException
Returns:
the element after the tracked element
Throws:
NoSuchElementException - the tracked element is not in the collection, or if current element is at position size-1.
ConcurrentModificationException

removeNextElement

public void removeNextElement()
                       throws ConcurrentModificationException
Removes the element that follows the tracked element

Throws:
NoSuchElementException - the tracked element is not in the collection, or if current element is at position size-1.
ConcurrentModificationException