Top Description Fields Constructors Methods
com.univocity.parsers.common

public abstract Class DefaultConversionProcessor

extends Object
implements ConversionProcessor
Class Inheritance
All Implemented Interfaces
com.univocity.parsers.common.ConversionProcessor
Known Direct Subclasses
com.univocity.parsers.common.processor.ObjectRowWriterProcessor, com.univocity.parsers.common.processor.core.AbstractObjectProcessor, com.univocity.parsers.common.processor.core.BeanConversionProcessor
Imports
com.univocity.parsers.common.fields.*, com.univocity.parsers.common.processor.*, com.univocity.parsers.conversions.*, java.util.*

The base class for RowProcessor and RowWriterProcessor implementations that support value conversions provided by Conversion instances.
Author
Univocity Software Pty Ltd - [email protected]

Field Summary

Modifier and TypeField and Description
pack-priv Context
protected FieldConversionMapping
private Map<Class<?>, Conversion<I, O>[]>
private boolean
pack-priv ProcessorErrorHandler<T extends Context>
private int[]
private boolean
private int[]

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public final Object[]

Returns:

an row of Object instances containing the values obtained after the execution of all conversions.

Fields that do not have any conversion defined will just be copied to the object array into their original positions.

applyConversions
(String[]
the parsed record with its individual records as extracted from the original input.
row
,
Context
the current state of the parsing process.
context
)

Executes the sequences of conversions defined using DefaultConversionProcessor#convertFields(Conversion...), DefaultConversionProcessor#convertIndexes(Conversion...) and DefaultConversionProcessor#convertAll(Conversion...), for every field in the given row.

private boolean
applyConversionsByType(boolean reverse, Object[] row, boolean[] convertedFlags)

private Object
applyTypeConversion(boolean revert, Object input)

public final void
convertAll(Conversion<I, O>...
The sequence of conversions to be executed in all elements of a record
conversions
)

Implements com.univocity.parsers.common.ConversionProcessor.convertAll.

Applies a set of Conversion objects over all elements of a record
public final FieldSet<String>
convertFields(Conversion<I, O>...
The sequence of conversions to be executed in a set of field indexes.
conversions
)

Implements com.univocity.parsers.common.ConversionProcessor.convertFields.

Applies a set of Conversion objects over fields of a record by name.
public final FieldSet<Integer>
convertIndexes(Conversion<I, O>...
The sequence of conversions to be executed in a set of field indexes.
conversions
)

Implements com.univocity.parsers.common.ConversionProcessor.convertIndexes.

Applies a set of Conversion objects over indexes of a record.
public final void
convertType(Class<?>
the type over which a sequence of conversions should be applied
type
,
Conversion<I, O>...
the sequence of conversions to apply over values of the given type.
conversions
)

Implements com.univocity.parsers.common.ConversionProcessor.convertType.

Applies a sequence of conversions over values of a given type.
private FieldConversionMapping
protected final boolean

Returns:

true if the error has been handled by the user and the record can still be processed, otherwise false if the record should be discarded.
handleConversionError
(Throwable
the exception that occurred when applying a conversion
ex
,
Object[]
the record being processed at the time the exception happened.
row
,
int
the column if the given row, whose value could not be converted. If negative, it's not possible to keep the record.
column
)

Handles an error that occurred when applying conversions over a value.

protected void
private void
public final boolean

Returns:

true if the the row should be discarded
reverseConversions
(boolean
flag to indicate whether the conversion sequence should be executed in the reverse order of its declaration.
executeInReverseOrder
,
Object[]
the row of objects that will be converted
row
,
NormalizedString[]
All field names used to produce records in a given destination. May be null if no headers have been defined in CommonSettings#getHeaders()
headers
,
int[]
The indexes of the headers that are actually being written. May be null if no fields have been selected using CommonSettings#selectFields(String...) or CommonSettings#selectIndexes(Integer...)
indexesToWrite
)

Executes the sequences of reverse conversions defined using DefaultConversionProcessor#convertFields(Conversion...), DefaultConversionProcessor#convertIndexes(Conversion...) and DefaultConversionProcessor#convertAll(Conversion...), for every field in the given row.

protected DataProcessingException
private boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

contextback to summary
pack-priv Context context
conversionsback to summary
protected FieldConversionMapping conversions
conversionsByTypeback to summary
private Map<Class<?>, Conversion<I, O>[]> conversionsByType
conversionsInitializedback to summary
private boolean conversionsInitialized
errorHandlerback to summary
pack-priv ProcessorErrorHandler<T extends Context> errorHandler
fieldIndexesback to summary
private int[] fieldIndexes
fieldsReorderedback to summary
private boolean fieldsReordered
reverseFieldIndexesback to summary
private int[] reverseFieldIndexes

Constructor Detail

DefaultConversionProcessorback to summary
public DefaultConversionProcessor()

Method Detail

applyConversionsback to summary
public final Object[] applyConversions(String[] row, Context context)

Executes the sequences of conversions defined using DefaultConversionProcessor#convertFields(Conversion...), DefaultConversionProcessor#convertIndexes(Conversion...) and DefaultConversionProcessor#convertAll(Conversion...), for every field in the given row.

Each field will be transformed using the Conversion#execute(Object) method.

In general the conversions will process a String and convert it to some object value (such as booleans, dates, etc).

Parameters
row:String[]

