messif.objects.util
Class StreamGenericAbstractObjectIterator<E extends LocalAbstractObject>

java.lang.Object
  extended by messif.objects.util.AbstractObjectIterator<E>
      extended by messif.objects.util.AbstractStreamObjectIterator<E>
          extended by messif.objects.util.StreamGenericAbstractObjectIterator<E>
Type Parameters:
E - the class of objects provided by this stream iterator (must be descendant of LocalAbstractObject)
All Implemented Interfaces:
java.io.Closeable, java.util.Iterator<E>, ObjectProvider<E>

public class StreamGenericAbstractObjectIterator<E extends LocalAbstractObject>
extends AbstractStreamObjectIterator<E>

This class represents an iterator on LocalAbstractObjects that are read from a file. The objects are instantiated one by one every time the next method is called. The file should be created using LocalAbstractObject.write(java.io.OutputStream) method.


Field Summary
protected  java.lang.reflect.Constructor<? extends E> constructor
          Class instance of objects of type E needed for instantiating objects read from a stream
protected  java.lang.Object[] constructorArgs
          Arguments for the constructor (first will always be the stream)
protected  E currentObject
          Instance of the current object
protected  java.lang.String fileName
          Remembered name of opened file to provide reset capability
protected  E nextObject
          Instance of a next object.
protected  int objectsRead
          Number of objects read from the stream
protected  java.io.BufferedReader stream
          An input stream for reading objects of this iterator from
 
Constructor Summary
StreamGenericAbstractObjectIterator(java.lang.Class<? extends E> objClass, java.io.BufferedReader stream)
          Creates a new instance of StreamGenericAbstractObjectIterator.
StreamGenericAbstractObjectIterator(java.lang.Class<? extends E> objClass, java.io.BufferedReader stream, java.util.Map<java.lang.String,java.lang.Object> namedInstances, java.util.Collection<?> constructorArgs)
          Creates a new instance of StreamGenericAbstractObjectIterator.
StreamGenericAbstractObjectIterator(java.lang.Class<? extends E> objClass, java.lang.String fileName)
          Creates a new instance of StreamGenericAbstractObjectIterator.
StreamGenericAbstractObjectIterator(java.lang.Class<? extends E> objClass, java.lang.String fileName, java.util.Map<java.lang.String,java.lang.Object> namedInstances, java.util.Collection<?> constructorArgs)
          Creates a new instance of StreamGenericAbstractObjectIterator.
StreamGenericAbstractObjectIterator(java.lang.reflect.Constructor<? extends E> constructor, java.lang.Object[] constructorArgs)
          Creates a new instance of StreamGenericAbstractObjectIterator.
 
Method Summary
 void close()
          Close the associated stream.
 E getCurrentObject()
          Returns an instance of object returned by the last call to next().
 java.lang.String getFileName()
          Returns the name of the file opened by this stream.
 boolean hasNext()
          Returns true if the iteration has more elements.
 E next()
          Returns the next object instance from the stream.
protected  E nextStreamObject()
          Returns an instance of object which would be returned by next call to next().
 void remove()
          Removes from the underlying collection the last element returned by the iterator.
 void reset()
          Reset the associated stream and restarts the iteration from beginning.
 void setConstructorParameter(int index, java.lang.Object paramValue)
          Sets the value of this stream's object constructor argument.
 java.lang.String toString()
           
 
Methods inherited from class messif.objects.util.AbstractObjectIterator
getCurrentObjectID, getMatchingObjects, getMatchingObjects, getMatchingObjects, getMatchingObjects, getObjectByAnyLocator, getObjectByData, getObjectByID, getObjectByLocator, getObjectByPosition, getRandomObject, getRandomObjects, nextObjectID, provideObjects
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

stream

protected java.io.BufferedReader stream
An input stream for reading objects of this iterator from


fileName

protected java.lang.String fileName
Remembered name of opened file to provide reset capability


nextObject

protected E extends LocalAbstractObject nextObject
Instance of a next object. This is needed for implementing reading objects from a stream


currentObject

protected E extends LocalAbstractObject currentObject
Instance of the current object


objectsRead

protected int objectsRead
Number of objects read from the stream


constructor

protected final java.lang.reflect.Constructor<? extends E extends LocalAbstractObject> constructor
Class instance of objects of type E needed for instantiating objects read from a stream


constructorArgs

protected final java.lang.Object[] constructorArgs
Arguments for the constructor (first will always be the stream)

Constructor Detail

StreamGenericAbstractObjectIterator

public StreamGenericAbstractObjectIterator(java.lang.reflect.Constructor<? extends E> constructor,
                                           java.lang.Object[] constructorArgs)
Creates a new instance of StreamGenericAbstractObjectIterator. The constructor args must be compatible with the constructor and the first argument must be a BufferedReader.

Parameters:
constructor - the constructor used to create instances of objects in this stream
constructorArgs - constructor arguments

StreamGenericAbstractObjectIterator

public StreamGenericAbstractObjectIterator(java.lang.Class<? extends E> objClass,
                                           java.io.BufferedReader stream,
                                           java.util.Map<java.lang.String,java.lang.Object> namedInstances,
                                           java.util.Collection<?> constructorArgs)
                                    throws java.lang.IllegalArgumentException,
                                           java.lang.IllegalStateException
Creates a new instance of StreamGenericAbstractObjectIterator. The objects are loaded from the given stream on the fly as this iterator is iterated. The constructor of objClass that acceps BufferedReader as the first argument and all the arguments from the constructorArgs is used to read objects from the stream.

