com.davisor.data.stream
Class IteratorDataStream

java.lang.Object
  extended bycom.davisor.data.VoidData
      extended bycom.davisor.data.stream.IteratorDataStream
All Implemented Interfaces:
Data, DataStream
Direct Known Subclasses:
IteratorTypeStream, ListIteratorDataStream, PivotDataStream, SortDataStream

public class IteratorDataStream
extends VoidData
implements DataStream

IteratorDataStream represents a data stream that uses a standard Java iterator to implement stream elements. The elements may or may not be DataValues, and they may or may not be of a mutually equal type. The stream itself has a single type, however.

References to DataValue elements are returned as they are. Elements not of the expected type are converted first to data objects of the proper type, and then the converted objects are returned. The original objects in the source are not changed.

Since:
JDK1.1
See Also:
next(), EnumeratorDataStream, Type

Field Summary
protected  boolean M_hasNext
          Is there still data in this stream.
protected  long M_index
          Element id counter.
protected  java.util.Iterator M_iterator
          Underlying stream iterator.
protected  java.lang.Object M_metaData
          Stream meta data (may be null).
protected  DataValue M_nextData
          Reference to the next data element to be returned (may be null).
 
Fields inherited from class com.davisor.data.VoidData
M_type
 
Fields inherited from interface com.davisor.data.stream.DataStream
INITIAL_INDEX
 
Constructor Summary
  IteratorDataStream(java.util.Collection collection)
          Creates a data stream out of a collection.
  IteratorDataStream(java.util.Collection collection, Type type)
          Creates a data stream out of a collection and given type.
  IteratorDataStream(java.util.Iterator iterator)
          Creates a data stream out of an iterator.
  IteratorDataStream(java.util.Iterator iterator, Type type)
          Creates a data stream out of an iterator and the given type.
protected IteratorDataStream(Type type)
          Creates a new data stream out of a given type.
 
Method Summary
 boolean close()
          Explicitly closes this data stream, and frees all system resources it has allocated immediately.
 long getIndex()
          Retrieves the index number of the element returned by the latest next() call.
 boolean hasNext()
          Tests if the stream still contains data.
protected  void init(java.util.Iterator iterator)
          Initializes this iterator stream with given iterator.
 DataValue next()
          Gets the next data value from the stream.
 java.lang.String toString()
          Gets the string representation of this data stream.
 
Methods inherited from class com.davisor.data.VoidData
equals, getDataValue, getType, hashCode, setType
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.davisor.data.Data
getType
 

Field Detail

M_nextData

protected DataValue M_nextData
Reference to the next data element to be returned (may be null).


M_index

protected long M_index
Element id counter.


M_iterator

protected java.util.Iterator M_iterator
Underlying stream iterator.


M_hasNext

protected boolean M_hasNext
Is there still data in this stream.


M_metaData

protected java.lang.Object M_metaData
Stream meta data (may be null).

Constructor Detail

IteratorDataStream

protected IteratorDataStream(Type type)
Creates a new data stream out of a given type. This is a protected constructor for special sub-clases that wish to leave the the stream iterator and next object initially uninitialized.

Parameters:
type - the type of data in stream
See Also:
VoidData.getType(), init(java.util.Iterator)

IteratorDataStream

public IteratorDataStream(java.util.Collection collection)
                   throws InvalidDataException
Creates a data stream out of a collection. The data stream type is taken from the first element in the collection.

Parameters:
collection - data collection to create a stream from
Throws:
InvalidDataException - if the first element is not a DataValue
See Also:
IteratorDataStream(Collection,Type)

IteratorDataStream

public IteratorDataStream(java.util.Collection collection,
                          Type type)
                   throws InvalidDataException
Creates a data stream out of a collection and given type.

Parameters:
collection - data collection to create a stream from
type - the type of data in stream
Throws:
InvalidDataException - if the first element is not of the given type, and cannot be converted to a data object of that type
See Also:
IteratorDataStream(Iterator,Type)

IteratorDataStream

public IteratorDataStream(java.util.Iterator iterator)
                   throws InvalidDataException
Creates a data stream out of an iterator. The data stream type is set to AutoType.

Parameters:
iterator - data iterator to create a stream from
Throws:
InvalidDataException - if the first element was not a DataValue, and no suitable type could be created for it
See Also:
IteratorDataStream(Iterator,Type), AutoType

IteratorDataStream

public IteratorDataStream(java.util.Iterator iterator,
                          Type type)
                   throws InvalidDataException
Creates a data stream out of an iterator and the given type. Returns nulls as they are (i.e. nulls).

Parameters:
iterator - data iterator to create a stream from
type - the type of data in stream
Throws:
InvalidDataException - if the first element is not of the given type, and cannot be converted to a data object of that type.
See Also:
IteratorDataStream(Collection,Type), IteratorDataStream(Iterator), init(java.util.Iterator)
Method Detail

close

public boolean close()
Explicitly closes this data stream, and frees all system resources it has allocated immediately. For iterator steams, the reference to underlying iterator object is dropped, which makes the iterator object subject to garbage collection, at least as far this data stream is concerned.

Specified by:
close in interface DataStream
Returns:
always true
See Also:
init(java.util.Iterator)

getIndex

public long getIndex()
Retrieves the index number of the element returned by the latest next() call. The index represents the position of the element within this stream. The stream index may or may not be equal to any other indexes the element itself thinks it posesses.

The index value is initially INITIAL_INDEX (-1), indicating no element. After first next() call, index becomes 0, then 1, and so on. This functionality resembles java.sql.resultSet#getRow method functionality, but with the difference that the index counter starts from 0, not 1.

Specified by:
getIndex in interface DataStream
Returns:
element index

hasNext

public boolean hasNext()
Tests if the stream still contains data.

Specified by:
hasNext in interface DataStream
Returns:
true if it does.

next

public DataValue next()
               throws InvalidDataException
Gets the next data value from the stream. Null values coming from the stream are not converted to DataValues, but they are returned as nulls.

Specified by:
next in interface DataStream
Throws:
InvalidDataException - if the stream content does not satisfy the stream type
See Also:
close(), VoidData.getDataValue(java.lang.Object), hasNext(), init(java.util.Iterator)

toString

public java.lang.String toString()
Gets the string representation of this data stream. This however consumes the stream entirely, taking the current iterator from it's current position to the end of the stream.

Overrides:
toString in class VoidData
See Also:
hasNext(), next()

init

protected void init(java.util.Iterator iterator)
             throws InvalidDataException
Initializes this iterator stream with given iterator. In particular, the given iterator hasNext() status is inspected at this time. If the iterator is not null and it has a next element, stream hasNext() status is set to true, and the next iterator element is set to be the next() stream object to be returned. Otherwise stream hasNext() status is set to false, and the next iterator element is set to null.

The stream index is also set to INITIAL_INDEX (-1) value.

Parameters:
iterator - the iterator to initialize this stream with (may be null)
Throws:
InvalidDataException - if the first element is not a DataValue
See Also:
VoidData.getDataValue(java.lang.Object)


Copyright © 2001-2004 Davisor Oy. All Rights Reserved.