Class CsvRecord

java.lang.Object
de.siegmar.fastcsv.reader.CsvRecord
Direct Known Subclasses:
NamedCsvRecord

public class CsvRecord extends 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:
  • Field Summary

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

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

    Modifier and Type
    Method
    Description
    getField(int index)
    Retrieves the value of a field based on its index, with indexing starting from 0.
    int
    Gets the count of fields in this record.
    Retrieves all fields of this record as an unmodifiable list.
    long
    Provides the line number at which this record originated, starting from 1.
    boolean
    Indicates whether the record is a commented record.
     

    Methods inherited from class java.lang.Object

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

    • startingLineNumber

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

      final String[] fields
      The fields this record is composed of.
      See Also:
    • comment

      final boolean comment
      If the record is a commented record.
      See Also:
  • Constructor Details

    • CsvRecord

      CsvRecord(long startingLineNumber, String[] fields, boolean comment)
  • Method Details

    • 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 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:
      IndexOutOfBoundsException - if the index is out of range
    • getFields

      public List<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
    • getFieldCount

      public int getFieldCount()
      Gets the count of fields in this record.

      The minimum number of fields is 1, even if the line was empty.

      Returns:
      the number of fields in this record
      See Also:
    • isComment

      public boolean isComment()
      Indicates whether the record is a commented record.

      Retrieve the comment by calling getField(int) with index 0.

      Returns:
      true if the record is a commented record
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Object