Class CsvToBean<T>

  • Type Parameters:
    T - Class to convert the objects to.
    All Implemented Interfaces:
    java.lang.Iterable<T>

    public class CsvToBean<T>
    extends java.lang.Object
    implements java.lang.Iterable<T>
    Converts CSV data to objects. Mixing the parse() method with the Iterator is not supported and will lead to unpredictable results. Additionally, reusing an instance of this class after all beans have been read is not supported and will certainly break something.
    • Constructor Summary

      Constructors 
      Constructor Description
      CsvToBean()
      Default constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.List<CsvException> getCapturedExceptions()
      Returns the list of all exceptions that would have been thrown during the import, but were suppressed by setting throwExceptions to false.
      java.util.Iterator<T> iterator()
      The iterator returned by this method takes one line of input at a time and returns one bean at a time.
      java.util.List<T> parse()
      Parses the input based on parameters already set through other methods.
      void setCsvReader​(CSVReader csvReader)
      Sets the reader to be used to read in the information from the CSV input.
      void setErrorLocale​(java.util.Locale errorLocale)
      Sets the locale for error messages.
      void setFilter​(CsvToBeanFilter filter)
      Sets a filter to selectively remove some lines of input before they become beans.
      void setIgnoreEmptyLines​(boolean ignoreEmptyLines)
      Ignores any blank lines in the data that are not part of a field.
      void setMappingStrategy​(MappingStrategy<? extends T> mappingStrategy)
      Sets the mapping strategy to be used by this bean.
      void setOrderedResults​(boolean orderedResults)
      Sets whether or not results must be returned in the same order in which they appear in the input.
      void setThrowExceptions​(boolean throwExceptions)
      Determines whether errors during import should be thrown or kept in a list for later retrieval via getCapturedExceptions().
      void setVerifiers​(java.util.List<BeanVerifier<T>> verifiers)
      Sets the list of verifiers to be run on all beans after creation.
      java.util.stream.Stream<T> stream()
      Parses the input based on parameters already set through other methods.
      • Methods inherited from class java.lang.Object

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

        forEach, spliterator
    • Constructor Detail

      • CsvToBean

        public CsvToBean()
        Default constructor.
    • Method Detail

      • parse

        public java.util.List<T> parse()
                                throws java.lang.IllegalStateException
        Parses the input based on parameters already set through other methods.
        Returns:
        A list of populated beans based on the input
        Throws:
        java.lang.IllegalStateException - If either MappingStrategy or CSVReader is not specified
        See Also:
        stream(), iterator()
      • stream

        public java.util.stream.Stream<T> stream()
                                          throws java.lang.IllegalStateException
        Parses the input based on parameters already set through other methods. This method saves a marginal amount of time and storage compared to parse() because it avoids the intermediate storage of the results in a List. If you plan on further processing the results as a Stream, use this method.
        Returns:
        A stream of populated beans based on the input
        Throws:
        java.lang.IllegalStateException - If either MappingStrategy or CSVReader is not specified
        See Also:
        parse(), iterator()
      • getCapturedExceptions

        public java.util.List<CsvException> getCapturedExceptions()
        Returns the list of all exceptions that would have been thrown during the import, but were suppressed by setting throwExceptions to false.
        Returns:
        The list of exceptions captured while processing the input file
      • setMappingStrategy

        public void setMappingStrategy​(MappingStrategy<? extends T> mappingStrategy)
        Sets the mapping strategy to be used by this bean.
        Parameters:
        mappingStrategy - Mapping strategy to convert CSV input to a bean
      • setCsvReader

        public void setCsvReader​(CSVReader csvReader)
        Sets the reader to be used to read in the information from the CSV input.
        Parameters:
        csvReader - Reader for input
      • setFilter

        public void setFilter​(CsvToBeanFilter filter)
        Sets a filter to selectively remove some lines of input before they become beans.
        Parameters:
        filter - A class that filters the input lines
      • setThrowExceptions

        public void setThrowExceptions​(boolean throwExceptions)
        Determines whether errors during import should be thrown or kept in a list for later retrieval via getCapturedExceptions().
        Parameters:
        throwExceptions - Whether or not to throw exceptions during processing
      • setOrderedResults

        public void setOrderedResults​(boolean orderedResults)
        Sets whether or not results must be returned in the same order in which they appear in the input. The default is that order is preserved. If your data do not need to be ordered, you can get a slight performance boost by setting orderedResults to false. The lack of ordering then also applies to any captured exceptions, if you have chosen not to have exceptions thrown.
        Parameters:
        orderedResults - Whether or not the beans returned are in the same order they appeared in the input
        Since:
        4.0
      • setErrorLocale

        public void setErrorLocale​(java.util.Locale errorLocale)
        Sets the locale for error messages.
        Parameters:
        errorLocale - Locale for error messages. If null, the default locale is used.
        Since:
        4.0
      • setVerifiers

        public void setVerifiers​(java.util.List<BeanVerifier<T>> verifiers)
        Sets the list of verifiers to be run on all beans after creation.
        Parameters:
        verifiers - A list of verifiers. May be null, in which case, no verifiers are run.
        Since:
        4.4
      • iterator

        public java.util.Iterator<T> iterator()
        The iterator returned by this method takes one line of input at a time and returns one bean at a time.

        The advantage to this method is saving memory. The cost is the loss of parallel processing, reducing throughput.

        The iterator respects all aspects of CsvToBean, including filters and capturing exceptions.

        Specified by:
        iterator in interface java.lang.Iterable<T>
        Returns:
        An iterator over the beans created from the input
        See Also:
        parse(), stream()
      • setIgnoreEmptyLines

        public void setIgnoreEmptyLines​(boolean ignoreEmptyLines)
        Ignores any blank lines in the data that are not part of a field.
        Parameters:
        ignoreEmptyLines - - true to ignore empty lines, false otherwise