Class CsvRow


  • public final class CsvRow
    extends java.lang.Object
    A row in a CSV file.

    Represents a single row in a CSV file, accessed via CsvFile. Each row object provides access to the data in the row by field index. If the CSV file has headers, the headers can also be used to lookup the fields.

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object obj)
      Checks if this CSV file equals another.
      java.lang.String field​(int index)
      Gets the specified field.
      int fieldCount()
      Gets the number of fields.
      com.google.common.collect.ImmutableList<java.lang.String> fields()
      Gets all fields in the row.
      java.util.Optional<java.lang.String> findField​(java.lang.String header)
      Gets a single field value from the row by header.
      java.util.Optional<java.lang.String> findField​(java.util.regex.Pattern headerPattern)
      Gets a single field value from the row by header pattern.
      java.util.Optional<java.lang.String> findValue​(java.lang.String header)
      Gets a single value from the row by header.
      java.util.Optional<java.lang.String> findValue​(java.util.regex.Pattern headerPattern)
      Gets a single value from the row by header pattern.
      java.lang.String getField​(java.lang.String header)
      Gets a single field value from the row by header.
      java.lang.String getField​(java.util.regex.Pattern headerPattern)
      Gets a single field value from the row by header pattern.
      java.lang.String getValue​(java.lang.String header)
      Gets a single field value from the row by header
      java.lang.String getValue​(java.util.regex.Pattern headerPattern)
      Gets a single field value from the row by header pattern
      int hashCode()
      Returns a suitable hash code for the CSV file.
      com.google.common.collect.ImmutableList<java.lang.String> headers()
      Gets the header row.
      int lineNumber()
      Gets the line number in the source file.
      CsvRow subRow​(int startInclusive)
      Obtains a sub-row, containing a selection of fields by index.
      CsvRow subRow​(int startInclusive, int endExclusive)
      Obtains a sub-row, containing a selection of fields by index.
      java.lang.String toString()
      Returns a string describing the CSV file.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Method Detail

      • headers

        public com.google.common.collect.ImmutableList<java.lang.String> headers()
        Gets the header row.

        If there is no header row, an empty list is returned.

        Returns:
        the header row
      • lineNumber

        public int lineNumber()
        Gets the line number in the source file.
        Returns:
        the line number
      • fields

        public com.google.common.collect.ImmutableList<java.lang.String> fields()
        Gets all fields in the row.
        Returns:
        the fields
      • fieldCount

        public int fieldCount()
        Gets the number of fields.

        This will never be less than the number of headers.

        Returns:
        the number of fields
      • field

        public java.lang.String field​(int index)
        Gets the specified field.
        Parameters:
        index - the field index
        Returns:
        the field
        Throws:
        java.lang.IndexOutOfBoundsException - if the field index is invalid
      • getField

        public java.lang.String getField​(java.lang.String header)
        Gets a single field value from the row by header.

        This returns the value of the first column where the header matches the specified header. Matching is case insensitive.

        Parameters:
        header - the column header
        Returns:
        the trimmed field value
        Throws:
        java.lang.IllegalArgumentException - if the header is not found
      • findField

        public java.util.Optional<java.lang.String> findField​(java.lang.String header)
        Gets a single field value from the row by header.

        This returns the value of the first column where the header matches the specified header. Matching is case insensitive.

        Parameters:
        header - the column header
        Returns:
        the trimmed field value, empty if not found
      • getField

        public java.lang.String getField​(java.util.regex.Pattern headerPattern)
        Gets a single field value from the row by header pattern.

        This returns the value of the first column where the header matches the specified header pattern.

        Parameters:
        headerPattern - the header pattern to match
        Returns:
        the trimmed field value, empty
        Throws:
        java.lang.IllegalArgumentException - if the header is not found
      • findField

        public java.util.Optional<java.lang.String> findField​(java.util.regex.Pattern headerPattern)
        Gets a single field value from the row by header pattern.

        This returns the value of the first column where the header matches the specified header pattern.

        Parameters:
        headerPattern - the header pattern to match
        Returns:
        the trimmed field value, empty if not found
      • getValue

        public java.lang.String getValue​(java.lang.String header)
        Gets a single field value from the row by header

        This returns the value of the first column where the header matches the specified header. If the header is not found or the value found is an empty string, then an IllegalArgumentException is thrown.

        Parameters:
        header - the column header
        Returns:
        the trimmed field value, empty
        Throws:
        java.lang.IllegalArgumentException - if the header is not found or if the value in the field is empty.
      • findValue

        public java.util.Optional<java.lang.String> findValue​(java.lang.String header)
        Gets a single value from the row by header.

        This returns the value of the first column where the header matches the specified header pattern. If the value is an empty string, then an empty optional is returned.

        Parameters:
        header - the column header
        Returns:
        the trimmed field value, empty
      • getValue

        public java.lang.String getValue​(java.util.regex.Pattern headerPattern)
        Gets a single field value from the row by header pattern

        This returns the value of the first column where the header matches the specified header pattern. If the header is not found or the value found is an empty string, then an IllegalArgumentException is thrown.

        Parameters:
        headerPattern - the header pattern to match
        Returns:
        the trimmed field value
        Throws:
        java.lang.IllegalArgumentException - if the header is not found or if the value in the field is empty.
      • findValue

        public java.util.Optional<java.lang.String> findValue​(java.util.regex.Pattern headerPattern)
        Gets a single value from the row by header pattern.

        This returns the value of the first column where the header matches the specified header pattern. If the value is an empty string, then an empty optional is returned.

        Parameters:
        headerPattern - the header pattern to match
        Returns:
        the trimmed field value, empty
      • subRow

        public CsvRow subRow​(int startInclusive)
        Obtains a sub-row, containing a selection of fields by index.

        All fields after the specified index are included.

        Parameters:
        startInclusive - the start index, zero-based, inclusive
        Returns:
        the sub row
      • subRow

        public CsvRow subRow​(int startInclusive,
                             int endExclusive)
        Obtains a sub-row, containing a selection of fields by index.
        Parameters:
        startInclusive - the start index, zero-based, inclusive
        endExclusive - the end index, zero-based, exclusive
        Returns:
        the sub row
      • equals

        public boolean equals​(java.lang.Object obj)
        Checks if this CSV file equals another.

        The comparison checks the content.

        Overrides:
        equals in class java.lang.Object
        Parameters:
        obj - the other file, null returns false
        Returns:
        true if equal
      • hashCode

        public int hashCode()
        Returns a suitable hash code for the CSV file.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        the hash code
      • toString

        public java.lang.String toString()
        Returns a string describing the CSV file.
        Overrides:
        toString in class java.lang.Object
        Returns:
        the descriptive string