T
- Type of object you are converting the data to.public interface MappingStrategy<T>
Any implementing class must be thread-safe. Specifically, the following methods must be thread-safe:
Modifier and Type | Method and Description |
---|---|
void |
captureHeader(CSVReader reader)
Implementation of this method can grab the header line before parsing
begins to use to map columns to bean properties.
|
String[] |
generateHeader(T bean)
Implementations of this method must return an array of column headers
based on the contents of the mapping strategy.
|
default void |
ignoreFields(org.apache.commons.collections4.MultiValuedMap<Class<?>,Field> fields)
When processing a bean for reading or writing, ignore the given fields
from the given classes completely, including all annotations and
requirements.
|
default boolean |
isAnnotationDriven()
Deprecated.
This is simply no longer necessary.
|
T |
populateNewBean(String[] line)
Takes a line of input from a CSV file and creates a bean out of it.
|
default void |
setErrorLocale(Locale errorLocale)
Sets the locale for all error messages.
|
default void |
setProfile(String profile)
Sets the profile this mapping strategy will use when configuring bean
fields.
|
void |
setType(Class<? extends T> type)
Sets the class type that is being mapped.
|
String[] |
transmuteBean(T bean)
Transmutes a bean instance into an array of
String s to be written
to a CSV file. |
void captureHeader(CSVReader reader) throws IOException, CsvRequiredFieldEmptyException
reader
- The CSVReader to use for header parsingIOException
- If parsing failsCsvRequiredFieldEmptyException
- If a field is required, but the
header or column position for the field is not present in the inputString[] generateHeader(T bean) throws CsvRequiredFieldEmptyException
null
.bean
- One fully populated bean from which the header can be derived.
This is important in the face of joining and splitting. If we have a
MultiValuedMap as a field that is the target for a join on reading, that
same field must be split into multiple columns on writing. Since the
joining is done via regular expressions, it is impossible for opencsv
to know what the column names are supposed to be on writing unless this
bean includes a fully populated map.null
.CsvRequiredFieldEmptyException
- If a required header is missing
while attempting to write. Since every other header is hard-wired
through the bean fields and their associated annotations, this can only
happen with multi-valued fields.@Deprecated default boolean isAnnotationDriven()
T populateNewBean(String[] line) throws CsvBeanIntrospectionException, CsvFieldAssignmentException, CsvChainedException
line
- A line of input returned from CSVReader
CsvBeanIntrospectionException
- Generally, if some part of the bean cannot
be accessed and used as neededCsvFieldAssignmentException
- A more specific subclass of this
exception is thrown for any problem decoding and assigning a field
of the input to a bean fieldCsvChainedException
- If multiple exceptions are thrown for the
same input linedefault void setErrorLocale(Locale errorLocale)
errorLocale
- Locale for error messages. If null, the default locale
is used.void setType(Class<? extends T> type) throws CsvBadConverterException
setErrorLocale(Locale)
or setProfile(String)
.type
- Class type.CsvBadConverterException
- If a field in the bean is annotated
with a custom converter that cannot be initialized. If you are not
using custom converters that you have written yourself, it should be
safe to catch this exception and ignore it.default void setProfile(String profile)
The default implementation throws
UnsupportedOperationException
.
profile
- The profile to usedefault void ignoreFields(org.apache.commons.collections4.MultiValuedMap<Class<?>,Field> fields) throws IllegalArgumentException
When processing a bean for reading or writing, ignore the given fields from the given classes completely, including all annotations and requirements. This method has two compelling applications:
Calling this method overwrites the fields passed in from any previous
calls. It is legal to call this method before calling
setType(Class)
, and it may be more efficient to do so.
Caution must be exercised with this method when letting opencsv
automatically determine the mapping strategy. When a field is ignored,
opencsv pretends it does not exist at all. If, for instance, all fields
annotated with opencsv binding annotations are ignored, opencsv will
automatically switch to HeaderColumnNameMappingStrategy
and
assume header names exactly match field names.
An implementation of this interface is not required to implement this
method. The default implementation throws
UnsupportedOperationException
.
fields
- All fields to be ignored, mapped from the classes of which
they are members. These are the classes as opencsv
encounters them, not necessarily the declaring classes if
any fields are inherited. May be null
.IllegalArgumentException
- If any entry in the map has a
null
key, a null
value, or if the value is not a field
in the class represented by the keyCsvIgnore
String[] transmuteBean(T bean) throws CsvFieldAssignmentException, CsvChainedException
String
s to be written
to a CSV file.bean
- The bean to be transmutedCSVWriter
CsvFieldAssignmentException
- A more specific subclass of this
exception is thrown for any problem decoding and assigning a field
of the input to a bean fieldCsvChainedException
- If multiple exceptions are thrown for the
same input lineCopyright © 2005–2023. All rights reserved.