Class CsvToBean<T>
- Type Parameters:
T- Class to convert the objects to.
- All Implemented Interfaces:
Iterable<T>
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classA private inner class for implementing an iterator for the input data. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final List<CsvException> A list of all exceptions during parsing and mapping of the input.private CSVReaderThe reader this class will use to access the data to be read.private LocaleThe errorLocale for error messages.private CsvExceptionHandlerDetermines how exceptions thrown during processing will be handled.private LineExecutor<T> TheExecutorServicefor parallel processing of beans.private CsvToBeanFilterThe filter this class will use on the beans it reads.private booleanWhen an empty line is encountered (not part of the data) then it is ignored.private MappingStrategy<? extends T> The mapping strategy to be used by this CsvToBean.private booleanDetermines whether resulting data sets have to be in the same order as the input.private List<BeanVerifier<T>> All verifiers that should be run on beans after creation but before returning them to the caller. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns the list of all exceptions that would have been thrown during the import, but were queued by the exception handler.(package private) CsvExceptionHandlerPackage scope method currently used by the CsvToBeanBuilderTestiterator()The iterator returned by this method takes one line of input at a time and returns one bean at a time.parse()Parses the input based on parameters already set through other methods.private voidvoidsetCsvReader(CSVReader csvReader) Sets the reader to be used to read in the information from the CSV input.voidsetErrorLocale(Locale errorLocale) Sets the locale for error messages.voidsetExceptionHandler(CsvExceptionHandler handler) Sets the handler for recoverable exceptions raised during processing of records.voidsetFilter(CsvToBeanFilter filter) Sets a filter to selectively remove some lines of input before they become beans.voidsetIgnoreEmptyLines(boolean ignoreEmptyLines) Ignores any blank lines in the data that are not part of a field.voidsetMappingStrategy(MappingStrategy<? extends T> mappingStrategy) Sets the mapping strategy to be used by this bean.voidsetOrderedResults(boolean orderedResults) Sets whether or not results must be returned in the same order in which they appear in the input.voidsetThrowExceptions(boolean throwExceptions) Determines whether errors during import should be thrown or kept in a list for later retrieval viagetCapturedExceptions().voidsetVerifiers(List<BeanVerifier<T>> verifiers) Sets the list of verifiers to be run on all beans after creation.stream()Parses the input based on parameters already set through other methods.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Iterable
forEach, spliterator
-
Field Details
-
capturedExceptions
A list of all exceptions during parsing and mapping of the input. -
mappingStrategy
The mapping strategy to be used by this CsvToBean. -
csvReader
The reader this class will use to access the data to be read. -
filter
The filter this class will use on the beans it reads. -
exceptionHandler
Determines how exceptions thrown during processing will be handled. -
orderedResults
private boolean orderedResultsDetermines whether resulting data sets have to be in the same order as the input. -
executor
TheExecutorServicefor parallel processing of beans. -
errorLocale
The errorLocale for error messages. -
verifiers
All verifiers that should be run on beans after creation but before returning them to the caller. -
ignoreEmptyLines
private boolean ignoreEmptyLinesWhen an empty line is encountered (not part of the data) then it is ignored. By default this isfalse, which means an exception is thrown if there are required fields or the number of fields do not match the number of headers.
-
-
Constructor Details
-
CsvToBean
public CsvToBean()Default constructor.
-
-
Method Details
-
parse
Parses the input based on parameters already set through other methods.- Returns:
- A list of populated beans based on the input
- Throws:
IllegalStateException- If either MappingStrategy or CSVReader is not specified- See Also:
-
stream
Parses the input based on parameters already set through other methods. This method saves a marginal amount of time and storage compared toparse()because it avoids the intermediate storage of the results in aList. If you plan on further processing the results as aStream, use this method.- Returns:
- A stream of populated beans based on the input
- Throws:
IllegalStateException- If either MappingStrategy or CSVReader is not specified- See Also:
-
getCapturedExceptions
Returns the list of all exceptions that would have been thrown during the import, but were queued by the exception handler.The results returned by this method are not consistent until parsing is concluded.
- Returns:
- The list of exceptions captured while processing the input file
- See Also:
-
setMappingStrategy
Sets the mapping strategy to be used by this bean.- Parameters:
mappingStrategy- Mapping strategy to convert CSV input to a bean
-
setCsvReader
Sets the reader to be used to read in the information from the CSV input.- Parameters:
csvReader- Reader for input
-
setFilter
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 viagetCapturedExceptions().This is a convenience function and is maintained for backwards compatibility. Passing in
trueis equivalent tosetExceptionHandler(new ExceptionHandlerThrow())andfalseis equivalent tosetExceptionHandler(new ExceptionHandlerQueue())Please note that if both this method and
setExceptionHandler(CsvExceptionHandler)are called, the last call wins.- Parameters:
throwExceptions- Whether or not to throw exceptions during processing- See Also:
-
setExceptionHandler
Sets the handler for recoverable exceptions raised during processing of records.If neither this method nor
setThrowExceptions(boolean)is called, the default exception handler isExceptionHandlerThrow.Please note that if both this method and
setThrowExceptions(boolean)are called, the last call wins.- Parameters:
handler- The exception handler to be used. Ifnull, this method does nothing.- Since:
- 5.2
-
getExceptionHandler
CsvExceptionHandler getExceptionHandler()Package scope method currently used by the CsvToBeanBuilderTest- Returns:
- CsvExceptionHandler used by the CsvToBean object.
-
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 settingorderedResultstofalse. 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
Sets the locale for error messages.- Parameters:
errorLocale- Locale for error messages. If null, the default locale is used.- Since:
- 4.0
-
setVerifiers
Sets the list of verifiers to be run on all beans after creation.- Parameters:
verifiers- A list of verifiers. May benull, in which case, no verifiers are run.- Since:
- 4.4
-
prepareToReadInput
- Throws:
IllegalStateException
-
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. -
setIgnoreEmptyLines
public void setIgnoreEmptyLines(boolean ignoreEmptyLines) Ignores any blank lines in the data that are not part of a field.- Parameters:
ignoreEmptyLines-trueto ignore empty lines,falseotherwise
-