Class CsvRecord

  • Direct Known Subclasses:
    NamedCsvRecord

    public class CsvRecord
    extends java.lang.Object
    Represents an immutable CSV record with unnamed (indexed) fields.

    The field values are never null. Empty fields are represented as empty strings.

    CSV records are created by CsvReader or IndexedCsvReader.

    See Also:
    CsvReader, IndexedCsvReader
    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) boolean comment
      If the record is a commented record.
      (package private) java.lang.String[] fields
      The fields this record is composed of.
      (package private) long startingLineNumber
      The starting line number (starting with 1).
    • Constructor Summary

      Constructors 
      Constructor Description
      CsvRecord​(long startingLineNumber, java.lang.String[] fields, boolean comment)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String getField​(int index)
      Retrieves the value of a field based on its index, with indexing starting from 0.
      int getFieldCount()
      Gets the count of fields in this record.
      java.util.List<java.lang.String> getFields()
      Retrieves all fields of this record as an unmodifiable list.
      long getStartingLineNumber()
      Provides the line number at which this record originated, starting from 1.
      boolean isComment()
      Indicates whether the record is a commented record.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • startingLineNumber

        final long startingLineNumber
        The starting line number (starting with 1).
        See Also:
        getStartingLineNumber()
      • comment

        final boolean comment
        If the record is a commented record.
        See Also:
        isComment()
    • Constructor Detail

      • CsvRecord

        CsvRecord​(long startingLineNumber,
                  java.lang.String[] fields,
                  boolean comment)
    • Method Detail

      • getStartingLineNumber

        public long getStartingLineNumber()
        Provides the line number at which this record originated, starting from 1.

        This information is particularly valuable in scenarios involving CSV files containing empty lines as well as multi-line or commented records, where the record number may deviate from the line number.

        Example:

         1 foo,bar
         2 foo,"multi
         3 line bar"
         4                    (empty, potentially skipped)
         5 #commented record  (potentially skipped)
         6 "latest
         7 record"
         
        The last record (containing the multi-line field "latest\nrecord") would have a starting line number of 6, no matter if empty lines or commented records are skipped or not.

        A starting offset of 1 is used to be consistent with the line numbers shown of most text editors.

        Note that this number is only correct if the CSV data was read from the very beginning. If you passed a Reader to the CsvReader and have already read from it, the line number will be incorrect.

        Returns:
        the starting line number of this record, starting from 1
      • getField

        public java.lang.String getField​(int index)
        Retrieves the value of a field based on its index, with indexing starting from 0.

        There is always at least one field, even if the line was empty.

        If this records holds a comment, the comment is returned by calling this method with index 0. The comment character is not included in the returned value.

        Parameters:
        index - index of the field to return
        Returns:
        field value, never null
        Throws:
        java.lang.IndexOutOfBoundsException - if the index is out of range
      • getFields

        public java.util.List<java.lang.String> getFields()
        Retrieves all fields of this record as an unmodifiable list.

        The returned list has a minimum size of 1, even if the line was empty. For empty lines, the first field is an empty string.

        Returns:
        all fields of this record, never null
      • toString

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