Class ConcurrentList.ListItr

java.lang.Object
  |
  +--ConcurrentList.ListItr
All Implemented Interfaces:
ConcurrentIterator, java.util.Iterator
Enclosing class:
ConcurrentList

private class ConcurrentList.ListItr
extends java.lang.Object
implements ConcurrentIterator

Private implementation of the ConcurrentIterator.


Field Summary
private  ConcurrentItem lastReturned
           
 
Constructor Summary
(package private) ConcurrentList.ListItr()
          Constructs an iterator of the items or elements in the list (in proper sequence), starting at the beginning of the list.
(package private) ConcurrentList.ListItr(int index)
          Constructs an iterator of the items or elements in the list (in proper sequence), starting at the given position.
 
Method Summary
protected  void finalize()
          Adjusts the iterator counter and notifies waiting threads if an iterator goes out of scope and finish() has not been called appropriately.
 void finish()
          Finishes an iteration.
 boolean hasNext()
          Indicates whether there is another element after the current position of the iterator.
 java.lang.Object next()
          Returns the next element in the list.
 ConcurrentItem nextItem()
          Returns the next item in the list.
 ConcurrentItem nextItem(ConcurrentItem item)
          Returns the next item and element in the list.
(package private)  ConcurrentItem nextItem(java.lang.Object[] elementArray)
          Returns the next item and element in the list.
 void remove()
          The remove method declared by the iterator interface cannot be overwritten directly, because a concurrent iterator might throw an InterruptedException.
 void removeCurrent()
          Removes the item currently held by the iterator.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lastReturned

private ConcurrentItem lastReturned
Constructor Detail

ConcurrentList.ListItr

ConcurrentList.ListItr()
Constructs an iterator of the items or elements in the list (in proper sequence), starting at the beginning of the list.


ConcurrentList.ListItr

ConcurrentList.ListItr(int index)
Constructs an iterator of the items or elements in the list (in proper sequence), starting at the given position.

Throws:
java.lang.IndexOutOfBoundsException - if index is out of range.
Method Detail

hasNext

public boolean hasNext()
Indicates whether there is another element after the current position of the iterator. Note that the information returned by this method is not reliable if other threads are concurrently manipulating this list. Hence, subsequent calls to next() still might cause an exception.

Specified by:
hasNext in interface java.util.Iterator
Returns:
true if an element is available, false otherwise.

next

public java.lang.Object next()
Returns the next element in the list.

Specified by:
next in interface java.util.Iterator
Returns:
the next element in the list.
Throws:
java.util.NoSuchElementException - if there are no more elements.

nextItem

public ConcurrentItem nextItem()
Returns the next item in the list.

Specified by:
nextItem in interface ConcurrentIterator
Returns:
the next item in the list, or null if there are no more items.

nextItem

ConcurrentItem nextItem(java.lang.Object[] elementArray)
Returns the next item and element in the list. Allows for an atomic access to an item of the list. If one uses the separate forms of first accessing the item then accessing the element, another thread might have changed the element of the item in the mean while. The method is only visible in the package, but there is a wrapper method which uses an item to transfer the element to the caller. The design with an array is easy to extent if more than one element should be returned.

Parameters:
elementArray - the array will contain the element found, the array will be unchanged if there are no more elements.
Returns:
the next item in the list, or null if there are no more elements.

nextItem

public ConcurrentItem nextItem(ConcurrentItem item)
Returns the next item and element in the list. Allows for an atomic access to an item of the list. If one uses the separate forms of first accessing the item then accessing the element, another thread might have changed the element of the item in the mean while.

Specified by:
nextItem in interface ConcurrentIterator
Parameters:
item - the item where to place the corresponding element, item will be unchanged if there are no more elements.
Returns:
the next item in the list, or null if there are no more elements.

remove

public void remove()
The remove method declared by the iterator interface cannot be overwritten directly, because a concurrent iterator might throw an InterruptedException. Use removeCurrent instead.

Specified by:
remove in interface java.util.Iterator
See Also:
removeCurrent()

removeCurrent

public void removeCurrent()
                   throws java.lang.InterruptedException
Removes the item currently held by the iterator. The iterator is placed to the previous item. The method can be called several times without calling a next() method as long as the iterator does not recover its initial state, that is, being placed as after construction.

Specified by:
removeCurrent in interface ConcurrentIterator
Throws:
java.util.NoSuchElementException - if there is no current item, i.e., if still none of the next() methods has been executed or if the iteration already has completed.
java.lang.InterruptedException - if the thread is interrupted while waiting for iterators releasing the current item.

finish

public void finish()
Finishes an iteration. Because an iteration over a ConcurrentList needs to maintain some state information, one should call this method whenever an iterator is not used any more. Once finish() has been called, the state of the iteration is the same as if one had iterated to the end of the list. If this advice is not followed, other threads might get blocked until the garbage collector finalizes the iterator.

Specified by:
finish in interface ConcurrentIterator

finalize

protected void finalize()
                 throws java.lang.Throwable
Adjusts the iterator counter and notifies waiting threads if an iterator goes out of scope and finish() has not been called appropriately.

Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable - whatever might be risen by the garbage collector.