Class AbstractRoutines<P extends CommonParserSettings<?>,W extends CommonWriterSettings<?>>

java.lang.Object
com.univocity.parsers.common.routine.AbstractRoutines<P,W>
Type Parameters:
P - parser configuration class
W - writer configuration class
Direct Known Subclasses:
CsvRoutines, FixedWidthRoutines, TsvRoutines

public abstract class AbstractRoutines<P extends CommonParserSettings<?>,W extends CommonWriterSettings<?>> extends Object
Basic implementation of commonly used routines around parsing/writing of data that can be reused and extended by parsers/writers of any supported format.
  • Field Details

    • keepResourcesOpen

      private boolean keepResourcesOpen
    • previousOutput

      private Writer previousOutput
    • columnMapper

      private ColumnMapping columnMapper
    • routineDescription

      private final String routineDescription
    • parserSettings

      private P extends CommonParserSettings<?> parserSettings
    • writerSettings

      private W extends CommonWriterSettings<?> writerSettings
  • Constructor Details

    • AbstractRoutines

      public AbstractRoutines(String routineDescription)
      Creates a new instance of this routine class.
      Parameters:
      routineDescription - description of the routines for a given format
    • AbstractRoutines

      public AbstractRoutines(String routineDescription, P parserSettings)
      Creates a new instance of this routine class.
      Parameters:
      routineDescription - description of the routines for a given format
      parserSettings - configuration to use for parsing
    • AbstractRoutines

      public AbstractRoutines(String routineDescription, W writerSettings)
      Creates a new instance of this routine class.
      Parameters:
      routineDescription - description of the routines for a given format
      writerSettings - configuration to use for writing
    • AbstractRoutines

      public AbstractRoutines(String routineDescription, P parserSettings, W writerSettings)
      Creates a new instance of this routine class.
      Parameters:
      routineDescription - description of the routines for a given format
      parserSettings - configuration to use for parsing
      writerSettings - configuration to use for writing
  • Method Details

    • createParser

      protected abstract AbstractParser<P> createParser(P parserSettings)
      Creates a new parser implementation using the given parser configuration
      Parameters:
      parserSettings - the configuration for new parser
      Returns:
      a parser implementation configured according to the given settings object.
    • createWriter

      protected abstract AbstractWriter<W> createWriter(Writer output, W writerSettings)
      Creates a new writer implementation using the given writer configuration
      Parameters:
      output - target output of the routine.
      writerSettings - the configuration for new writer
      Returns:
      a writer implementation configured according to the given settings object.
    • createDefaultParserSettings

      protected abstract P createDefaultParserSettings()
      Creates a default parser settings configuration
      Returns:
      a new instance of a usable parser configuration.
    • createDefaultWriterSettings

      protected abstract W createDefaultWriterSettings()
      Creates a default writer settings configuration
      Returns:
      a new instance of a usable writer configuration.
    • validateWriterSettings

      private void validateWriterSettings()
    • validateParserSettings

      private void validateParserSettings()
    • getParserSettings

      public final P getParserSettings()
      Returns the parser configuration (if any) used by the routines of this utility class.
      Returns:
      the parser configuration.
    • setParserSettings

      public final void setParserSettings(P parserSettings)
      Defines the parser configuration to be used by the routines of this utility class.
      Parameters:
      parserSettings - the parser configuration.
    • getWriterSettings

      public final W getWriterSettings()
      Returns the writer configuration (if any) used by the routines of this utility class.
      Returns:
      the writer configuration.
    • setWriterSettings

      public final void setWriterSettings(W writerSettings)
      Defines the writer configuration to be used by the routines of this utility class.
      Parameters:
      writerSettings - the parser configuration.
    • adjustColumnLengths

      protected void adjustColumnLengths(String[] headers, int[] lengths)
      Allows writers of any given format to adjust its settings to take into account column headers and lengths prior to writing data in any routine.
      Parameters:
      headers - headers to be written
      lengths - the corresponding lengths of each header
    • write

      public final void write(ResultSet rs, File output)
      Dumps the content of a ResultSet into a file.
      Parameters:
      rs - the ResultSet whose contents should be read and written to a given output
      output - the output file that will store the data in the given ResultSet in the format specified by concrete implementations of this class.
    • write

      public final void write(ResultSet rs, File output, String encoding)
      Dumps the content of a ResultSet into a file.
      Parameters:
      rs - the ResultSet whose contents should be read and written to a given output
      output - the output file that will store the data in the given ResultSet in the format specified by concrete implementations of this class.
      encoding - the output encoding of the file
    • write

      public final void write(ResultSet rs, File output, Charset encoding)
      Dumps the content of a ResultSet into a file.
      Parameters:
      rs - the ResultSet whose contents should be read and written to a given output
      output - the output file that will store the data in the given ResultSet in the format specified by concrete implementations of this class.
      encoding - the output encoding of the file
    • write

      public final void write(ResultSet rs, OutputStream output)
      Dumps the content of a ResultSet into an output stream.
      Parameters:
      rs - the ResultSet whose contents should be read and written to a given output
      output - the output stream that will store the data in the given ResultSet in the format specified by concrete implementations of this class.
    • write

      public final void write(ResultSet rs, OutputStream output, String encoding)
      Dumps the content of a ResultSet into an output stream.
      Parameters:
      rs - the ResultSet whose contents should be read and written to a given output
      output - the output file that will store the data in the given ResultSet in the format specified by concrete implementations of this class.
      encoding - the output encoding of the output stream
    • write

      public final void write(ResultSet rs, OutputStream output, Charset encoding)
      Dumps the content of a ResultSet into an output stream.
      Parameters:
      rs - the ResultSet whose contents should be read and written to a given output
      output - the output file that will store the data in the given ResultSet in the format specified by concrete implementations of this class.
      encoding - the output encoding of the output stream
    • write

      public final void write(ResultSet rs, Writer output)
      Dumps the content of a ResultSet.
      Parameters:
      rs - the ResultSet whose contents should be read and written to a given output
      output - the output that will store the data in the given ResultSet in the format specified by concrete implementations of this class.
    • parseAndWrite

      public final void parseAndWrite(Reader input, Writer output)
      Reads all data from a given input and writes it to an output.
      Parameters:
      input - the input data to be parsed using the settings provided in getParserSettings()
      output - the output into where the input data should be written, using the format provided in getWriterSettings()
    • setRowWriterProcessor

      private void setRowWriterProcessor(RowWriterProcessor rowWriterProcessor)
    • setRowProcessor

      private void setRowProcessor(RowProcessor rowProcessor)
    • createWritingRowProcessor

      private RowProcessor createWritingRowProcessor(Writer output)
    • close

      private void close(AbstractWriter writer)
    • writeAll

      public <T> void writeAll(Iterable<T> elements, Class<T> beanType, File output, String... headers)
      Writes a collection of annotated java beans to a given output.
      Type Parameters:
      T - the type of element in the given collection
      Parameters:
      elements - the elements to write to the output
      beanType - the type of element in the given collection
      output - the output into which the given elements will be written
      headers - headers to use in the first row of the written result.
    • writeAll

      public <T> void writeAll(Iterable<T> elements, Class<T> beanType, File output, String encoding, String[] headers)
      Writes a collection of annotated java beans to a given output.
      Type Parameters:
      T - the type of element in the given collection
      Parameters:
      elements - the elements to write to the output
      beanType - the type of element in the given collection
      output - the output into which the given elements will be written
      encoding - the output encoding to use for writing
      headers - headers to use in the first row of the written result.
    • writeAll

      public <T> void writeAll(Iterable<T> elements, Class<T> beanType, File output, Charset encoding, String... headers)
      Writes a collection of annotated java beans to a given output.
      Type Parameters:
      T - the type of element in the given collection
      Parameters:
      elements - the elements to write to the output
      beanType - the type of element in the given collection
      output - the output into which the given elements will be written
      encoding - the output encoding to use for writing
      headers - headers to use in the first row of the written result.
    • writeAll

      public <T> void writeAll(Iterable<T> elements, Class<T> beanType, OutputStream output, String... headers)
      Writes a collection of annotated java beans to a given output.
      Type Parameters:
      T - the type of element in the given collection
      Parameters:
      elements - the elements to write to the output
      beanType - the type of element in the given collection
      output - the output into which the given elements will be written
      headers - headers to use in the first row of the written result.
    • writeAll

      public <T> void writeAll(Iterable<T> elements, Class<T> beanType, OutputStream output, String encoding, String[] headers)
      Writes a collection of annotated java beans to a given output.
      Type Parameters:
      T - the type of element in the given collection
      Parameters:
      elements - the elements to write to the output
      beanType - the type of element in the given collection
      output - the output into which the given elements will be written
      encoding - the output encoding to use for writing
      headers - headers to use in the first row of the written result.
    • writeAll

      public <T> void writeAll(Iterable<T> elements, Class<T> beanType, OutputStream output, Charset encoding, String... headers)
      Writes a collection of annotated java beans to a given output.
      Type Parameters:
      T - the type of element in the given collection
      Parameters:
      elements - the elements to write to the output
      beanType - the type of element in the given collection
      output - the output into which the given elements will be written
      encoding - the output encoding to use for writing
      headers - headers to use in the first row of the written result.
    • writeAll

      public <T> void writeAll(Iterable<T> elements, Class<T> beanType, Writer output, String... headers)
      Writes a collection of annotated java beans to a given output.
      Type Parameters:
      T - the type of element in the given collection
      Parameters:
      elements - the elements to write to the output
      beanType - the type of element in the given collection
      output - the output into which the given elements will be written
      headers - headers to use in the first row of the written result.
    • parseAll

      public <T> List<T> parseAll(Class<T> beanType, File input, int expectedBeanCount)
      Parses a file into a list of annotated java beans
      Type Parameters:
      T - the type of java beans to be instantiated.
      Parameters:
      beanType - the type of java beans to be instantiated.
      input - the file to be parsed
      expectedBeanCount - expected number of rows to be parsed from the input which will be converted into java beans. Used to pre-allocate the size of the output List
      Returns:
      an Iterable that allows iterating over the input and producing instances of java beans on demand.
    • parseAll

      public <T> List<T> parseAll(Class<T> beanType, File input, String encoding, int expectedBeanCount)
      Parses a file into a list of annotated java beans
      Type Parameters:
      T - the type of java beans to be instantiated.
      Parameters:
      beanType - the type of java beans to be instantiated.
      input - the file to be parsed
      encoding - encoding of the given file
      expectedBeanCount - expected number of rows to be parsed from the input which will be converted into java beans. Used to pre-allocate the size of the output List
      Returns:
      an Iterable that allows iterating over the input and producing instances of java beans on demand.
    • parseAll

      public <T> List<T> parseAll(Class<T> beanType, File input, Charset encoding, int expectedBeanCount)
      Parses a file into a list of annotated java beans
      Type Parameters:
      T - the type of java beans to be instantiated.
      Parameters:
      beanType - the type of java beans to be instantiated.
      input - the file to be parsed
      encoding - encoding of the given file
      expectedBeanCount - expected number of rows to be parsed from the input which will be converted into java beans. Used to pre-allocate the size of the output List
      Returns:
      a list containing all java beans read from the input.
    • parseAll

      public <T> List<T> parseAll(Class<T> beanType, InputStream input, int expectedBeanCount)
      Parses an input stream into a list of annotated java beans
      Type Parameters:
      T - the type of java beans to be instantiated.
      Parameters:
      beanType - the type of java beans to be instantiated.
      input - the input stream to be parsed
      expectedBeanCount - expected number of rows to be parsed from the input which will be converted into java beans. Used to pre-allocate the size of the output List
      Returns:
      a list containing all java beans read from the input.
    • parseAll

      public <T> List<T> parseAll(Class<T> beanType, InputStream input, String encoding, int expectedBeanCount)
      Parses an input stream into a list of annotated java beans
      Type Parameters:
      T - the type of java beans to be instantiated.
      Parameters:
      beanType - the type of java beans to be instantiated.
      input - the input stream to be parsed
      encoding - encoding of the given input stream
      expectedBeanCount - expected number of rows to be parsed from the input which will be converted into java beans. Used to pre-allocate the size of the output List
      Returns:
      a list containing all java beans read from the input.
    • parseAll

      public <T> List<T> parseAll(Class<T> beanType, InputStream input, Charset encoding, int expectedBeanCount)
      Parses an input stream into a list of annotated java beans
      Type Parameters:
      T - the type of java beans to be instantiated.
      Parameters:
      beanType - the type of java beans to be instantiated.
      input - the input stream to be parsed
      encoding - encoding of the given input stream
      expectedBeanCount - expected number of rows to be parsed from the input which will be converted into java beans. Used to pre-allocate the size of the output List
      Returns:
      a list containing all java beans read from the input.
    • parseAll

      public <T> List<T> parseAll(Class<T> beanType, Reader input, int expectedBeanCount)
      Parses the input into a list of annotated java beans
      Type Parameters:
      T - the type of java beans to be instantiated.
      Parameters:
      beanType - the type of java beans to be instantiated.
      input - the input to be parsed
      expectedBeanCount - expected number of rows to be parsed from the input which will be converted into java beans. Used to pre-allocate the size of the output List
      Returns:
      a list containing all java beans read from the input.
    • parseAll

      public <T> List<T> parseAll(Class<T> beanType, File input)
      Parses a file into a list of annotated java beans
      Type Parameters:
      T - the type of java beans to be instantiated.
      Parameters:
      beanType - the type of java beans to be instantiated.
      input - the file to be parsed
      Returns:
      an Iterable that allows iterating over the input and producing instances of java beans on demand.
    • parseAll

      public <T> List<T> parseAll(Class<T> beanType, File input, String encoding)
      Parses a file into a list of annotated java beans
      Type Parameters:
      T - the type of java beans to be instantiated.
      Parameters:
      beanType - the type of java beans to be instantiated.
      input - the file to be parsed
      encoding - encoding of the given file
      Returns:
      an Iterable that allows iterating over the input and producing instances of java beans on demand.
    • parseAll

      public <T> List<T> parseAll(Class<T> beanType, File input, Charset encoding)
      Parses a file into a list of annotated java beans
      Type Parameters:
      T - the type of java beans to be instantiated.
      Parameters:
      beanType - the type of java beans to be instantiated.
      input - the file to be parsed
      encoding - encoding of the given file
      Returns:
      a list containing all java beans read from the input.
    • parseAll

      public <T> List<T> parseAll(Class<T> beanType, InputStream input)
      Parses an input stream into a list of annotated java beans
      Type Parameters:
      T - the type of java beans to be instantiated.
      Parameters:
      beanType - the type of java beans to be instantiated.
      input - the input stream to be parsed
      Returns:
      a list containing all java beans read from the input.
    • parseAll

      public <T> List<T> parseAll(Class<T> beanType, InputStream input, String encoding)
      Parses an input stream into a list of annotated java beans
      Type Parameters:
      T - the type of java beans to be instantiated.
      Parameters:
      beanType - the type of java beans to be instantiated.
      input - the input stream to be parsed
      encoding - encoding of the given input stream
      Returns:
      a list containing all java beans read from the input.
    • parseAll

      public <T> List<T> parseAll(Class<T> beanType, InputStream input, Charset encoding)
      Parses an input stream into a list of annotated java beans
      Type Parameters:
      T - the type of java beans to be instantiated.
      Parameters:
      beanType - the type of java beans to be instantiated.
      input - the input stream to be parsed
      encoding - encoding of the given input stream
      Returns:
      a list containing all java beans read from the input.
    • parseAll

      public <T> List<T> parseAll(Class<T> beanType, Reader input)
      Parses the input into a list of annotated java beans
      Type Parameters:
      T - the type of java beans to be instantiated.
      Parameters:
      beanType - the type of java beans to be instantiated.
      input - the input to be parsed
      Returns:
      a list containing all java beans read from the input.
    • iterate

      public <T> IterableResult<T,ParsingContext> iterate(Class<T> beanType, File input)
      Iterates over a file to produce instances of annotated java beans on demand.
      Type Parameters:
      T - the type of java beans to be instantiated.
      Parameters:
      beanType - the type of java beans to be instantiated.
      input - the file to be parsed
      Returns:
      an Iterable that allows iterating over the input and producing instances of java beans on demand.
    • iterate

      public <T> IterableResult<T,ParsingContext> iterate(Class<T> beanType, File input, String encoding)
      Iterates over a file to produce instances of annotated java beans on demand.
      Type Parameters:
      T - the type of java beans to be instantiated.
      Parameters:
      beanType - the type of java beans to be instantiated.
      input - the file to be parsed
      encoding - encoding of the given file
      Returns:
      an Iterable that allows iterating over the input and producing instances of java beans on demand.
    • iterate

      public <T> IterableResult<T,ParsingContext> iterate(Class<T> beanType, File input, Charset encoding)
      Iterates over a file to produce instances of annotated java beans on demand.
      Type Parameters:
      T - the type of java beans to be instantiated.
      Parameters:
      beanType - the type of java beans to be instantiated.
      input - the file to be parsed
      encoding - encoding of the given file
      Returns:
      an Iterable that allows iterating over the input and producing instances of java beans on demand.
    • iterate

      public <T> IterableResult<T,ParsingContext> iterate(Class<T> beanType, InputStream input)
      Iterates over an input stream to produce instances of annotated java beans on demand.
      Type Parameters:
      T - the type of java beans to be instantiated.
      Parameters:
      beanType - the type of java beans to be instantiated.
      input - the input stream to be parsed
      Returns:
      an Iterable that allows iterating over the input and producing instances of java beans on demand.
    • iterate

      public <T> IterableResult<T,ParsingContext> iterate(Class<T> beanType, InputStream input, String encoding)
      Iterates over an input stream to produce instances of annotated java beans on demand.
      Type Parameters:
      T - the type of java beans to be instantiated.
      Parameters:
      beanType - the type of java beans to be instantiated.
      input - the input stream to be parsed
      encoding - encoding of the given input stream
      Returns:
      an Iterable that allows iterating over the input and producing instances of java beans on demand.
    • iterate

      public <T> IterableResult<T,ParsingContext> iterate(Class<T> beanType, InputStream input, Charset encoding)
      Iterates over an input stream to produce instances of annotated java beans on demand.
      Type Parameters:
      T - the type of java beans to be instantiated.
      Parameters:
      beanType - the type of java beans to be instantiated.
      input - the input stream to be parsed
      encoding - encoding of the given input stream
      Returns:
      an Iterable that allows iterating over the input and producing instances of java beans on demand.
    • iterate

      public <T> IterableResult<T,ParsingContext> iterate(Class<T> beanType, Reader input)
      Iterates over an input to produce instances of annotated java beans on demand.
      Type Parameters:
      T - the type of java beans to be instantiated.
      Parameters:
      beanType - the type of java beans to be instantiated.
      input - the input to be parsed
      Returns:
      an Iterable that allows iterating over the input and producing instances of java beans on demand.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getInputDimension

      public InputDimension getInputDimension(File input)
      Calculates the dimensions of a file (row and column count).
      Parameters:
      input - the file to be parsed
      Returns:
      a InputDimension with information about the dimensions of the given input.
    • getInputDimension

      public InputDimension getInputDimension(File input, String encoding)
      Calculates the dimensions of a file (row and column count).
      Parameters:
      input - the file to be parsed
      encoding - encoding of the given file
      Returns:
      a InputDimension with information about the dimensions of the given input.
    • getInputDimension

      public InputDimension getInputDimension(InputStream input)
      Calculates the dimensions of a given input (row and column count).
      Parameters:
      input - the input to be parsed
      Returns:
      a InputDimension with information about the dimensions of the given input.
    • getInputDimension

      public InputDimension getInputDimension(InputStream input, String encoding)
      Calculates the dimensions of a given input (row and column count).
      Parameters:
      input - the input to be parsed
      encoding - encoding of the given input
      Returns:
      a InputDimension with information about the dimensions of the given input.
    • getInputDimension

      public InputDimension getInputDimension(Reader input)
      Calculates the dimensions of a given input (row and column count).
      Parameters:
      input - the input to be parsed
      Returns:
      a InputDimension with information about the dimensions of the given input.
    • getKeepResourcesOpen

      public boolean getKeepResourcesOpen()
      Returns a flag indicating whether resources used for writing should be kept open after being used by the routines available from this object, when applicable.
      Returns:
      flag indicating whether to call the close() (or any other cleanup method) after a routine executes.
    • setKeepResourcesOpen

      public void setKeepResourcesOpen(boolean keepResourcesOpen)
      Allows preventing resources used for writing from being closed after being used by the routines available from this object, when applicable.
      Parameters:
      keepResourcesOpen - flag indicating whether to call the close() (or any other cleanup method) after a routine executes.
    • getColumnMapper

      public ColumnMapper getColumnMapper()
      Returns a mapper that allows users to manually define mappings from attributes/methods of a given class to columns to be parsed or written in routines that manipulate java beans. This allows users to use instances of classes that are not annotated with Parsed nor Nested. Any mappings defined with the column mapper will take precedence over these annotations.
      Returns:
      the column mapper
    • setColumnMapper

      public void setColumnMapper(ColumnMapper columnMapper)
      Copies the given column mappings over to this processor. Further changes to the given object won't be reflected on the copy stored internally.
      Parameters:
      columnMapper - the column mappings to use