org.dasein.util
Class Jiterator<T>

java.lang.Object
  extended by org.dasein.util.Jiterator<T>
Type Parameters:
T - the type of object being managed in the jiterator
All Implemented Interfaces:
Iterable<T>, Iterator<T>

public class Jiterator<T>
extends Object
implements Iterator<T>, Iterable<T>

A "Just-in-time" iterator for streaming a flow of objects across an iterable interface. This class is useful in situations where you don't want to keep your list of objects around in memory, but instead are using the collection to move them from one location (often slow-loading) to another.

To use the jiterator, just construct it and starting pushing items into it. Once the last item has been added, call complete().

Last Modified $Date: 2009/07/02 01:37:02 $

Author:
George Reese

Constructor Summary
Jiterator()
          Constructs an empty jiterator into which you can start adding items.
Jiterator(Collection<T> list)
          Constructs a jiterator from the specified collection.
Jiterator(JiteratorFilter<T> filter)
           
 
Method Summary
 void complete()
          Marks the jiterator as complete.
 boolean hasNext()
          Checks to see if there are more elements to be processed in the jiterator.
 boolean isComplete()
           
 boolean isLoaded()
           
 Iterator<T> iterator()
           
 T next()
          Provides the next element in the jiterator.
 void push(T item)
          Pushes a new item into the jiterator.
 void remove()
          This operation is not allowed
 void setLoadException(Exception e)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Jiterator

public Jiterator()
Constructs an empty jiterator into which you can start adding items.


Jiterator

public Jiterator(JiteratorFilter<T> filter)

Jiterator

public Jiterator(Collection<T> list)
Constructs a jiterator from the specified collection. This jiterator will load items from the source collection in a background thread. It will not make the jiterator complete. You can thus add elements in addition to the source collection.

Parameters:
list - the source list of items to initialize the jiterator with
Method Detail

complete

public void complete()
Marks the jiterator as complete. Once you call this method, you cannot add any more items into the jiterator. If you fail to call this method, any threads reading from this jiterator will ultimately hang until you call this method.


hasNext

public boolean hasNext()
Checks to see if there are more elements to be processed in the jiterator. If this method is called prior to the jiterator being loaded with an item, it will hang until either an item is added into the jiterator or the jiterator is marked as complete.

Specified by:
hasNext in interface Iterator<T>
Returns:
true if there are more elements to be processed
Throws:
JiteratorLoadException - an error occurred during the load of the jiterator

isComplete

public boolean isComplete()
Returns:
true if all items have been processed and this jiterator can be discarded

isLoaded

public boolean isLoaded()
Returns:
true if all items have been loaded into the jiterator

iterator

public Iterator<T> iterator()
Specified by:
iterator in interface Iterable<T>
Returns:
this jiterator

next

public T next()
       throws JiteratorLoadException
Provides the next element in the jiterator. If this method is called prior to the jiterator being loaded with an item, it will hang until either an item is added into the jiterator or the jiterator is marked as complete.

Specified by:
next in interface Iterator<T>
Returns:
true if there are more elements to be processed
Throws:
NoSuchElementException - an attempt was made to read beyond the last item in the jiterator
JiteratorLoadException

push

public void push(T item)
Pushes a new item into the jiterator. The new item will be added to the end of the jiterator.

Parameters:
item - the item to be added
Throws:
IllegalStateException - an attempt was made to push an item on a jiterator marked complete

remove

public void remove()
This operation is not allowed

Specified by:
remove in interface Iterator<T>
Throws:
UnsupportedOperationException - always thrown

setLoadException

public void setLoadException(Exception e)


Copyright © 2011 enStratus Networks LLC. All Rights Reserved.