Class CSVRecord

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<java.lang.String>

    public final class CSVRecord
    extends java.lang.Object
    implements java.io.Serializable, java.lang.Iterable<java.lang.String>
    A CSV record parsed from a CSV file.
    Version:
    $Id: CSVRecord.java 1727809 2016-01-31 13:08:33Z sebb $
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private long characterPosition  
      private java.lang.String comment
      The accumulated comments (if any)
      private static java.lang.String[] EMPTY_STRING_ARRAY  
      private java.util.Map<java.lang.String,​java.lang.Integer> mapping
      The column name to index mapping.
      private long recordNumber
      The record number.
      private static long serialVersionUID  
      private java.lang.String[] values
      The values of the record
    • Constructor Summary

      Constructors 
      Constructor Description
      CSVRecord​(java.lang.String[] values, java.util.Map<java.lang.String,​java.lang.Integer> mapping, java.lang.String comment, long recordNumber, long characterPosition)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String get​(int i)
      Returns a value by index.
      java.lang.String get​(java.lang.Enum<?> e)
      Returns a value by Enum.
      java.lang.String get​(java.lang.String name)
      Returns a value by name.
      long getCharacterPosition()
      Returns the start position of this record as a character position in the source stream.
      java.lang.String getComment()
      Returns the comment for this record, if any.
      long getRecordNumber()
      Returns the number of this record in the parsed CSV file.
      boolean hasComment()
      Checks whether this record has a comment, false otherwise.
      boolean isConsistent()
      Tells whether the record size matches the header size.
      boolean isMapped​(java.lang.String name)
      Checks whether a given column is mapped, i.e.
      boolean isSet​(java.lang.String name)
      Checks whether a given columns is mapped and has a value.
      java.util.Iterator<java.lang.String> iterator()
      Returns an iterator over the values of this record.
      (package private) <M extends java.util.Map<java.lang.String,​java.lang.String>>
      M
      putIn​(M map)
      Puts all values of this record into the given Map.
      int size()
      Returns the number of values in this record.
      private java.util.List<java.lang.String> toList()
      Converts the values to a List.
      java.util.Map<java.lang.String,​java.lang.String> toMap()
      Copies this record into a new Map.
      java.lang.String toString()
      Returns a string representation of the contents of this record.
      (package private) java.lang.String[] values()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Field Detail

      • EMPTY_STRING_ARRAY

        private static final java.lang.String[] EMPTY_STRING_ARRAY
      • characterPosition

        private final long characterPosition
      • comment

        private final java.lang.String comment
        The accumulated comments (if any)
      • mapping

        private final java.util.Map<java.lang.String,​java.lang.Integer> mapping
        The column name to index mapping.
      • recordNumber

        private final long recordNumber
        The record number.
      • values

        private final java.lang.String[] values
        The values of the record
    • Constructor Detail

      • CSVRecord

        CSVRecord​(java.lang.String[] values,
                  java.util.Map<java.lang.String,​java.lang.Integer> mapping,
                  java.lang.String comment,
                  long recordNumber,
                  long characterPosition)
    • Method Detail

      • get

        public java.lang.String get​(java.lang.Enum<?> e)
        Returns a value by Enum.
        Parameters:
        e - an enum
        Returns:
        the String at the given enum String
      • get

        public java.lang.String get​(int i)
        Returns a value by index.
        Parameters:
        i - a column index (0-based)
        Returns:
        the String at the given index
      • get

        public java.lang.String get​(java.lang.String name)
        Returns a value by name.
        Parameters:
        name - the name of the column to be retrieved.
        Returns:
        the column value, maybe null depending on CSVFormat.getNullString().
        Throws:
        java.lang.IllegalStateException - if no header mapping was provided
        java.lang.IllegalArgumentException - if name is not mapped or if the record is inconsistent
        See Also:
        isConsistent(), CSVFormat.withNullString(String)
      • getCharacterPosition

        public long getCharacterPosition()
        Returns the start position of this record as a character position in the source stream. This may or may not correspond to the byte position depending on the character set.
        Returns:
        the position of this record in the source stream.
      • getComment

        public java.lang.String getComment()
        Returns the comment for this record, if any. Note that comments are attached to the following record. If there is no following record (i.e. the comment is at EOF) the comment will be ignored.
        Returns:
        the comment for this record, or null if no comment for this record is available.
      • getRecordNumber

        public long getRecordNumber()
        Returns the number of this record in the parsed CSV file.

        ATTENTION: If your CSV input has multi-line values, the returned number does not correspond to the current line number of the parser that created this record.

        Returns:
        the number of this record.
        See Also:
        CSVParser.getCurrentLineNumber()
      • isConsistent

        public boolean isConsistent()
        Tells whether the record size matches the header size.

        Returns true if the sizes for this record match and false if not. Some programs can export files that fail this test but still produce parsable files.

        Returns:
        true of this record is valid, false if not
      • hasComment

        public boolean hasComment()
        Checks whether this record has a comment, false otherwise. Note that comments are attached to the following record. If there is no following record (i.e. the comment is at EOF) the comment will be ignored.
        Returns:
        true if this record has a comment, false otherwise
        Since:
        1.3
      • isMapped

        public boolean isMapped​(java.lang.String name)
        Checks whether a given column is mapped, i.e. its name has been defined to the parser.
        Parameters:
        name - the name of the column to be retrieved.
        Returns:
        whether a given column is mapped.
      • isSet

        public boolean isSet​(java.lang.String name)
        Checks whether a given columns is mapped and has a value.
        Parameters:
        name - the name of the column to be retrieved.
        Returns:
        whether a given columns is mapped and has a value
      • iterator

        public java.util.Iterator<java.lang.String> iterator()
        Returns an iterator over the values of this record.
        Specified by:
        iterator in interface java.lang.Iterable<java.lang.String>
        Returns:
        an iterator over the values of this record.
      • putIn

        <M extends java.util.Map<java.lang.String,​java.lang.String>> M putIn​(M map)
        Puts all values of this record into the given Map.
        Parameters:
        map - The Map to populate.
        Returns:
        the given map.
      • size

        public int size()
        Returns the number of values in this record.
        Returns:
        the number of values.
      • toList

        private java.util.List<java.lang.String> toList()
        Converts the values to a List. TODO: Maybe make this public?
        Returns:
        a new List
      • toMap

        public java.util.Map<java.lang.String,​java.lang.String> toMap()
        Copies this record into a new Map. The new map is not connect
        Returns:
        A new Map. The map is empty if the record has no headers.
      • toString

        public java.lang.String toString()
        Returns a string representation of the contents of this record. The result is constructed by comment, mapping, recordNumber and by passing the internal values array to Arrays.toString(Object[]).
        Overrides:
        toString in class java.lang.Object
        Returns:
        a String representation of this record.
      • values

        java.lang.String[] values()