chemaxon.marvin.io
Class MDocSource

java.lang.Object
  extended by chemaxon.marvin.io.MDocSource
All Implemented Interfaces:
java.lang.Iterable<Molecule>
Direct Known Subclasses:
ArrayMDocSource, MolImporter

public abstract class MDocSource
extends java.lang.Object
implements java.lang.Iterable<Molecule>

Marvin document source.

Since:
Marvin 4.1, 04/14/2006
Version:
5.4, 09/28/2010
Author:
Peter Csizmadia

Constructor Summary
MDocSource()
           
 
Method Summary
 void close()
          Closes the underlying input stream(s).
abstract  int estimateNumRecords()
          Estimates the total number of records.
 java.lang.String getDocLabel(int k, MDocument doc)
          Returns the display label for the given document and document index.
 MoleculeIterator getMoleculeIterator()
          Gets a molecule iterator for this document source.
abstract  int getRecordCount()
          Gets the current record number.
abstract  int getRecordCountMax()
          Gets the total number of records.
abstract  boolean isEndReached()
          Tests whether the end of input is already reached.
abstract  boolean isRewindable()
          Tests whether rewinding (seeking backwards) is possible.
 java.util.Iterator<Molecule> iterator()
          Gets a molecule iterator for this document source.
abstract  MDocument nextDoc()
          Produces the next document.
 int seekForward(int k, chemaxon.common.util.MProgressMonitor pmon, int dt, java.lang.Runnable sizeupdater)
          Seeks in forward direction.
abstract  void seekRecord(int k, chemaxon.common.util.MProgressMonitor pmon)
          Seeks the specified record.
 void seekRecordAtFraction(double q, int start, int n, int ncols, chemaxon.common.util.MProgressMonitor pmon)
          Seeks an approximate file position.
protected abstract  void seekVisitedRecord(int k)
          Seeks an already visited position in case of rewindable input.
abstract  boolean skipRecord()
          Skips the next document.
protected  int skipRecords(int start, int end, chemaxon.common.util.MProgressMonitor pmon, int dt, java.lang.Runnable sizeupdater)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MDocSource

public MDocSource()
Method Detail

nextDoc

public abstract MDocument nextDoc()
                           throws java.io.IOException
Produces the next document.

Returns:
the document or null at end of file
Throws:
java.io.IOException - If I/O error occured

close

public void close()
           throws java.io.IOException
Closes the underlying input stream(s). The default implementation does not perform any operation.

Throws:
java.io.IOException - If an I/O error has occurred.
Since:
Marvin 5.2, 02/12/2009

getDocLabel

public java.lang.String getDocLabel(int k,
                                    MDocument doc)
Returns the display label for the given document and document index. The default implementation returns k+1 as String, where k is the document index.

Parameters:
k - the document index
doc - the document
Returns:
the document label
Since:
Marvin 5.0

skipRecord

public abstract boolean skipRecord()
                            throws java.io.IOException
Skips the next document.

Returns:
true if the end of the next document is found, false if there is no chance to continue
Throws:
java.io.IOException - If I/O error occured

isRewindable

public abstract boolean isRewindable()
Tests whether rewinding (seeking backwards) is possible.

Returns:
true if rewinding is possible, false otherwise
See Also:
seekRecord(int, MProgressMonitor), seekRecordAtFraction(double, int, int, int, MProgressMonitor)

seekRecord

public abstract void seekRecord(int k,
                                chemaxon.common.util.MProgressMonitor pmon)
                         throws java.io.EOFException,
                                java.io.IOException
Seeks the specified record. Backward seeking (rewinding) in the stream is only possible if the underlying input stream is seekable. Forward seeking is always possible. Seeking terminates before reaching the specified position if the user cancels the progress dialog.

Parameters:
k - position
pmon - progress monitor or null
Throws:
java.io.EOFException - if end of file reached while trying to seek
java.io.IOException - if read error occured
See Also:
isRewindable(), seekRecordAtFraction(double, int, int, int, MProgressMonitor)

seekRecordAtFraction

public final void seekRecordAtFraction(double q,
                                       int start,
                                       int n,
                                       int ncols,
                                       chemaxon.common.util.MProgressMonitor pmon)
                                throws java.io.IOException
Seeks an approximate file position. To seek the start or end of input, use x = 0 or 1, respectively. If records correspond to rows and columns of a table and the ncols argument differs from 1, then seeking is restricted to the first column. Backward seeking (rewinding) in the stream is only possible if the underlying input stream is seekable. Forward seeking is always possible. Seeking terminates before reaching the specified position if the user cancels the progress dialog.

Parameters:
q - the position divided by the file size, 0 is the start, 1 is the end of file
start - the minimum record index (offset)
n - the total number of available records or 0 if all records are available
ncols - the number of columns (1 if there are no more columns)
pmon - progress monitor or null
Throws:
java.io.IOException - if read error occured
See Also:
isRewindable(), seekRecord(int, MProgressMonitor)

seekVisitedRecord

protected abstract void seekVisitedRecord(int k)
                                   throws java.io.IOException
Seeks an already visited position in case of rewindable input.

Parameters:
k - the record index
Throws:
java.io.IOException - if read error occured

seekForward

public final int seekForward(int k,
                             chemaxon.common.util.MProgressMonitor pmon,
                             int dt,
                             java.lang.Runnable sizeupdater)
                      throws java.io.IOException
Seeks in forward direction.

Parameters:
k - the record to seek
pmon - the progress monitor
dt - time interval in milliseconds for calling size update
sizeupdater - optional runnable to be called if the total number of records changed or null
Returns:
the record number where seeking stopped
Throws:
java.io.IOException - if read error occured
See Also:
seekRecordAtFraction(double, int, int, int, MProgressMonitor), seekVisitedRecord(int)

skipRecords

protected final int skipRecords(int start,
                                int end,
                                chemaxon.common.util.MProgressMonitor pmon,
                                int dt,
                                java.lang.Runnable sizeupdater)
                         throws java.io.IOException
Throws:
java.io.IOException

isEndReached

public abstract boolean isEndReached()
Tests whether the end of input is already reached.

Returns:
true if the end was reached, false otherwise

getRecordCount

public abstract int getRecordCount()
Gets the current record number.

Returns:
the record number

getRecordCountMax

public abstract int getRecordCountMax()
Gets the total number of records.

Returns:
the number of records

estimateNumRecords

public abstract int estimateNumRecords()
Estimates the total number of records. Implementations must return -1 in the beginning if the size of the stream is absolutely uncertain. After the first record is parsed, an estimation is returned. The estimation must be greater than the total number of records read so far, unless there are surely no more records. If the total size is still uncertain, then it should return 2 times the current number of records.

Returns:
estimated number of records or -1 (at the beginning)

iterator

public java.util.Iterator<Molecule> iterator()
Gets a molecule iterator for this document source. The iterator starts at the current position. Only one iterator can be created for one MolImporter.

Specified by:
iterator in interface java.lang.Iterable<Molecule>
Returns:
Iterator
Throws:
java.lang.UnsupportedOperationException - if an iterator was created before
Since:
Marvin 5.11

getMoleculeIterator

public final MoleculeIterator getMoleculeIterator()
Gets a molecule iterator for this document source. The iterator starts at the current position. Only one iterator can be created for one MolImporter.

Returns:
the molecule iterator
Throws:
java.lang.UnsupportedOperationException - if an iterator was created before
Since:
Marvin 5.0.2, 03/06/2008