com.univocity.parsers.common.processor
Class BeanWriterProcessor<T>

java.lang.Object
  extended by com.univocity.parsers.common.processor.BeanWriterProcessor<T>
Type Parameters:
T - the annotated class type.
All Implemented Interfaces:
RowWriterProcessor<T>

public class BeanWriterProcessor<T>
extends Object
implements RowWriterProcessor<T>

A RowWriterProcessor implementation for converting annotated java objects into object arrays suitable for writing in any implementation of AbstractWriter.

The class type of the object must contain the annotations provided in com.univocity.parsers.annotations.

For any given java bean instance, this processor will read and convert annotated fields into an object array.

Author:
uniVocity Software Pty Ltd - [email protected]
See Also:
AbstractWriter, RowWriterProcessor

Constructor Summary
BeanWriterProcessor(Class<T> beanType)
          Initializes the BeanWriterProcessor with the annotated bean class
 
Method Summary
 Object[] applyConversions(String[] row, ParsingContext context)
          Executes the sequences of conversions defined using ConversionProcessor.convertFields(Conversion...), ConversionProcessor.convertIndexes(Conversion...) and ConversionProcessor.convertAll(Conversion...), for every field in the given row.
 void convertAll(Conversion... conversions)
          Applies a set of Conversion objects over all elements of a record
 FieldSet<String> convertFields(Conversion... conversions)
          Applies a set of Conversion objects over fields of a record by name.
 FieldSet<Integer> convertIndexes(Conversion... conversions)
          Applies a set of Conversion objects over indexes of a record.
 T createBean(String[] row, ParsingContext context)
          Converts a record with values extracted from the parser into a java bean instance.
protected  void initialize()
          Identifies and extracts fields annotated with the Parsed annotation
 void reverseConversions(boolean executeInReverseOrder, Object[] row, String[] headers, int[] indexesToWrite)
          Executes the sequences of reverse conversions defined using ConversionProcessor.convertFields(Conversion...), ConversionProcessor.convertIndexes(Conversion...) and ConversionProcessor.convertAll(Conversion...), for every field in the given row.
 Object[] reverseConversions(T bean, String[] headers, int[] indexesToWrite)
          Converts a java bean instance into a sequence of values for writing.
 Object[] write(T input, String[] headers, int[] indexesToWrite)
          Converts the java bean instance into a sequence of values for writing.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BeanWriterProcessor

public BeanWriterProcessor(Class<T> beanType)
Initializes the BeanWriterProcessor with the annotated bean class

Parameters:
beanType - the class annotated with one or more of the annotations provided in com.univocity.parsers.annotations.
Method Detail

write

public Object[] write(T input,
                      String[] headers,
                      int[] indexesToWrite)
Converts the java bean instance into a sequence of values for writing.

Specified by:
write in interface RowWriterProcessor<T>
Parameters:
input - an instance of the type defined in this class constructor.
headers - All field names used to produce records in a given destination. May be null if no headers have been defined in CommonSettings.getHeaders()
indexesToWrite - 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:
a row of objects containing the values extracted from the java bean
See Also:
CsvWriter, FixedWidthWriter, CommonSettings, AbstractWriter

initialize

protected final void initialize()
Identifies and extracts fields annotated with the Parsed annotation


createBean

public final T createBean(String[] row,
                          ParsingContext context)
Converts a record with values extracted from the parser into a java bean instance.

Parameters:
row - The values extracted from the parser
context - The current state of the parsing process
Returns:
an instance of the java bean type defined in this class constructor.

reverseConversions

public final Object[] reverseConversions(T bean,
                                         String[] headers,
                                         int[] indexesToWrite)
Converts a java bean instance into a sequence of values for writing.

Parameters:
bean - an instance of the type defined in this class constructor.
headers - All field names used to produce records in a given destination. May be null if no headers have been defined in CommonSettings.getHeaders()
indexesToWrite - 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:
a row of objects containing the values extracted from the java bean

convertIndexes

public final FieldSet<Integer> convertIndexes(Conversion... conversions)
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 - The sequence of conversions to be executed in a set of field indexes.
Returns:
A FieldSet for indexes.

convertAll

public final void convertAll(Conversion... conversions)
Applies a set of Conversion objects over all elements of a record

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

convertFields

public final FieldSet<String> convertFields(Conversion... conversions)
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 - The sequence of conversions to be executed in a set of field indexes.
Returns:
A FieldSet for field names.

applyConversions

public final Object[] applyConversions(String[] row,
                                       ParsingContext context)
Executes the sequences of conversions defined using ConversionProcessor.convertFields(Conversion...), ConversionProcessor.convertIndexes(Conversion...) and ConversionProcessor.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 - the parsed record with its individual records as extracted from the original input.
context - the current state of the parsing process.
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.


reverseConversions

public final void reverseConversions(boolean executeInReverseOrder,
                                     Object[] row,
                                     String[] headers,
                                     int[] indexesToWrite)
Executes the sequences of reverse conversions defined using ConversionProcessor.convertFields(Conversion...), ConversionProcessor.convertIndexes(Conversion...) and ConversionProcessor.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 - flag to indicate whether the conversion sequence should be executed in the reverse order of its declaration (used in BeanConversionProcessor.reverseConversions(T, java.lang.String[], int[])) .
row - the row of objects that will be converted
headers - All field names used to produce records in a given destination. May be null if no headers have been defined in CommonSettings.getHeaders()
indexesToWrite - 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...)


Copyright © 2014 uniVocity Software Pty Ltd. All rights reserved.