T
- Type of the bean being populatedI
- Type of the index into a multivalued fieldpublic abstract class AbstractBeanField<T,I> extends Object implements BeanField<T,I>
All custom converters must be descended from this class.
Internally, opencsv uses another set of classes for the actual conversion, leaving this class mostly to deal with assigment to bean fields.
Modifier and Type | Field and Description |
---|---|
protected CsvConverter |
converter
A class that converts from a string to the destination type on reading
and vice versa on writing.
|
protected Locale |
errorLocale
Locale for error messages.
|
protected Field |
field
The field this class represents.
|
protected FieldAccess<Object> |
fieldAccess
An encapsulated way of accessing the member variable associated with this
field.
|
protected boolean |
required
Whether or not this field is required.
|
protected Class<?> |
type
The type the field is located in.
|
Constructor and Description |
---|
AbstractBeanField()
Default nullary constructor, so derived classes aren't forced to create
a constructor identical to this one.
|
AbstractBeanField(Class<?> type,
Field field,
boolean required,
Locale errorLocale,
CsvConverter converter) |
Modifier and Type | Method and Description |
---|---|
protected void |
assignValueToField(Object bean,
Object obj,
String header)
Assigns the given object to this field of the destination bean.
|
protected abstract Object |
convert(String value)
Method for converting from a string to the proper datatype of the
destination field.
|
protected String |
convertToWrite(Object value)
This is the method that actually performs the conversion from field to
string for
write(java.lang.Object, java.lang.Object) and should
be overridden in derived classes. |
Locale |
getErrorLocale()
Returns the error locale for the beans.
|
Field |
getField()
Gets the field to be processed.
|
Object |
getFieldValue(Object bean)
Gets the contents of the selected field of the given bean.
|
Class<?> |
getType()
Gets the type of the bean this field is attached to.
|
Object[] |
indexAndSplitMultivaluedField(Object value,
I index)
Given the value of a bean field and an index into that value, determine
what values need to be written.
|
protected boolean |
isFieldEmptyForWrite(Object value)
Whether or not this implementation of
BeanField considers the
value passed in as empty for the purposes of determining whether or not
a required field is empty. |
boolean |
isRequired()
Answers the query, whether this field is required or not.
|
void |
setErrorLocale(Locale errorLocale)
Sets the locale for all error messages.
|
void |
setField(Field field)
Sets the field to be processed.
|
void |
setFieldValue(Object bean,
String value,
String header)
Populates the selected field of the bean.
|
void |
setRequired(boolean required)
Determines whether or not a field is required.
|
void |
setType(Class<?> type)
Sets the type of the bean this field is attached to.
|
String[] |
write(Object bean,
I index)
This method takes the current value of the field in question in the bean
passed in and converts it to a string.
|
protected Class<?> type
protected Field field
protected boolean required
protected Locale errorLocale
protected CsvConverter converter
protected FieldAccess<Object> fieldAccess
public AbstractBeanField()
public AbstractBeanField(Class<?> type, Field field, boolean required, Locale errorLocale, CsvConverter converter)
type
- The type of the class in which this field is found. This is
the type as instantiated by opencsv, and not necessarily the
type in which the field is declared in the case of
inheritance.field
- A Field
object.required
- Whether or not this field is required in inputerrorLocale
- The errorLocale to use for error messages.converter
- The converter to be used to perform the actual data
conversionpublic Class<?> getType()
BeanField
public void setType(Class<?> type)
BeanField
public void setField(Field field)
BeanField
public Field getField()
BeanField
public boolean isRequired()
BeanField
isRequired
in interface BeanField<T,I>
public void setRequired(boolean required)
BeanField
setRequired
in interface BeanField<T,I>
required
- Whether or not the field is requiredpublic void setErrorLocale(Locale errorLocale)
BeanField
setErrorLocale
in interface BeanField<T,I>
errorLocale
- Locale for error messages. If null, the default locale
is used.public Locale getErrorLocale()
BeanField
getErrorLocale
in interface BeanField<T,I>
public final void setFieldValue(Object bean, String value, String header) throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException, CsvConstraintViolationException, CsvValidationException
BeanField
setFieldValue
in interface BeanField<T,I>
bean
- Object containing the field to be set.value
- String containing the value to set the field to.header
- The header from the CSV file under which this value was found.CsvDataTypeMismatchException
- When the result of data conversion returns
an object that cannot be assigned to the selected fieldCsvRequiredFieldEmptyException
- When a field is mandatory, but there is no
input datum in the CSV fileCsvConstraintViolationException
- When the internal structure of
data would be violated by the data in the CSV fileCsvValidationException
- If a user-supplied validator determines
that the input is invalidpublic Object getFieldValue(Object bean)
BeanField
getFieldValue
in interface BeanField<T,I>
bean
- Object containing the field to be readpublic Object[] indexAndSplitMultivaluedField(Object value, I index) throws CsvDataTypeMismatchException
BeanField
indexAndSplitMultivaluedField
in interface BeanField<T,I>
value
- The value of the bean field that should be writtenindex
- An index into value
that determines which of the
many possible values are currently being written. For header-based
mapping strategies, this will be the header name, and for column
position mapping strategies, it will be the zero-based column position.value
wrapped in an array, since we assume most values
will not be multi-valuedCsvDataTypeMismatchException
- If value
is not of the type
expected by the implementing classprotected boolean isFieldEmptyForWrite(Object value)
BeanField
considers the
value passed in as empty for the purposes of determining whether or not
a required field is empty.
This allows any overriding class to define "empty" while writing
values to a CSV file in a way that is meaningful for its own data. A
simple example is a Collection
that is not null, but
empty.
The default implementation simply checks for null
.
value
- The value of a field out of a bean that is being written to
a CSV file. Can be null
.value
to be
empty for the purposes of its conversionprotected void assignValueToField(Object bean, Object obj, String header) throws CsvDataTypeMismatchException
Uses the setter method if available.
Derived classes can override this method if they have special needs for setting the value of a field, such as adding to an existing collection.
bean
- The bean in which the field is locatedobj
- The data to be assigned to this field of the destination beanheader
- The header from the CSV file under which this value was found.CsvDataTypeMismatchException
- If the data to be assigned cannot
be converted to the type of the destination fieldprotected abstract Object convert(String value) throws CsvDataTypeMismatchException, CsvConstraintViolationException
value
- The string from the selected field of the CSV file. If the
field is marked as required in the annotation, this value is guaranteed
not to be null, empty or blank according to
StringUtils.isBlank(java.lang.CharSequence)
Object
representing the input data converted
into the proper typeCsvDataTypeMismatchException
- If the input string cannot be converted into
the proper typeCsvConstraintViolationException
- When the internal structure of
data would be violated by the data in the CSV filepublic final String[] write(Object bean, I index) throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException
convertToWrite(java.lang.Object)
for the actual conversion, and itself performs validation and handles
exceptions thrown by convertToWrite(java.lang.Object)
. The
validation consists of verifying that both bean
and field
are not null before calling convertToWrite(java.lang.Object)
.write
in interface BeanField<T,I>
bean
- The bean holding the field to be writtenindex
- The header name or column number of the field currently
being processed. This can be used to find a certain position in a
multivalued field when not all of the values should be written.BeanFieldJoin
may return multiple values.
If either the bean or the field are null
, this method returns
an empty array to allow the writer to treat null
specially. It
is also possible that individual values in the array are null
.
The writer may wish to write "(null)" or "\0" or "NULL" or some other
key instead of a blank string.CsvDataTypeMismatchException
- If expected to convert an
unsupported data typeCsvRequiredFieldEmptyException
- If the field is marked as required,
but is currently emptyprotected String convertToWrite(Object value) throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException
write(java.lang.Object, java.lang.Object)
and should
be overridden in derived classes.
The default implementation simply calls toString()
on the
object in question. Derived classes will, in most cases, want to override
this method. Alternatively, for complex types, overriding the
toString()
method in the type of the field in question would also
work fine.
value
- The contents of the field currently being processed from the
bean to be written. Can be null if the field is not marked as required.value
is nullCsvDataTypeMismatchException
- This implementation does not throw
this exceptionCsvRequiredFieldEmptyException
- If the input is empty but the
field is required. The case of the field being null is checked before
this method is called, but other implementations may have other cases
that are semantically equivalent to being empty, such as an empty
collection. The preferred way to perform this check is in
isFieldEmptyForWrite(java.lang.Object)
. This exception may
be removed from this method signature sometime in the future.write(java.lang.Object, java.lang.Object)
Copyright © 2005–2023. All rights reserved.