the parsed record with its individual records as extracted from the original input.

context:Context

the current state of the parsing process.

Returns:Object[]

an row of Object instances containing the values obtained after the execution of all conversions.

Fields that do not have any conversion defined will just be copied to the object array into their original positions.

applyConversionsByTypeback to summary
private boolean applyConversionsByType(boolean reverse, Object[] row, boolean[] convertedFlags)
applyTypeConversionback to summary
private Object applyTypeConversion(boolean revert, Object input)
convertAllback to summary
public final void convertAll(Conversion<I, O>... conversions)

Implements com.univocity.parsers.common.ConversionProcessor.convertAll.

Doc from com.univocity.parsers.common.ConversionProcessor.convertAll.

Applies a set of Conversion objects over all elements of a record

Parameters
conversions:Conversion<I, O>[]

The sequence of conversions to be executed in all elements of a record

Annotations
@Override
@SuppressWarnings:rawtypes, unchecked
convertFieldsback to summary
public final FieldSet<String> convertFields(Conversion<I, O>... conversions)

Implements com.univocity.parsers.common.ConversionProcessor.convertFields.

Doc from com.univocity.parsers.common.ConversionProcessor.convertFields.

Applies a set of Conversion objects over fields of a record by name.

The idiom to define which fields should have these conversions applied is as follows:



processor.convertFields(Conversions.trim(), Conversions.toUpperCase()).add("name", "position"); // applies trim and uppercase conversions to fields with headers "name" and "position"

Parameters
conversions:Conversion<I, O>[]

The sequence of conversions to be executed in a set of field indexes.

Returns:FieldSet<String>

A FieldSet for field names.

Annotations
@Override
@SuppressWarnings:unchecked, rawtypes
convertIndexesback to summary
public final FieldSet<Integer> convertIndexes(Conversion<I, O>... conversions)

Implements com.univocity.parsers.common.ConversionProcessor.convertIndexes.

Doc from com.univocity.parsers.common.ConversionProcessor.convertIndexes.

Applies a set of Conversion objects over indexes of a record.

The idiom to define which indexes should have these conversions applies is as follows:



processor.convertIndexes(Conversions.trim(), Conversions.toUpperCase()).add(2, 5); // applies trim and uppercase conversions to fields in indexes 2 and 5

Parameters
conversions:Conversion<I, O>[]

The sequence of conversions to be executed in a set of field indexes.

Returns:FieldSet<Integer>

A FieldSet for indexes.

Annotations
@Override
@SuppressWarnings:unchecked, rawtypes
convertTypeback to summary
public final void convertType(Class<?> type, Conversion<I, O>... conversions)

Implements com.univocity.parsers.common.ConversionProcessor.convertType.

Doc from com.univocity.parsers.common.ConversionProcessor.convertType.

Applies a sequence of conversions over values of a given type. Used for writing.

Parameters
type:Class<?>

the type over which a sequence of conversions should be applied

conversions:Conversion<I, O>[]

the sequence of conversions to apply over values of the given type.

Annotations
@Override
getConversionsback to summary
private FieldConversionMapping getConversions()
handleConversionErrorback to summary
protected final boolean handleConversionError(Throwable ex, Object[] row, int column)

Handles an error that occurred when applying conversions over a value. If the user defined a ProcessorErrorHandler the user will receive the exception and is able to determine whether or not processing should continue, discarding the record. If the error handler is an instance of RetryableErrorHandler, the user can provide a default value to use in place of the one that could not be converted, and decide whether or not the record should be kept with the use of the RetryableErrorHandler#keepRecord() method.

Parameters
ex:Throwable

the exception that occurred when applying a conversion

row:Object[]

the record being processed at the time the exception happened.

column:int

the column if the given row, whose value could not be converted. If negative, it's not possible to keep the record.

Returns:boolean

true if the error has been handled by the user and the record can still be processed, otherwise false if the record should be discarded.

initializeConversionsback to summary
protected void initializeConversions(String[] row, Context context)
populateReverseFieldIndexesback to summary
private void populateReverseFieldIndexes()
reverseConversionsback to summary
public final boolean reverseConversions(boolean executeInReverseOrder, Object[] row, NormalizedString[] headers, int[] indexesToWrite)

Executes the sequences of reverse conversions defined using DefaultConversionProcessor#convertFields(Conversion...), DefaultConversionProcessor#convertIndexes(Conversion...) and DefaultConversionProcessor#convertAll(Conversion...), for every field in the given row.

Each field will be transformed using the Conversion#revert(Object) method.

In general the conversions will process an Object (such as a Boolean, Date, etc), and convert it to a String representation.

Parameters
executeInReverseOrder:boolean

flag to indicate whether the conversion sequence should be executed in the reverse order of its declaration.

row:Object[]

the row of objects that will be converted

headers:NormalizedString[]

All field names used to produce records in a given destination. May be null if no headers have been defined in CommonSettings#getHeaders()

indexesToWrite:int[]

The indexes of the headers that are actually being written. May be null if no fields have been selected using CommonSettings#selectFields(String...) or CommonSettings#selectIndexes(Integer...)

Returns:boolean

true if the the row should be discarded

toDataProcessingExceptionback to summary
protected DataProcessingException toDataProcessingException(Throwable ex, Object[] row, int column)
validateAllValuesback to summary
private boolean validateAllValues(Object[] objectRow)