public class LimitedTableIterator extends java.lang.Object implements TableIterator
Wrap a TableIterator
in order to limit its reading to a fixed number of rows.
This wrapper can be "mixed" with a LimitedSizeInputStream
, by wrapping the original input stream by a LimitedSizeInputStream
and then by wrapping the TableIterator
based on this wrapped input stream by LimitedTableIterator
.
Thus, this wrapper will be able to detect embedded ExceededSizeException
thrown by a LimitedSizeInputStream
through another TableIterator
.
If a such exception is detected, it will declare this wrapper as overflowed as it would be if a rows limit is reached.
Warning:
To work together with a LimitedSizeInputStream
, this wrapper relies on the hypothesis that any IOException
(including ExceededSizeException
)
will be embedded in a DataReadException
as cause of this exception (using DataReadException.DataReadException(Throwable)
or DataReadException.DataReadException(String, Throwable)
). If it is not the case, no overflow detection could be done and the exception will just be forwarded.
If a limit - either of rows or of bytes - is reached, a flag "overflow" is set to true. This flag can be got with isOverflow()
.
Thus, when a DataReadException
is caught, it will be easy to detect whether the error occurred because of an overflow
or of another problem.
Constructor and Description |
---|
LimitedTableIterator(java.lang.Class<T> classIt,
java.io.InputStream input,
ServiceConnection.LimitUnit type,
int limit)
Build the specified
TableIterator instance and wrap it so that limiting the number of rows OR bytes to read. |
LimitedTableIterator(TableIterator it,
int nbMaxRows)
Wrap the given
TableIterator so that limiting the number of rows to read. |
Modifier and Type | Method and Description |
---|---|
void |
close()
Close the stream or input over which this class iterates.
|
DBType |
getColType()
Get the type of the current column value.
|
TAPColumn[] |
getMetadata()
Get all the metadata column that have been successfully extracted at the creation of this iterator.
|
TableIterator |
getWrappedIterator()
Get the iterator wrapped by this
TableIterator instance. |
boolean |
hasNextCol()
Tell whether another column is available.
|
boolean |
isOverflow()
Tell whether a limit (in rows or bytes) has been reached.
|
java.lang.Object |
nextCol()
Return the value of the next column.
|
boolean |
nextRow()
Go to the next row if there is one.
|
public LimitedTableIterator(TableIterator it, int nbMaxRows) throws DataReadException
TableIterator
so that limiting the number of rows to read.it
- The iterator to wrap. MUST NOT be NULLnbMaxRows
- Maximum number of rows that can be read. There is overflow if more than this number of rows is asked. A negative value means "no limit".DataReadException
public LimitedTableIterator(java.lang.Class<T> classIt, java.io.InputStream input, ServiceConnection.LimitUnit type, int limit) throws DataReadException
Build the specified TableIterator
instance and wrap it so that limiting the number of rows OR bytes to read.
If the limit is on the number of bytes, the given input stream will be first wrapped inside a LimitedSizeInputStream
.
Then, it will be given as only parameter of the constructor of the specified TableIterator
instance.
If the limit is on the number of rows, this LimitedTableIterator
will count and limit itself the number of rows.
IMPORTANT: The specified class must:
TableIterator
,InputStream
.Note: If the given limit type is NULL (or different from ROWS and BYTES), or the limit value is <=0, no limit will be set. All rows and bytes will be read until the end of input is reached.
classIt
- Class of the TableIterator
implementation to create and whose the output must be limited.input
- Input stream toward the table to read.type
- Type of the limit: ROWS or BYTES. MAY be NULLlimit
- Limit in rows or bytes, depending of the "type" parameter. MAY BE <=0DataReadException
- If no instance of the given class can be created,
or if the TableIterator
instance can not be initialized,
or if the limit (in rows or bytes) has been reached.public final TableIterator getWrappedIterator()
TableIterator
instance.public final boolean isOverflow()
Tell whether a limit (in rows or bytes) has been reached.
Note:
If true is returned (that's to say, if a limit has been reached) no more rows or column values
can be read ; an IllegalStateException
would then be thrown.
public void close() throws DataReadException
TableIterator
close
in interface TableIterator
DataReadException
- If any error occurs while closing it.public TAPColumn[] getMetadata() throws DataReadException
TableIterator
Get all the metadata column that have been successfully extracted at the creation of this iterator.
Important: This function should be callable at any moment from the creation of the iterator until the end of the table dataset has been reached.
Note: This function MAY BE NOT IMPLEMENTED or the metadata can not be fetched. In this case, NULL will be returned.
Warning: If the metadata part of the original document is corrupted (i.e. false number of columns),
the column type information should be fetched thanks to TableIterator.getColType()
while iterating over rows and columns.
getMetadata
in interface TableIterator
TAPColumn
objects (each for a column of any row),
or NULL if this function is not implemented OR if it was not possible to get these metadata.DataReadException
TableIterator.getColType()
public boolean nextRow() throws DataReadException
TableIterator
Go to the next row if there is one.
Note: After a call to this function the columns must be fetched individually using TableIterator.nextCol()
IF this function returned true.
nextRow
in interface TableIterator
DataReadException
- If an error occurs while reading the table dataset.public boolean hasNextCol() throws java.lang.IllegalStateException, DataReadException
TableIterator
hasNextCol
in interface TableIterator
TableIterator.nextCol()
will return the value of the next column with no error,
false otherwise.java.lang.IllegalStateException
- If TableIterator.nextRow()
has not yet been called.DataReadException
- If an error occurs while reading the table dataset.public java.lang.Object nextCol() throws java.util.NoSuchElementException, java.lang.IllegalStateException, DataReadException
TableIterator
Return the value of the next column.
Note: The column type can be fetched using TableIterator.getColType()
after a call to TableIterator.nextCol()
.
nextCol
in interface TableIterator
java.util.NoSuchElementException
- If no more column value is available.java.lang.IllegalStateException
- If TableIterator.nextRow()
has not yet been called.DataReadException
- If an error occurs while reading the table dataset.public DBType getColType() throws java.lang.IllegalStateException, DataReadException
TableIterator
Get the type of the current column value.
Note 1: "Current column value" means here "the value last returned by TableIterator.nextCol()
".
Note 2: This function MAY BE NOT IMPLEMENTED or the type information can not be fetched. If this is the case, NULL will be returned.
Warning: In some cases, the metadata part of the original document does not match with the data
it should have represented. In such case, the types returned here and by TableIterator.getMetadata()
would be different.
In case of such mismatch, the type returned by TableIterator.getColType()
should be considered as more correct/accurate.
getColType
in interface TableIterator
java.lang.IllegalStateException
- If TableIterator.nextCol()
has not yet been called.DataReadException
- If an error occurs while reading the table dataset.