Top Description Fields Constructors Methods
com.opencsv.bean

public abstract Class AbstractBeanField<T, I>

extends Object
implements BeanField<T, I>
Class Inheritance
All Implemented Interfaces
com.opencsv.bean.BeanField
Known Direct Subclasses
com.opencsv.bean.BeanFieldSingleValue, com.opencsv.bean.BeanFieldSplit, com.opencsv.bean.customconverter.ConverterLanguageToBoolean
Type Parameters
<T>
Type of the bean being populated
<I>
Type of the index into a multivalued field
Imports
com.opencsv.ICSVParser, com.opencsv.bean.processor.PreAssignmentProcessor, .StringProcessor, com.opencsv.bean.validators.PreAssignmentValidator, .StringValidator, com.opencsv.exceptions.*, org.apache.commons.lang3.ObjectUtils, .StringUtils, java.lang.reflect.Field, .InvocationTargetException, java.util.Locale, .Objects, .ResourceBundle

This base bean takes over the responsibility of converting the supplied string to the proper type for the destination field and setting the destination field.

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.

Author
Andrew Rucker Jones
Since
3.8

Field Summary

Modifier and TypeField 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. This is not necessarily the declaring class in the case of inheritance, but rather the type that opencsv expects to instantiate.

Constructor Summary

AccessConstructor and Description
public
AbstractBeanField()

Default nullary constructor, so derived classes aren't forced to create a constructor identical to this one.

public
AbstractBeanField(Class<?>
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.
type
,
Field field, boolean
Whether or not this field is required in input
required
,
Locale
The errorLocale to use for error messages.
errorLocale
,
CsvConverter
The converter to be used to perform the actual data conversion
converter
)

Method Summary

Modifier and TypeMethod and Description
protected void
assignValueToField(Object
The bean in which the field is located
bean
,
Object
The data to be assigned to this field of the destination bean
obj
,
String
The header from the CSV file under which this value was found.
header
)

Assigns the given object to this field of the destination bean.

protected abstract Object

Returns:

An java.lang.Object representing the input data converted into the proper type
convert
(String
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 org.apache.commons.lang3.StringUtils#isBlank(java.lang.CharSequence)
value
)

Method for converting from a string to the proper datatype of the destination field.

protected String

Returns:

A string representation of the value of the field in question in the bean passed in, or an empty string if value is null
convertToWrite
(Object
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
)

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.

public Locale
getErrorLocale()

Implements com.opencsv.bean.BeanField.getErrorLocale.

Returns the error locale for the beans.

public Field
getField()

Implements com.opencsv.bean.BeanField.getField.

Gets the field to be processed.

public Object
getFieldValue(Object bean)

Implements com.opencsv.bean.BeanField.getFieldValue.

Gets the contents of the selected field of the given bean.

public Class<?>
getType()

Implements com.opencsv.bean.BeanField.getType.

Gets the type of the bean this field is attached to.

public Object[]

Returns:

value wrapped in an array, since we assume most values will not be multi-valued
indexAndSplitMultivaluedField
(Object value, I index)

Implements com.opencsv.bean.BeanField.indexAndSplitMultivaluedField.

Given the value of a bean field and an index into that value, determine what values need to be written.

protected boolean

Returns:

