com.univocity.parsers.annotations
Class HeaderTransformer

java.lang.Object
  extended by com.univocity.parsers.annotations.HeaderTransformer

public abstract class HeaderTransformer
extends Object

A transformer of headers used in Nested attributes. Used to reassign header names/indexes of attributes of a Nested class which is useful when multiple Nested attributes of the same type are used within a class. For example, consider the Wheel class defined as:



 public class Wheel {
     @Parsed
     String brand;

     @Parsed
     int miles;
 }
  

And a Car which has four Wheels:

 public static class Car {
                @Nested
                Wheel frontLeft;

                @Nested
                Wheel frontRight;

                @Nested
                Wheel rearLeft;

                @Nested
                Wheel rearRight;
 }
  

The HeaderTransformer allows us to "rename" the attributes of each different Wheel of the Car so that input columns can be assigned to the appropriate places. Assuming an input with headers frontLeftWheelBrand,frontLeftWheelMiles,frontRightWheelBrand,frontRightWheelMiles,rearLeftWheelBrand,rearLeftWheelMiles,rearRightWheelBrand,rearRightWheelMiles, a HeaderTransformer can be created like this to assign a prefix in front of the header names derived from Wheel (originally just "brand" and "miles"):

 public static class PrefixTransformer extends HeaderTransformer {

                private String prefix;

                public PrefixTransformer(String... args) {
                        prefix = args[0];
        }

                @Override
                public String transformName(Field field, String name) {
                        return prefix + Character.toUpperCase(name.charAt(0)) + name.substring(1);
        }
 }
 

This allows us to to define the Car class as:

 public static class Car {
                @Nested(headerTransformer = PrefixTransformer.class, args = "frontLeftWheel")
                Wheel frontLeft;

                @Nested(headerTransformer = PrefixTransformer.class, args = "frontRightWheel")
                Wheel frontRight;

                @Nested(headerTransformer = PrefixTransformer.class, args = "rearLeftWheel")
                Wheel rearLeft;

                @Nested(headerTransformer = PrefixTransformer.class, args = "rearRightWheel")
                Wheel rearRight;
 }
 

The above annotation will prefix the frontLeft fields ("brand" and "miles") with "frontLeftWheel", effectively forming the header "frontLeftWheelBrand" and "frontLeftWheelMiles", which will match the input headers and assign the correct values to the correct Wheel instance. IMPORTANT It is mandatory to define a constructor that takes String[] as a parameter. The actual parameter values come from Nested.args() to allow custom configuration of the concrete HeaderTransformer instance.

Author:
uniVocity Software Pty Ltd - dev@univocity.com

Constructor Summary
HeaderTransformer()
           
 
Method Summary
 int transformIndex(AnnotatedElement element, int index)
           
 int transformIndex(Field field, int index)
          Transforms a header index
 int transformIndex(Method method, int index)
          Transforms a header index
 String transformName(AnnotatedElement element, String name)
           
 String transformName(Field field, String name)
          Transforms a header name
 String transformName(Method method, String name)
          Transforms a header name
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HeaderTransformer

public HeaderTransformer()
Method Detail

transformName

public final String transformName(AnnotatedElement element,
                                  String name)

transformIndex

public final int transformIndex(AnnotatedElement element,
                                int index)

transformName

public String transformName(Field field,
                            String name)
Transforms a header name

Parameters:
field - the field of a Nested class whose header will be transformed
name - the current header name associated with the field of the Nested class
Returns:
the transformed header name to be used to read/write values from/to the given field.

transformIndex

public int transformIndex(Field field,
                          int index)
Transforms a header index

Parameters:
field - the field of a Nested class whose header will be transformed
index - the current column position associated with the field of the Nested class
Returns:
the transformed position to be used to read/write values from/to the given field.

transformName

public String transformName(Method method,
                            String name)
Transforms a header name

Parameters:
method - the method of a Nested class whose header will be transformed
name - the current header name associated with the method of the Nested class
Returns:
the transformed header name to be used to read/write values from/to the given method.

transformIndex

public int transformIndex(Method method,
                          int index)
Transforms a header index

Parameters:
method - the method of a Nested class whose header will be transformed
index - the current column position associated with the method of the Nested class
Returns:
the transformed position to be used to read/write values from/to the given method.


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