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; }
Car which has four Wheels:
public static class Car { @Nested Wheel frontLeft; @Nested Wheel frontRight; @Nested Wheel rearLeft; @Nested Wheel rearRight; }
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); } }
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; }
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 instanceImportant Note
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.
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public final int | |
| public int | Returns: the transformed position to be used to read/write values from/to the given field.the field of a field, int Nested class whose header will be transformedthe current column position associated with the field of the index)Nested classTransforms a header index |
| public int | Returns: the transformed position to be used to read/write values from/to the given method.the method of a method, int Nested class whose header will be transformedthe current column position associated with the method of the index)Nested classTransforms a header index |
| public final String | |
| public String | Returns: the transformed header name to be used to read/write values from/to the given field.the field of a field, String Nested class whose header will be transformedthe current header name associated with the field of the name)Nested classTransforms a header name |
| public String | Returns: the transformed header name to be used to read/write values from/to the given method.the method of a method, String Nested class whose header will be transformedthe current header name associated with the method of the name)Nested classTransforms a header name |
| HeaderTransformer | back to summary |
|---|---|
| public HeaderTransformer() | |
| transformIndex | back to summary |
|---|---|
| public final int transformIndex(AnnotatedElement element, int index) | |
| transformIndex | back to summary |
|---|---|
| public int transformIndex(Field field, int index) Transforms a header index | |
| transformIndex | back to summary |
|---|---|
| public int transformIndex(Method method, int index) Transforms a header index | |
| transformName | back to summary |
|---|---|
| public final String transformName(AnnotatedElement element, String name) | |
| transformName | back to summary |
|---|---|
| public String transformName(Field field, String name) Transforms a header name | |
| transformName | back to summary |
|---|---|
| public String transformName(Method method, String name) Transforms a header name | |