Whether or not this implementation considers value to be empty for the purposes of its conversion
isFieldEmptyForWrite
(Object
The value of a field out of a bean that is being written to a CSV file. Can be null.
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.

public boolean
isRequired()

Implements com.opencsv.bean.BeanField.isRequired.

Answers the query, whether this field is required or not.

private String
public void
setErrorLocale(Locale errorLocale)

Implements com.opencsv.bean.BeanField.setErrorLocale.

Sets the locale for all error messages.

public void
setField(Field field)

Implements com.opencsv.bean.BeanField.setField.

Sets the field to be processed.

public final void
setFieldValue(Object bean, String value, String header)

Implements com.opencsv.bean.BeanField.setFieldValue.

Populates the selected field of the bean.

public void
setRequired(boolean required)

Implements com.opencsv.bean.BeanField.setRequired.

Determines whether or not a field is required.

public void
setType(Class<?> type)

Implements com.opencsv.bean.BeanField.setType.

Sets the type of the bean this field is attached to.

private void
public final String[]
write(Object bean, I index)

Implements com.opencsv.bean.BeanField.write.

This method takes the current value of the field in question in the bean passed in and converts it to a string.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

converterback to summary
protected CsvConverter converter

A class that converts from a string to the destination type on reading and vice versa on writing. This is only used for opencsv-internal conversions, not by custom converters.

errorLocaleback to summary
protected Locale errorLocale

Locale for error messages.

fieldback to summary
protected Field field

The field this class represents.

fieldAccessback to summary
protected FieldAccess<Object> fieldAccess

An encapsulated way of accessing the member variable associated with this field.

requiredback to summary
protected boolean required

Whether or not this field is required.

typeback to summary
protected Class<?> type

The type the field is located in. This is not necessarily the declaring class in the case of inheritance, but rather the type that opencsv expects to instantiate.

Constructor Detail

AbstractBeanFieldback to summary
public AbstractBeanField()

Default nullary constructor, so derived classes aren't forced to create a constructor identical to this one.

AbstractBeanFieldback to summary
public AbstractBeanField(Class<?> type, Field field, boolean required, Locale errorLocale, CsvConverter converter)
Parameters
type:Class<?>

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:Field

A java.lang.reflect.Field object.

required:boolean

Whether or not this field is required in input

errorLocale:Locale

The errorLocale to use for error messages.

converter:CsvConverter

The converter to be used to perform the actual data conversion

Since
4.2

Method Detail

assignValueToFieldback to summary
protected void assignValueToField(Object bean, Object obj, String header) throws CsvDataTypeMismatchException

Assigns the given object to this field of the destination bean.

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.

Parameters
bean:Object

The bean in which the field is located

obj:Object

The data to be assigned to this field of the destination bean

header:String

The header from the CSV file under which this value was found.

Exceptions
CsvDataTypeMismatchException:
If the data to be assigned cannot be converted to the type of the destination field
convertback to summary
protected abstract Object convert(String value) throws CsvDataTypeMismatchException, CsvConstraintViolationException

Method for converting from a string to the proper datatype of the destination field. This method must be specified in all non-abstract derived classes.

Parameters
value:String

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 org.apache.commons.lang3.StringUtils#isBlank(java.lang.CharSequence)

Returns:Object

An java.lang.Object representing the input data converted into the proper type

Exceptions
CsvDataTypeMismatchException:
If the input string cannot be converted into the proper type
CsvConstraintViolationException:
When the internal structure of data would be violated by the data in the CSV file
convertToWriteback to summary
protected String convertToWrite(Object value) throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException

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.

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.

Parameters
value:Object

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.

Returns:String

A string representation of the value of the field in question in the bean passed in, or an empty string if value is null

Exceptions
CsvDataTypeMismatchException:
This implementation does not throw this exception
CsvRequiredFieldEmptyException:
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.
Since
3.9
See Also
write(java.lang.Object, java.lang.Object)
getErrorLocaleback to summary
public Locale getErrorLocale()

Implements com.opencsv.bean.BeanField.getErrorLocale.

Doc from com.opencsv.bean.BeanField.getErrorLocale.

Returns the error locale for the beans. Used by the opencsv provided validators to populate the error messages they generate.

Annotations
@Override
getFieldback to summary
public Field getField()

Implements com.opencsv.bean.BeanField.getField.

Doc from com.opencsv.bean.BeanField.getField.

Gets the field to be processed.

Annotations
@Override
getFieldValueback to summary
public Object getFieldValue(Object bean)

Implements com.opencsv.bean.BeanField.getFieldValue.

Doc from com.opencsv.bean.BeanField.getFieldValue.

Gets the contents of the selected field of the given bean. This method performs no conversions of any kind, but simply gets the value of the desired field using an accessor method if one is available and reflection if one is not.

Annotations
@Override
getTypeback to summary
public Class<?> getType()

Implements com.opencsv.bean.BeanField.getType.

Doc from com.opencsv.bean.BeanField.getType.

Gets the type of the bean this field is attached to. This is necessary because the declaring class as given by the field itself may be a superclass of the class that is instantiated during bean population.

Annotations
@Override
indexAndSplitMultivaluedFieldback to summary
public Object[] indexAndSplitMultivaluedField(Object value, I index) throws CsvDataTypeMismatchException

Implements com.opencsv.bean.BeanField.indexAndSplitMultivaluedField.

Doc from com.opencsv.bean.BeanField.indexAndSplitMultivaluedField.

Given the value of a bean field and an index into that value, determine what values need to be written. When writing a bean to a CSV file, some single fields from the bean could have values that need to be split into multiple fields when writing them to the CSV file. Given the value of the bean field and an index into the data, this method returns the objects to be converted and written.

Returns:Object[]

value wrapped in an array, since we assume most values will not be multi-valued

Annotations
@Override
Exceptions
CsvDataTypeMismatchException:
If value is not of the type expected by the implementing class
Since
4.2
isFieldEmptyForWriteback to summary
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.

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 java.util.Collection that is not null, but empty.

The default implementation simply checks for null.

Parameters
value:Object

The value of a field out of a bean that is being written to a CSV file. Can be null.

Returns:boolean

Whether or not this implementation considers value to be empty for the purposes of its conversion

Since
4.2
isRequiredback to summary
public boolean isRequired()

Implements com.opencsv.bean.BeanField.isRequired.

Doc from com.opencsv.bean.BeanField.isRequired.

Answers the query, whether this field is required or not.

Annotations
@Override
preProcessValueback to summary
private String preProcessValue(PreAssignmentProcessor processor, String value) throws CsvValidationException
setErrorLocaleback to summary
public void setErrorLocale(Locale errorLocale)

Implements com.opencsv.bean.BeanField.setErrorLocale.

Doc from com.opencsv.bean.BeanField.setErrorLocale.

Sets the locale for all error messages.

Annotations
@Override
setFieldback to summary
public void setField(Field field)

Implements com.opencsv.bean.BeanField.setField.

Doc from com.opencsv.bean.BeanField.setField.

Sets the field to be processed.

Annotations
@Override
setFieldValueback to summary
public final void setFieldValue(Object bean, String value, String header) throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException, CsvConstraintViolationException, CsvValidationException

Implements com.opencsv.bean.BeanField.setFieldValue.

Doc from com.opencsv.bean.BeanField.setFieldValue.

Populates the selected field of the bean. This method performs conversion on the input string and assigns the result to the proper field in the provided bean.

Annotations
@Override
Exceptions
CsvDataTypeMismatchException:
When the result of data conversion returns an object that cannot be assigned to the selected field
CsvRequiredFieldEmptyException:
When a field is mandatory, but there is no input datum in the CSV file
CsvConstraintViolationException:
When the internal structure of data would be violated by the data in the CSV file
CsvValidationException:
If a user-supplied validator determines that the input is invalid
setRequiredback to summary
public void setRequired(boolean required)

Implements com.opencsv.bean.BeanField.setRequired.

Doc from com.opencsv.bean.BeanField.setRequired.

Determines whether or not a field is required.

Implementation Note

This method is necessary for custom converters. If we did not have it, every custom converter would be required to implement a constructor with this one boolean parameter, and the instantiation code for the custom converter would look much uglier.

Annotations
@Override
setTypeback to summary
public void setType(Class<?> type)

Implements com.opencsv.bean.BeanField.setType.

Doc from com.opencsv.bean.BeanField.setType.

Sets the type of the bean this field is attached to.

Annotations
@Override
validateValueback to summary
private void validateValue(PreAssignmentValidator validator, String value) throws CsvValidationException
writeback to summary
public final String[] write(Object bean, I index) throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException

Implements com.opencsv.bean.BeanField.write.

This method takes the current value of the field in question in the bean passed in and converts it to a string. It is actually a stub that calls 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).

Annotations
@Override
Exceptions
CsvDataTypeMismatchException:

Doc from com.opencsv.bean.BeanField.write.

If expected to convert an unsupported data type

CsvRequiredFieldEmptyException:

Doc from com.opencsv.bean.BeanField.write.

If the field is marked as required, but is currently empty