Class CsvToBean<T>
- java.lang.Object
-
- com.opencsv.bean.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>
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
CsvToBean.CsvToBeanIterator
A private inner class for implementing an iterator for the input data.
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<CsvException>
capturedExceptions
A list of all exceptions during parsing and mapping of the input.private CSVReader
csvReader
The reader this class will use to access the data to be read.private java.util.Locale
errorLocale
The errorLocale for error messages.private CsvExceptionHandler
exceptionHandler
Determines how exceptions thrown during processing will be handled.private LineExecutor<T>
executor
TheExecutorService
for parallel processing of beans.private CsvToBeanFilter
filter
The filter this class will use on the beans it reads.private boolean
ignoreEmptyLines
When an empty line is encountered (not part of the data) then it is ignored.private MappingStrategy<? extends T>
mappingStrategy
The mapping strategy to be used by this CsvToBean.private boolean
orderedResults
Determines whether resulting data sets have to be in the same order as the input.private java.util.List<BeanVerifier<T>>
verifiers
All verifiers that should be run on beans after creation but before returning them to the caller.
-
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 queued by the exception handler.(package private) CsvExceptionHandler
getExceptionHandler()
Package scope method currently used by the CsvToBeanBuilderTestjava.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.private void
prepareToReadInput()
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
setExceptionHandler(CsvExceptionHandler handler)
Sets the handler for recoverable exceptions raised during processing of records.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 viagetCapturedExceptions()
.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.
-
-
-
Field Detail
-
capturedExceptions
private final java.util.List<CsvException> capturedExceptions
A list of all exceptions during parsing and mapping of the input.
-
mappingStrategy
private MappingStrategy<? extends T> mappingStrategy
The mapping strategy to be used by this CsvToBean.
-
csvReader
private CSVReader csvReader
The reader this class will use to access the data to be read.
-
filter
private CsvToBeanFilter filter
The filter this class will use on the beans it reads.
-
exceptionHandler
private CsvExceptionHandler exceptionHandler
Determines how exceptions thrown during processing will be handled.
-
orderedResults
private boolean orderedResults
Determines whether resulting data sets have to be in the same order as the input.
-
executor
private LineExecutor<T> executor
TheExecutorService
for parallel processing of beans.
-
errorLocale
private java.util.Locale errorLocale
The errorLocale for error messages.
-
verifiers
private java.util.List<BeanVerifier<T>> verifiers
All verifiers that should be run on beans after creation but before returning them to the caller.
-
ignoreEmptyLines
private boolean ignoreEmptyLines
When 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.
-
-
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 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:
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 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:
setExceptionHandler(CsvExceptionHandler)
,setThrowExceptions(boolean)
-
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 viagetCapturedExceptions()
.This is a convenience function and is maintained for backwards compatibility. Passing in
true
is equivalent tosetExceptionHandler(new ExceptionHandlerThrow())
andfalse
is 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(CsvExceptionHandler)
-
setExceptionHandler
public void setExceptionHandler(CsvExceptionHandler handler)
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 settingorderedResults
tofalse
. 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 benull
, in which case, no verifiers are run.- Since:
- 4.4
-
prepareToReadInput
private void prepareToReadInput() throws java.lang.IllegalStateException
- Throws:
java.lang.IllegalStateException
-
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.
-
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
-
-