Top Description Methods
com.opencsv.bean

public Interface BeanField<T, I>

Known Direct Implementers
com.opencsv.bean.AbstractBeanField
Type Parameters
<T>
Type of the bean being populated
<I>
Type of the index into a multivalued field
Imports
com.opencsv.exceptions.CsvConstraintViolationException, .CsvDataTypeMismatchException, .CsvRequiredFieldEmptyException, .CsvValidationException, java.lang.reflect.Field, java.util.Locale

Used to extend the java.lang.reflect.Field class to include functionality that opencsv requires.

This includes a required flag and a write(java.lang.Object, java.lang.Object) method for writing beans back out to a CSV file. The required flag determines if the field has to be non-empty.

Synchronization

All implementations of this interface must be thread-safe.

Method Summary

Modifier and TypeMethod and Description
public Locale

Returns:

Locale for error messages.
getErrorLocale
()

Returns the error locale for the beans.

public Field

Returns:

A field object
getField
()

Gets the field to be processed.

public Object

Returns:

The value of the field in the given bean
getFieldValue
(Object
Object containing the field to be read
bean
)

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

public Class<?>

Returns:

The type of the bean this field is attached to
getType
()

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

public Object[]

Returns:

An array of Objects that should be converted for the output and written
indexAndSplitMultivaluedField
(Object
The value of the bean field that should be written
value
,
I
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.
index
)

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

public boolean

Returns:

True if the field is required to be set (cannot be null or an empty string), false otherwise
isRequired
()

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

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

Sets the locale for all error messages.

public void
setField(Field
Which field is being populated
field
)

Sets the field to be processed.

public void
setFieldValue(Object
Object containing the field to be set.
bean
,
String
String containing the value to set the field to.
value
,
String
The header from the CSV file under which this value was found.
header
)

Populates the selected field of the bean.

public void
setRequired(boolean
Whether or not the field is required
required
)

Determines whether or not a field is required.

public void
setType(Class<?>
The type that is instantiated when this field is used
type
)

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

public String[]

Returns:

An array of string representations for the values of this field out of the bean passed in. Typically, there will be only one value, but com.opencsv.bean.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.
write
(Object
The bean holding the field to be written
bean
,
I
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.
index
)

This method takes the current value of the field in question in the bean passed in and converts it to one or more strings.

Method Detail

getErrorLocaleback to summary
public Locale getErrorLocale()

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

Returns:Locale

Locale for error messages.

Since
5.0
getFieldback to summary
public Field getField()

Gets the field to be processed.

Returns:Field

A field object

See Also
java.lang.reflect.Field
getFieldValueback to summary
public Object getFieldValue(Object bean)

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.

Parameters
bean:Object

Object containing the field to be read

Returns:Object

The value of the field in the given bean

Since
4.2
getTypeback to summary
public Class<?> 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.

Returns:Class<?>

The type of the bean this field is attached to

Since
5.0
indexAndSplitMultivaluedFieldback to summary
public Object[] indexAndSplitMultivaluedField(Object value, I index) throws CsvDataTypeMismatchException

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.

Parameters
value:Object

The value of the bean field that should be written

index:I

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.

Returns:Object[]

An array of Objects that should be converted for the output and written

Exceptions
CsvDataTypeMismatchException:
If value is not of the type expected by the implementing class
Since
4.2
isRequiredback to summary
public boolean isRequired()

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

Returns:boolean

True if the field is required to be set (cannot be null or an empty string), false otherwise

Since
3.10
setErrorLocaleback to summary
public void setErrorLocale(Locale errorLocale)

Sets the locale for all error messages.

Parameters
errorLocale:Locale

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

Since
4.0
setFieldback to summary
public void setField(Field field)

Sets the field to be processed.

Parameters
field:Field

Which field is being populated

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

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.

Parameters
bean:Object

Object containing the field to be set.

value:String

String containing the value to set the field to.

header:String

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

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)

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.

Parameters
required:boolean

Whether or not the field is required

Since
3.10
setTypeback to summary
public void setType(Class<?> type)

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

Parameters
type:Class<?>

The type that is instantiated when this field is used

Since
5.0
writeback to summary
public String[] write(Object bean, I index) throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException

This method takes the current value of the field in question in the bean passed in and converts it to one or more strings. This method is used to write beans back out to a CSV file, and should ideally provide an accurate representation of the field such that it is round trip equivalent. That is to say, this method should write data out just as it would expect to read the data in.

Parameters
bean:Object

The bean holding the field to be written

index:I

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.

Returns:String[]

An array of string representations for the values of this field out of the bean passed in. Typically, there will be only one value, but com.opencsv.bean.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.

Exceptions
CsvDataTypeMismatchException:
If expected to convert an unsupported data type
CsvRequiredFieldEmptyException:
If the field is marked as required, but is currently empty
Since
3.9