Package com.opencsv.bean
Class BeanFieldJoin<T,I>
- java.lang.Object
-
- com.opencsv.bean.AbstractBeanField<T,I>
-
- com.opencsv.bean.BeanFieldSingleValue<T,I>
-
- com.opencsv.bean.BeanFieldJoin<T,I>
-
- Type Parameters:
T
- The type of the bean being populatedI
- The index of theMultiValuedMap
in use
- All Implemented Interfaces:
BeanField<T,I>
- Direct Known Subclasses:
BeanFieldJoinIntegerIndex
,BeanFieldJoinStringIndex
public abstract class BeanFieldJoin<T,I> extends BeanFieldSingleValue<T,I>
This class is used for combining multiple columns of the input, possibly with multiple identically named columns, into one field.- Since:
- 4.2
- Author:
- Andrew Rucker Jones
-
-
Field Summary
-
Fields inherited from class com.opencsv.bean.BeanFieldSingleValue
capture, writeFormat
-
Fields inherited from class com.opencsv.bean.AbstractBeanField
converter, errorLocale, field, fieldAccess, required, type
-
-
Constructor Summary
Constructors Constructor Description BeanFieldJoin(java.lang.Class<?> type, java.lang.reflect.Field field, boolean required, java.util.Locale errorLocale, CsvConverter converter, java.lang.Class<? extends org.apache.commons.collections4.MultiValuedMap> mapType, java.lang.String capture, java.lang.String format)
Creates a new instance.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
assignValueToField(java.lang.Object bean, java.lang.Object obj, java.lang.String header)
Assigns the value given to the proper field of the bean given.java.lang.Object[]
indexAndSplitMultivaluedField(java.lang.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(java.lang.Object value)
Checks thatvalue
is not null and not empty.protected abstract java.lang.Object
putNewValue(org.apache.commons.collections4.MultiValuedMap<I,java.lang.Object> map, java.lang.String index, java.lang.Object newValue)
Puts the value given innewValue
intomap
usingindex
.-
Methods inherited from class com.opencsv.bean.BeanFieldSingleValue
convert, convertToWrite
-
Methods inherited from class com.opencsv.bean.AbstractBeanField
getErrorLocale, getField, getFieldValue, getType, isRequired, setErrorLocale, setField, setFieldValue, setRequired, setType, write
-
-
-
-
Constructor Detail
-
BeanFieldJoin
public BeanFieldJoin(java.lang.Class<?> type, java.lang.reflect.Field field, boolean required, java.util.Locale errorLocale, CsvConverter converter, java.lang.Class<? extends org.apache.commons.collections4.MultiValuedMap> mapType, java.lang.String capture, java.lang.String format)
Creates a new instance.- Parameters:
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
- The bean field this object representsrequired
- Whether or not a value is always required for this fielderrorLocale
- The locale to use for error messagesconverter
- The converter to be used for performing the data conversion on reading or writingmapType
- The type of theMultiValuedMap
that should be instantiated for the bean field being populatedcapture
- SeeCsvBindAndJoinByName.capture()
format
- The format string used for packaging values to be written. Ifnull
or empty, it is ignored.
-
-
Method Detail
-
putNewValue
protected abstract java.lang.Object putNewValue(org.apache.commons.collections4.MultiValuedMap<I,java.lang.Object> map, java.lang.String index, java.lang.Object newValue)
Puts the value given innewValue
intomap
usingindex
. This allows derived classes to do something special before assigning the value, such as converting the index to a different type.- Parameters:
map
- The map to which to assign the new value. Never null.index
- The index under which the new value should be placed in the map. Never null.newValue
- The new value to be put in the map- Returns:
- The previous value under this index, or null if there was no previous value
-
assignValueToField
protected void assignValueToField(java.lang.Object bean, java.lang.Object obj, java.lang.String header) throws CsvDataTypeMismatchException
Assigns the value given to the proper field of the bean given. In the case of this kind of bean field, the new value will be added to an existing map, and a new map will be created if one does not already exist.- Overrides:
assignValueToField
in classAbstractBeanField<T,I>
- Parameters:
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.- Throws:
CsvDataTypeMismatchException
- If the data to be assigned cannot be converted to the type of the destination field
-
indexAndSplitMultivaluedField
public java.lang.Object[] indexAndSplitMultivaluedField(java.lang.Object value, I index) throws CsvDataTypeMismatchException
Description copied from interface:BeanField
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.- Specified by:
indexAndSplitMultivaluedField
in interfaceBeanField<T,I>
- Overrides:
indexAndSplitMultivaluedField
in classAbstractBeanField<T,I>
- Parameters:
value
- The value of the bean field that should be writtenindex
- An index intovalue
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:
- An array of all objects in the
MultiValuedMap
addressed by this bean field answering to the key given inindex
- Throws:
CsvDataTypeMismatchException
- Ifvalue
is not of the type expected by the implementing class
-
isFieldEmptyForWrite
protected boolean isFieldEmptyForWrite(java.lang.Object value)
Checks thatvalue
is not null and not empty.- Overrides:
isFieldEmptyForWrite
in classAbstractBeanField<T,I>
- Parameters:
value
- The value of a field out of a bean that is being written to a CSV file. Can benull
.- Returns:
- Whether or not this implementation considers
value
to be empty for the purposes of its conversion
-
-