Class CsvRow


  • public final class CsvRow
    extends 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 Detail

      • headers

        public ImmutableList<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 ImmutableList<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 String field​(int index)
        Gets the specified field.
        Parameters:
        index - the field index
        Returns:
        the field
        Throws:
        IndexOutOfBoundsException - if the field index is invalid
      • getField

        public String getField​(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 field value, trimmed unless surrounded by quotes
        Throws:
        IllegalArgumentException - if the header is not found
      • getField

        public <T> T getField​(String header,
                              Function<String,​T> postProcessor)
        Gets a single field value from the row by header, post processing the result.

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

        The value is post processed via the specified function.

        Type Parameters:
        T - the post process result type
        Parameters:
        header - the column header
        postProcessor - the post processor
        Returns:
        the post processed field value
        Throws:
        IllegalArgumentException - if the header is not found
      • findField

        public Optional<String> findField​(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.

        If the value needs post processing, use Optional.map(Function).

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

        public String getField​(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 field value, trimmed unless surrounded by quotes
        Throws:
        IllegalArgumentException - if the header is not found
      • getField

        public <T> T getField​(Pattern headerPattern,
                              Function<String,​T> postProcessor)
        Gets a single field value from the row by header pattern, post processing the result.

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

        The value is post processed via the specified function.

        Type Parameters:
        T - the post process result type
        Parameters:
        headerPattern - the header pattern to match
        postProcessor - the post processor
        Returns:
        the post processed field value
        Throws:
        IllegalArgumentException - if the header is not found
      • findField

        public Optional<String> findField​(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 value needs post processing, use Optional.map(Function).

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

        public String getValue​(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 field value, trimmed unless surrounded by quotes
        Throws:
        IllegalArgumentException - if the header is not found or if the value in the field is empty.
      • getValue

        public <T> T getValue​(String header,
                              Function<String,​T> postProcessor)
        Gets a single field value from the row by header, post processing the result.

        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.

        The value is post processed via the specified function.

        Type Parameters:
        T - the post process result type
        Parameters:
        header - the column header
        postProcessor - the post processor
        Returns:
        the post processed field value
        Throws:
        IllegalArgumentException - if the header is not found or if the value in the field is empty.
      • findValue

        public Optional<String> findValue​(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.

        If the value needs post processing, use findValue(String, Function).

        Parameters:
        header - the column header
        Returns:
        the field value, trimmed unless surrounded by quotes, empty if not found
      • findValue

        public <T> Optional<T> findValue​(String header,
                                         Function<String,​T> postProcessor)
        Gets a single value from the row by header pattern, post processing the result.

        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.

        Type Parameters:
        T - the post process result type
        Parameters:
        header - the column header
        postProcessor - the post processor
        Returns:
        the field value, trimmed unless surrounded by quotes, empty if not found
      • getValue

        public String getValue​(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 field value, trimmed unless surrounded by quotes
        Throws:
        IllegalArgumentException - if the header is not found or if the value in the field is empty.
      • getValue

        public <T> T getValue​(Pattern headerPattern,
                              Function<String,​T> postProcessor)
        Gets a single field value from the row by header pattern, post processing the result.

        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.

        The value is post processed via the specified function.

        Type Parameters:
        T - the post process result type
        Parameters:
        headerPattern - the header pattern to match
        postProcessor - the post processor
        Returns:
        the post processed field value
        Throws:
        IllegalArgumentException - if the header is not found or if the value in the field is empty.
      • findValue

        public Optional<String> findValue​(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.

        If the value needs post processing, use findValue(Pattern, Function).

        Parameters:
        headerPattern - the header pattern to match
        Returns:
        the field value, trimmed unless surrounded by quotes, empty if not found
      • findValue

        public <T> Optional<T> findValue​(Pattern headerPattern,
                                         Function<String,​T> postProcessor)
        Gets a single value from the row by header pattern, post processing the result.

        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.

        Type Parameters:
        T - the post process result type
        Parameters:
        headerPattern - the header pattern to match
        postProcessor - the post processor
        Returns:
        the field value, trimmed unless surrounded by quotes, empty if not found
      • 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​(Object obj)
        Checks if this CSV file equals another.

        The comparison checks the content.

        Overrides:
        equals in class 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 Object
        Returns:
        the hash code
      • toString

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