Top Description Inners Fields Constructors Methods
com.opencsv.bean

public Class CsvToBean<T>

extends Object
implements Iterable<T>
Class Inheritance
All Implemented Interfaces
java.lang.Iterable
Type Parameters
<T>
Class to convert the objects to.
Imports
com.opencsv.CSVReader, .ICSVParser, com.opencsv.bean.concurrent.CompleteFileReader, .LineExecutor, .ProcessCsvLine, .SingleLineReader, com.opencsv.bean.exceptionhandler.CsvExceptionHandler, .ExceptionHandlerQueue, .ExceptionHandlerThrow, com.opencsv.bean.util.OrderedObject, com.opencsv.exceptions.CsvException, .CsvValidationException, org.apache.commons.lang3.ObjectUtils, java.io.IOException, java.util.*, java.util.concurrent.ArrayBlockingQueue, .BlockingQueue, .LinkedBlockingQueue, java.util.stream.Collectors, .Stream, .StreamSupport

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.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
CsvToBean.CsvToBeanIterator

A private inner class for implementing an iterator for the input data.

Field Summary

Modifier and TypeField and Description
private final 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 Locale
errorLocale

The errorLocale for error messages.

private CsvExceptionHandler
exceptionHandler

Determines how exceptions thrown during processing will be handled.

private LineExecutor<T>
executor

The java.util.concurrent.ExecutorService 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 List<BeanVerifier<T>>
verifiers

All verifiers that should be run on beans after creation but before returning them to the caller.

Constructor Summary

AccessConstructor and Description
public
CsvToBean()

Default constructor.

Method Summary

Modifier and TypeMethod and Description
public List<CsvException>

Returns:

The list of exceptions captured while processing the input file
getCapturedExceptions
()

Returns the list of all exceptions that would have been thrown during the import, but were queued by the exception handler.

pack-priv CsvExceptionHandler

Returns:

CsvExceptionHandler used by the CsvToBean object.
getExceptionHandler
()

Package scope method currently used by the CsvToBeanBuilderTest

public Iterator<T>

Returns:

An iterator over the beans created from the input
iterator
()

Implements java.lang.Iterable.iterator.

The iterator returned by this method takes one line of input at a time and returns one bean at a time.

public List<T>

Returns:

A list of populated beans based on the input
parse
()

Parses the input based on parameters already set through other methods.

private void
public void
setCsvReader(CSVReader
Reader for input
csvReader
)

Sets the reader to be used to read in the information from the CSV input.

public void
setErrorLocale(Locale
Locale for error messages. If null, the default locale is used.
errorLocale
)

Sets the locale for error messages.

public void
setExceptionHandler(CsvExceptionHandler
The exception handler to be used. If null, this method does nothing.
handler
)

Sets the handler for recoverable exceptions raised during processing of records.

public void
setFilter(CsvToBeanFilter
A class that filters the input lines
filter
)

Sets a filter to selectively remove some lines of input before they become beans.

public void
setIgnoreEmptyLines(boolean
true to ignore empty lines, false otherwise
ignoreEmptyLines
)

Ignores any blank lines in the data that are not part of a field.

public void
setMappingStrategy(MappingStrategy<? extends T>
Mapping strategy to convert CSV input to a bean
mappingStrategy
)

Sets the mapping strategy to be used by this bean.

public void
setOrderedResults(boolean
Whether or not the beans returned are in the same order they appeared in the input
orderedResults
)

Sets whether or not results must be returned in the same order in which they appear in the input.

public void
setThrowExceptions(boolean
Whether or not to throw exceptions during processing
throwExceptions
)

Determines whether errors during import should be thrown or kept in a list for later retrieval via getCapturedExceptions().

public void
setVerifiers(List<BeanVerifier<T>>
A list of verifiers. May be null, in which case, no verifiers are run.
verifiers
)

Sets the list of verifiers to be run on all beans after creation.

public Stream<T>

Returns:

A stream of populated beans based on the input
stream
()

Parses the input based on parameters already set through other methods.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

capturedExceptionsback to summary
private final List<CsvException> capturedExceptions

A list of all exceptions during parsing and mapping of the input.

csvReaderback to summary
private CSVReader csvReader

The reader this class will use to access the data to be read.

errorLocaleback to summary
private Locale errorLocale

The errorLocale for error messages.

exceptionHandlerback to summary
private CsvExceptionHandler exceptionHandler

Determines how exceptions thrown during processing will be handled.

executorback to summary
private LineExecutor<T> executor

The java.util.concurrent.ExecutorService for parallel processing of beans.

filterback to summary
private CsvToBeanFilter filter

The filter this class will use on the beans it reads.

ignoreEmptyLinesback to summary
private boolean ignoreEmptyLines

When an empty line is encountered (not part of the data) then it is ignored. By default this is false, which means an exception is thrown if there are required fields or the number of fields do not match the number of headers.

mappingStrategyback to summary
private MappingStrategy<? extends T> mappingStrategy

The mapping strategy to be used by this CsvToBean.

orderedResultsback to summary
private boolean orderedResults

Determines whether resulting data sets have to be in the same order as the input.

verifiersback to summary
private List<BeanVerifier<T>> verifiers

All verifiers that should be run on beans after creation but before returning them to the caller.

Constructor Detail

CsvToBeanback to summary
public CsvToBean()

Default constructor.

Method Detail

getCapturedExceptionsback to summary
public 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:List<CsvException>

The list of exceptions captured while processing the input file

See Also
setExceptionHandler(CsvExceptionHandler), setThrowExceptions(boolean)
getExceptionHandlerback to summary
pack-priv CsvExceptionHandler getExceptionHandler()