Parameters:
objClass - the class used to create the instances of objects in this stream
stream - stream from which objects are read and instantiated
namedInstances - map of named instances - an instance from this map is returned if the string matches a key in the map
constructorArgs - additional constructor arguments
Throws:
java.lang.IllegalArgumentException - if the provided class does not have a proper "stream" constructor
java.lang.IllegalStateException - if there was an error reading from the stream

StreamGenericAbstractObjectIterator

public StreamGenericAbstractObjectIterator(java.lang.Class<? extends E> objClass,
                                           java.lang.String fileName,
                                           java.util.Map<java.lang.String,java.lang.Object> namedInstances,
                                           java.util.Collection<?> constructorArgs)
                                    throws java.lang.IllegalArgumentException,
                                           java.io.IOException
Creates a new instance of StreamGenericAbstractObjectIterator. The objects are loaded from the given file on the fly as this iterator is iterated. If the fileName is empty, null or dash, standard input is used.

Parameters:
objClass - the class used to create the instances of objects in this stream
fileName - the path to a file from which objects are read; if it is a directory, all files that match the glob pattern are loaded (see DirectoryInputStream for more informations)
namedInstances - map of named instances - an instance from this map is returned if the string matches a key in the map
constructorArgs - additional constructor arguments
Throws:
java.lang.IllegalArgumentException - if the provided class does not have a proper "stream" constructor
java.io.IOException - if there was an error opening the file

StreamGenericAbstractObjectIterator

public StreamGenericAbstractObjectIterator(java.lang.Class<? extends E> objClass,
                                           java.lang.String fileName)
                                    throws java.lang.IllegalArgumentException,
                                           java.io.IOException
Creates a new instance of StreamGenericAbstractObjectIterator. The objects are loaded from the given file on the fly as this iterator is iterated. If the fileName is empty, null or dash, standard input is used.

Parameters:
objClass - the class used to create the instances of objects in this stream
fileName - the path to a file from which objects are read
Throws:
java.lang.IllegalArgumentException - if the provided class does not have a proper "stream" constructor
java.io.IOException - if there was an error opening the file

StreamGenericAbstractObjectIterator

public StreamGenericAbstractObjectIterator(java.lang.Class<? extends E> objClass,
                                           java.io.BufferedReader stream)
                                    throws java.lang.IllegalArgumentException
Creates a new instance of StreamGenericAbstractObjectIterator. The objects are loaded from the given stream on the fly as this iterator is iterated.

Parameters:
objClass - the class used to create the instances of objects in this stream
stream - stream from which objects are read and instantiated
Throws:
java.lang.IllegalArgumentException - if the provided class does not have a proper "stream" constructor
Method Detail

setConstructorParameter

public void setConstructorParameter(int index,
                                    java.lang.Object paramValue)
                             throws java.lang.IndexOutOfBoundsException,
                                    java.lang.IllegalArgumentException,
                                    java.lang.InstantiationException
Sets the value of this stream's object constructor argument. This method can be used to change object passed to constructorArgs.

Specified by:
setConstructorParameter in class AbstractStreamObjectIterator<E extends LocalAbstractObject>
Parameters:
index - the parameter index to change (zero-based)
paramValue - the changed value to pass to the constructor
Throws:
java.lang.IllegalArgumentException - when the passed object is incompatible with the constructor's parameter
java.lang.IndexOutOfBoundsException - if the index parameter is out of bounds (zero parameter cannot be changed)
java.lang.InstantiationException - if the value passed is string that is not convertible to the constructor class

getFileName

public java.lang.String getFileName()
Returns the name of the file opened by this stream. Null is returned if this stream was created from the standard input or from a stream.

Returns:
the name of the file opened by this stream

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

next

public E next()
                                   throws java.util.NoSuchElementException,
                                          java.lang.IllegalArgumentException,
                                          java.lang.IllegalStateException
Returns the next object instance from the stream.

Returns:
the next object instance from the stream
Throws:
java.util.NoSuchElementException - if the end-of-file was reached
java.lang.IllegalArgumentException - if there was an error creating a new instance of the object
java.lang.IllegalStateException - if there was an error reading from the stream

getCurrentObject

public E getCurrentObject()
                                               throws java.util.NoSuchElementException
Returns an instance of object returned by the last call to next().

Specified by:
getCurrentObject in class AbstractObjectIterator<E extends LocalAbstractObject>
Returns:
an instance of object returned by the last call to next()
Throws:
java.util.NoSuchElementException - if next() has not been called yet

hasNext

public boolean hasNext()
Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

Returns:
true if the iterator has more elements.

remove

public void remove()
Removes from the underlying collection the last element returned by the iterator. This method is unsupported by the stream iterator.


nextStreamObject

protected E nextStreamObject()
                                                  throws java.lang.IllegalArgumentException,
                                                         java.lang.IllegalStateException
Returns an instance of object which would be returned by next call to next().

Returns:
Returns an instance of object of type E which would be returned by the next call to next(). If there is no additional object, null is returned.
Throws:
java.lang.IllegalArgumentException - if there was an error creating a new instance of the object
java.lang.IllegalStateException - if there was an error reading from the stream

close

public void close()
           throws java.io.IOException
Close the associated stream. The iteration is finished, hasNext() will return false. However, getCurrentObject is still valid if there was previous call to next().

Throws:
java.io.IOException - if there was an I/O error closing the file

reset

public void reset()
           throws java.io.IOException
Reset the associated stream and restarts the iteration from beginning.

Specified by:
reset in class AbstractStreamObjectIterator<E extends LocalAbstractObject>
Throws:
java.io.IOException - if there was an I/O error re-opening the file