Package scope method currently used by the CsvToBeanBuilderTest

Returns:CsvExceptionHandler

CsvExceptionHandler used by the CsvToBean object.

iteratorback to summary
public Iterator<T> iterator()

Implements java.lang.Iterable.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.

Returns:Iterator<T>

An iterator over the beans created from the input

Annotations
@Override
See Also
parse(), stream()
parseback to summary
public List<T> parse() throws IllegalStateException

Parses the input based on parameters already set through other methods.

Returns:List<T>

A list of populated beans based on the input

Exceptions
IllegalStateException:
If either MappingStrategy or CSVReader is not specified
See Also
stream(), iterator()
prepareToReadInputback to summary
private void prepareToReadInput() throws IllegalStateException
setCsvReaderback to summary
public void setCsvReader(CSVReader csvReader)

Sets the reader to be used to read in the information from the CSV input.

Parameters
csvReader:CSVReader

Reader for input

setErrorLocaleback to summary
public void setErrorLocale(Locale errorLocale)

Sets the locale for error messages.

Parameters
errorLocale:Locale

Locale for error messages. If null, the default locale is used.

Since
4.0
setExceptionHandlerback to summary
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 is ExceptionHandlerThrow.

Please note that if both this method and setThrowExceptions(boolean) are called, the last call wins.

Parameters
handler:CsvExceptionHandler

The exception handler to be used. If null, this method does nothing.

Since
5.2
setFilterback to summary
public void setFilter(CsvToBeanFilter filter)

Sets a filter to selectively remove some lines of input before they become beans.

Parameters
filter:CsvToBeanFilter

A class that filters the input lines

setIgnoreEmptyLinesback to summary
public void setIgnoreEmptyLines(boolean ignoreEmptyLines)

Ignores any blank lines in the data that are not part of a field.

Parameters
ignoreEmptyLines:boolean

true to ignore empty lines, false otherwise

setMappingStrategyback to summary
public void setMappingStrategy(MappingStrategy<? extends T> mappingStrategy)

Sets the mapping strategy to be used by this bean.

Parameters
mappingStrategy:MappingStrategy<? extends T>

Mapping strategy to convert CSV input to a bean

setOrderedResultsback to summary
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:boolean

Whether or not the beans returned are in the same order they appeared in the input

Since
4.0
setThrowExceptionsback to summary
public void setThrowExceptions(boolean throwExceptions)

Determines whether errors during import should be thrown or kept in a list for later retrieval via getCapturedExceptions().

This is a convenience function and is maintained for backwards compatibility. Passing in true is equivalent to setExceptionHandler(new ExceptionHandlerThrow()) and false is equivalent to setExceptionHandler(new ExceptionHandlerQueue())

Please note that if both this method and setExceptionHandler(CsvExceptionHandler) are called, the last call wins.

Parameters
throwExceptions:boolean

Whether or not to throw exceptions during processing

See Also
setExceptionHandler(CsvExceptionHandler)
setVerifiersback to summary
public void setVerifiers(List<BeanVerifier<T>> verifiers)

Sets the list of verifiers to be run on all beans after creation.

Parameters
verifiers:List<BeanVerifier<T>>

A list of verifiers. May be null, in which case, no verifiers are run.

Since
4.4
streamback to summary
public Stream<T> stream() throws 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 java.util.List. If you plan on further processing the results as a java.util.stream.Stream, use this method.

Returns:Stream<T>

A stream of populated beans based on the input

Exceptions
IllegalStateException:
If either MappingStrategy or CSVReader is not specified
See Also
parse(), iterator()
com.opencsv.bean back to summary

private Class CsvToBean.CsvToBeanIterator

extends Object
implements Iterator<T>
Class Inheritance
All Implemented Interfaces
java.util.Iterator

A private inner class for implementing an iterator for the input data.

Field Summary

Modifier and TypeField and Description
private T
private String[]
private long
private final SingleLineReader
private final BlockingQueue<OrderedObject<T>>
private final BlockingQueue<OrderedObject<CsvException>>

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public boolean
hasNext()

Implements java.util.Iterator.hasNext.

Returns true if the iteration has more elements.

public T
next()

Implements java.util.Iterator.next.

Returns the next element in the iteration.

private void
private void
private void
public void
remove()

Overrides default java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation).

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

beanback to summary
private T bean
lineback to summary
private String[] line
lineProcessedback to summary
private long lineProcessed
lineReaderback to summary
private final SingleLineReader lineReader
resultantBeansQueueback to summary
private final BlockingQueue<OrderedObject<T>> resultantBeansQueue
thrownExceptionsQueueback to summary
private final BlockingQueue<OrderedObject<CsvException>> thrownExceptionsQueue

Constructor Detail

CsvToBeanIteratorback to summary
pack-priv CsvToBeanIterator()

Method Detail

hasNextback to summary
public boolean hasNext()

Implements java.util.Iterator.hasNext.

Doc from java.util.Iterator.hasNext.

Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

Annotations
@Override
nextback to summary
public T next()

Implements java.util.Iterator.next.

Doc from java.util.Iterator.next.

Returns the next element in the iteration.

Annotations
@Override
processExceptionback to summary
private void processException()
readLineWithPossibleErrorback to summary
private void readLineWithPossibleError() throws IOException, CsvValidationException
readSingleLineback to summary
private void readSingleLine()
removeback to summary
public void remove()

Overrides default java.util.Iterator.remove.

Doc from java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next.

The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.

The behavior of an iterator is unspecified if this method is called after a call to the forEachRemaining method.

Annotations
@Override