Package com.univocity.parsers.fixed
Class FixedWidthFields
- java.lang.Object
-
- com.univocity.parsers.fixed.FixedWidthFields
-
- All Implemented Interfaces:
java.lang.Cloneable
- Direct Known Subclasses:
FixedWidthFieldLengths
public class FixedWidthFields extends java.lang.Object implements java.lang.Cloneable
This class provides the name, length, alignment and padding of each field in a fixed-width record.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<FieldAlignment>
fieldAlignment
private java.util.List<java.lang.Integer>
fieldLengths
private java.util.List<NormalizedString>
fieldNames
private java.util.List<java.lang.Character>
fieldPadding
private java.util.List<java.lang.Boolean>
fieldsToIgnore
private boolean
noNames
private java.util.List<java.lang.Boolean>
paddingsToKeep
private int
totalLength
-
Constructor Summary
Constructors Modifier Constructor Description FixedWidthFields(int... fieldLengths)
Creates a new instance initialized with the lengths of all fields in a fixed-width record.FixedWidthFields(java.lang.Class beanClass)
Deprecated.useforParsing(Class)
andforWriting(Class)
to initialize the fields from the given class and filter out getters and setters that target the same field.private
FixedWidthFields(java.lang.Class beanClass, MethodFilter methodFilter)
Creates a new instance initialized fromFixedWidth
annotations in the fields of a given class.FixedWidthFields(java.lang.String[] headers, int[] lengths)
Defines a sequence of field names used to refer to columns in the input/output text of an entity, along with their lengths.FixedWidthFields(java.util.LinkedHashMap<java.lang.String,java.lang.Integer> fields)
Defines a sequence of field names used to refer to columns in the input/output text of an entity, along with their lengths.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description FixedWidthFields
addField(int length)
Adds the length of the next field in a fixed-width record.FixedWidthFields
addField(int length, char padding)
Adds the length of the next field in a fixed-width record.FixedWidthFields
addField(int startPosition, int endPosition)
Adds the range of the next field in a fixed-width record.FixedWidthFields
addField(int startPosition, int endPosition, char padding)
Adds the range of the next field in a fixed-width record.FixedWidthFields
addField(int startPosition, int endPosition, FieldAlignment alignment)
Adds the range of the next field in a fixed-width record.FixedWidthFields
addField(int startPosition, int endPosition, FieldAlignment alignment, char padding)
Adds the range of the next field in a fixed-width record.FixedWidthFields
addField(int length, FieldAlignment alignment)
Adds the length of the next field in a fixed-width record.FixedWidthFields
addField(int length, FieldAlignment alignment, char padding)
Adds the length of the next field in a fixed-width record.FixedWidthFields
addField(java.lang.String name, int length)
Adds the length of the next field in a fixed-width record.FixedWidthFields
addField(java.lang.String name, int length, char padding)
Adds the length of the next field in a fixed-width record.FixedWidthFields
addField(java.lang.String name, int startPosition, int endPosition)
Adds the range of the next field in a fixed-width record.FixedWidthFields
addField(java.lang.String name, int startPosition, int endPosition, char padding)
Adds the range of the next field in a fixed-width record.FixedWidthFields
addField(java.lang.String name, int startPosition, int endPosition, FieldAlignment alignment)
Adds the range of the next field in a fixed-width record.FixedWidthFields
addField(java.lang.String name, int startPosition, int endPosition, FieldAlignment alignment, char padding)
Adds the range of the next field in a fixed-width record.FixedWidthFields
addField(java.lang.String name, int length, FieldAlignment alignment)
Adds the length of the next field in a fixed-width record.FixedWidthFields
addField(java.lang.String name, int length, FieldAlignment alignment, char padding)
Adds the length of the next field in a fixed-width record.protected FixedWidthFields
clone()
static FixedWidthFields
forParsing(java.lang.Class beanClass)
Creates a new instance initialized fromFixedWidth
annotations in the fields and methods of a given class.static FixedWidthFields
forWriting(java.lang.Class beanClass)
Creates a new instance initialized fromFixedWidth
annotations in the fields and methods of a given class.FieldAlignment
getAlignment(int position)
Returns the alignment of a given field.FieldAlignment
getAlignment(java.lang.String fieldName)
Returns the alignment of a given field.(package private) int[]
getAllLengths()
FieldAlignment[]
getFieldAlignments()
Returns a copy of the sequence of alignment settings to apply over each field in the fixed-width record.int[]
getFieldLengths()
Returns a copy of the sequence of field lengths of a fixed-width recordNormalizedString[]
getFieldNames()
Returns the name of each field in a fixed-width record, if anychar[]
getFieldPaddings()
Returns a copy of the sequence of padding characters to apply over each field in the fixed-width record.(package private) char[]
getFieldPaddings(FixedWidthFormat format)
int
getFieldsPerRecord()
Returns the number of fields in a fixed-width record(package private) boolean[]
getFieldsToIgnore()
Returns the sequence of fields to ignore.(package private) java.lang.Boolean[]
getKeepPaddingFlags()
Returns the sequence of fields whose padding character must/must not be retained in the parsed valueprivate <T> java.util.List<T>
getSelectedElements(java.util.List<T> elements)
int
indexOf(java.lang.String fieldName)
Returns the index of a field name.void
keepPaddingOn(int position, int... positions)
Configures a given list of fields to retain the padding character in any parsed values, overriding the any default set for the whole input inFixedWidthParserSettings.getKeepPadding()
void
keepPaddingOn(java.lang.String name, java.lang.String... names)
Configures a given list of fields to retain the padding character in any parsed values, overriding the any default set for the whole input inFixedWidthParserSettings.getKeepPadding()
private void
setAlignment(int position, FieldAlignment alignment)
void
setAlignment(FieldAlignment alignment, int... positions)
Applies alignment to a given list of fieldsvoid
setAlignment(FieldAlignment alignment, java.lang.String... names)
Applies alignment to a given list of fieldsvoid
setFieldLength(int position, int newLength)
Modifies the length of a given fieldvoid
setFieldLength(java.lang.String name, int newLength)
Modifies the length of a given field(package private) static void
setHeadersIfPossible(FixedWidthFields fieldLengths, CommonSettings settings)
private void
setKeepPaddingFlag(boolean keep, int position, int... positions)
private void
setKeepPaddingFlag(boolean keep, java.lang.String name, java.lang.String... names)
void
setPadding(char padding, int... positions)
Applies a custom padding character to a given list of fieldsvoid
setPadding(char padding, java.lang.String... names)
Applies a custom padding character to a given list of fieldsprivate void
setPadding(int position, char padding)
private void
setPaddingToKeep(int position, boolean keepPaddingFlag)
void
stripPaddingFrom(int position, int... positions)
Configures a given list of fields to remove the padding character in any parsed values, overriding the any default set for the whole input inFixedWidthParserSettings.getKeepPadding()
void
stripPaddingFrom(java.lang.String name, java.lang.String... names)
Configures a given list of fields to remove the padding character in any parsed values, overriding the any default set for the whole input inFixedWidthParserSettings.getKeepPadding()
java.lang.String
toString()
private void
validateIndex(int position)
private void
validateLength(java.lang.String name, int length)
-
-
-
Field Detail
-
fieldLengths
private java.util.List<java.lang.Integer> fieldLengths
-
fieldsToIgnore
private java.util.List<java.lang.Boolean> fieldsToIgnore
-
fieldNames
private java.util.List<NormalizedString> fieldNames
-
fieldAlignment
private java.util.List<FieldAlignment> fieldAlignment
-
fieldPadding
private java.util.List<java.lang.Character> fieldPadding
-
paddingsToKeep
private java.util.List<java.lang.Boolean> paddingsToKeep
-
noNames
private boolean noNames
-
totalLength
private int totalLength
-
-
Constructor Detail
-
FixedWidthFields
public FixedWidthFields(java.util.LinkedHashMap<java.lang.String,java.lang.Integer> fields)
Defines a sequence of field names used to refer to columns in the input/output text of an entity, along with their lengths. The field names defined will be used as headers, having the same effect of a call toCommonSettings.setHeaders(String...)
.- Parameters:
fields
- aLinkedHashMap
containing the sequence of fields to be associated with each column in the input/output, with their respective length.
-
FixedWidthFields
public FixedWidthFields(java.lang.String[] headers, int[] lengths)
Defines a sequence of field names used to refer to columns in the input/output text of an entity, along with their lengths. The field names defined will be used as headers, having the same effect of a call toCommonSettings.setHeaders(String...)
.- Parameters:
headers
- the sequence of fields to be associated with each column in the input/outputlengths
- the sequence of lengths to be associated with each given header. The size of this array must match the number of given headers.
-
FixedWidthFields
public FixedWidthFields(int... fieldLengths)
Creates a new instance initialized with the lengths of all fields in a fixed-width record.- Parameters:
fieldLengths
- The number lengths of all fields in a fixed-width record. All lengths must be greater than 0.
-
FixedWidthFields
@Deprecated public FixedWidthFields(java.lang.Class beanClass)
Deprecated.useforParsing(Class)
andforWriting(Class)
to initialize the fields from the given class and filter out getters and setters that target the same field. If the given class has any annotated methods only the setters will be used, making it usable only for parsing.Creates a new instance initialized fromFixedWidth
annotations in the fields and methods of a given class. Note that all fields should additionally have theParsed
annotation to configure header names and/or their positions.- Parameters:
beanClass
- the class whoseFixedWidth
annotations will be processed to configure this field list.
-
FixedWidthFields
private FixedWidthFields(java.lang.Class beanClass, MethodFilter methodFilter)
Creates a new instance initialized fromFixedWidth
annotations in the fields of a given class. Note that all fields should additionally have theParsed
annotation to configure header names and/or their positions.- Parameters:
beanClass
- the class whoseFixedWidth
annotations will be processed to configure this field list.methodFilter
- filter to apply over annotated methods when the fixed-width writer is reading data from beans (to write values to an output) or writing values into beans (when parsing). It is used to choose either a "get" or a "set" method annotated withParsed
, when both methods target the same field.
-
-
Method Detail
-
forParsing
public static FixedWidthFields forParsing(java.lang.Class beanClass)
Creates a new instance initialized fromFixedWidth
annotations in the fields and methods of a given class. Note that all fields should additionally have theParsed
annotation to configure header names and/or their positions. Only setter methods will be considered as fields.- Parameters:
beanClass
- the class whoseFixedWidth
annotations will be processed to configure this field list.- Returns:
- a new
FixedWidthFields
instance built with theFixedWidth
annotations found in the given class' attributes and methods (excluding getters)
-
forWriting
public static FixedWidthFields forWriting(java.lang.Class beanClass)
Creates a new instance initialized fromFixedWidth
annotations in the fields and methods of a given class. Note that all fields should additionally have theParsed
annotation to configure header names and/or their positions. Only getter methods will be considered as fields.- Parameters:
beanClass
- the class whoseFixedWidth
annotations will be processed to configure this field list.- Returns:
- a new
FixedWidthFields
instance built with theFixedWidth
annotations found in the given class' attributes and methods (excluding setters)
-
addField
public FixedWidthFields addField(int startPosition, int endPosition)
Adds the range of the next field in a fixed-width record. The given range cannot overlap with previously defined fields. Blanks will be used to fill any "gap" between record ranges when writing.- Parameters:
startPosition
- starting position of the field.endPosition
- ending position of the field- Returns:
- the FixedWidthFields instance itself for chaining.
-
addField
public FixedWidthFields addField(java.lang.String name, int startPosition, int endPosition)
Adds the range of the next field in a fixed-width record. The given range cannot overlap with previously defined fields. Blanks will be used to fill any "gap" between record ranges when writing.- Parameters:
name
- the name of the next field. It is not validated.startPosition
- starting position of the field.endPosition
- ending position of the field- Returns:
- the FixedWidthFields instance itself for chaining.
-
addField
public FixedWidthFields addField(java.lang.String name, int startPosition, int endPosition, char padding)
Adds the range of the next field in a fixed-width record. The given range cannot overlap with previously defined fields. Blanks will be used to fill any "gap" between record ranges when writing.- Parameters:
name
- the name of the next field. It is not validated.startPosition
- starting position of the field.endPosition
- ending position of the fieldpadding
- the representation of unused space in this field- Returns:
- the FixedWidthFields instance itself for chaining.
-
addField
public FixedWidthFields addField(java.lang.String name, int startPosition, int endPosition, FieldAlignment alignment)
Adds the range of the next field in a fixed-width record. The given range cannot overlap with previously defined fields. Blanks will be used to fill any "gap" between record ranges when writing.- Parameters:
name
- the name of the next field. It is not validated.startPosition
- starting position of the field.endPosition
- ending position of the fieldalignment
- the alignment of the field- Returns:
- the FixedWidthFields instance itself for chaining.
-
addField
public FixedWidthFields addField(int startPosition, int endPosition, FieldAlignment alignment)
Adds the range of the next field in a fixed-width record. The given range cannot overlap with previously defined fields. Blanks will be used to fill any "gap" between record ranges when writing.- Parameters:
startPosition
- starting position of the field.endPosition
- ending position of the fieldalignment
- the alignment of the field- Returns:
- the FixedWidthFields instance itself for chaining.
-
addField
public FixedWidthFields addField(int startPosition, int endPosition, FieldAlignment alignment, char padding)
Adds the range of the next field in a fixed-width record. The given range cannot overlap with previously defined fields. Blanks will be used to fill any "gap" between record ranges when writing.- Parameters:
startPosition
- starting position of the field.endPosition
- ending position of the fieldalignment
- the alignment of the fieldpadding
- the representation of unused space in this field- Returns:
- the FixedWidthFields instance itself for chaining.
-
addField
public FixedWidthFields addField(int startPosition, int endPosition, char padding)
Adds the range of the next field in a fixed-width record. The given range cannot overlap with previously defined fields. Blanks will be used to fill any "gap" between record ranges when writing.- Parameters:
startPosition
- starting position of the field.endPosition
- ending position of the fieldpadding
- the representation of unused space in this field- Returns:
- the FixedWidthFields instance itself for chaining.
-
addField
public FixedWidthFields addField(java.lang.String name, int startPosition, int endPosition, FieldAlignment alignment, char padding)
Adds the range of the next field in a fixed-width record. The given range cannot overlap with previously defined fields. Blanks will be used to fill any "gap" between record ranges when writing.- Parameters:
name
- the name of the next field. It is not validated.startPosition
- starting position of the field.endPosition
- ending position of the fieldalignment
- the alignment of the fieldpadding
- the representation of unused space in this field- Returns:
- the FixedWidthFields instance itself for chaining.
-
getFieldsToIgnore
boolean[] getFieldsToIgnore()
Returns the sequence of fields to ignore.- Returns:
- the sequence of fields to ignore.
-
getKeepPaddingFlags
java.lang.Boolean[] getKeepPaddingFlags()
Returns the sequence of fields whose padding character must/must not be retained in the parsed value- Returns:
- the sequence that have an explicit 'keepPadding' flag.
-
addField
public FixedWidthFields addField(int length)
Adds the length of the next field in a fixed-width record. This method can be chained like this: addField(5).addField(6)...- Parameters:
length
- the length of the next field. It must be greater than 0.- Returns:
- the FixedWidthFields instance itself for chaining.
-
addField
public FixedWidthFields addField(int length, FieldAlignment alignment)
Adds the length of the next field in a fixed-width record. This method can be chained like this: addField(5).addField(6)...- Parameters:
length
- the length of the next field. It must be greater than 0.alignment
- the alignment of the field- Returns:
- the FixedWidthFields instance itself for chaining.
-
addField
public FixedWidthFields addField(java.lang.String name, int length)
Adds the length of the next field in a fixed-width record. This method can be chained like this: addField("field_1", 5).addField("field_2", 6)...- Parameters:
name
- the name of the next field. It is not validated.length
- the length of the next field. It must be greater than 0.- Returns:
- the FixedWidthFields instance itself for chaining.
-
addField
public FixedWidthFields addField(java.lang.String name, int length, FieldAlignment alignment)
Adds the length of the next field in a fixed-width record. This method can be chained like this: addField("field_1", 5).addField("field_2", 6)...- Parameters:
name
- the name of the next field. It is not validated.length
- the length of the next field. It must be greater than 0.alignment
- the alignment of the field- Returns:
- the FixedWidthFields instance itself for chaining.
-
addField
public FixedWidthFields addField(int length, char padding)
Adds the length of the next field in a fixed-width record. This method can be chained like this: addField(5).addField(6)...- Parameters:
length
- the length of the next field. It must be greater than 0.padding
- the representation of unused space in this field- Returns:
- the FixedWidthFields instance itself for chaining.
-
addField
public FixedWidthFields addField(int length, FieldAlignment alignment, char padding)
Adds the length of the next field in a fixed-width record. This method can be chained like this: addField(5).addField(6)...- Parameters:
length
- the length of the next field. It must be greater than 0.alignment
- the alignment of the fieldpadding
- the representation of unused space in this field- Returns:
- the FixedWidthFields instance itself for chaining.
-
addField
public FixedWidthFields addField(java.lang.String name, int length, char padding)
Adds the length of the next field in a fixed-width record. This method can be chained like this: addField("field_1", 5).addField("field_2", 6)...- Parameters:
name
- the name of the next field. It is not validated.length
- the length of the next field. It must be greater than 0.padding
- the representation of unused space in this field- Returns:
- the FixedWidthFields instance itself for chaining.
-
addField
public FixedWidthFields addField(java.lang.String name, int length, FieldAlignment alignment, char padding)
Adds the length of the next field in a fixed-width record. This method can be chained like this: addField("field_1", 5).addField("field_2", 6)...- Parameters:
name
- the name of the next field. It is not validated.length
- the length of the next field. It must be greater than 0.alignment
- the alignment of the fieldpadding
- the representation of unused space in this field- Returns:
- the FixedWidthFields instance itself for chaining.
-
validateLength
private void validateLength(java.lang.String name, int length)
-
getFieldsPerRecord
public int getFieldsPerRecord()
Returns the number of fields in a fixed-width record- Returns:
- the number of fields in a fixed-width record
-
getFieldNames
public NormalizedString[] getFieldNames()
Returns the name of each field in a fixed-width record, if any- Returns:
- the name of each field in a fixed-width record, or null if no name has been defined.
-
getSelectedElements
private <T> java.util.List<T> getSelectedElements(java.util.List<T> elements)
-
getFieldLengths
public int[] getFieldLengths()
Returns a copy of the sequence of field lengths of a fixed-width record- Returns:
- a copy of the sequence of field lengths of a fixed-width record
-
getAllLengths
int[] getAllLengths()
-
setFieldLength
public void setFieldLength(java.lang.String name, int newLength)
Modifies the length of a given field- Parameters:
name
- the name of the field whose length must be alterednewLength
- the new length of the given field
-
setFieldLength
public void setFieldLength(int position, int newLength)
Modifies the length of a given field- Parameters:
position
- the position of the field whose length must be alterednewLength
- the new length of the given field
-
setAlignment
public void setAlignment(FieldAlignment alignment, int... positions)
Applies alignment to a given list of fields- Parameters:
alignment
- the alignment to applypositions
- the positions of the fields that should be aligned
-
setAlignment
public void setAlignment(FieldAlignment alignment, java.lang.String... names)
Applies alignment to a given list of fields- Parameters:
alignment
- the alignment to applynames
- the names of the fields that should be aligned
-
validateIndex
private void validateIndex(int position)
-
indexOf
public int indexOf(java.lang.String fieldName)
Returns the index of a field name. AnIllegalArgumentException
will be thrown if no names have been defined.- Parameters:
fieldName
- the name of the field to be searched- Returns:
- the index of the field, or -1 if it does not exist.
-
setAlignment
private void setAlignment(int position, FieldAlignment alignment)
-
getAlignment
public FieldAlignment getAlignment(int position)
Returns the alignment of a given field.- Parameters:
position
- the index of the field whose alignment will be returned- Returns:
- the alignment of the field
-
getAlignment
public FieldAlignment getAlignment(java.lang.String fieldName)
Returns the alignment of a given field. AnIllegalArgumentException
will be thrown if no names have been defined.- Parameters:
fieldName
- the name of the field whose alignment will be returned- Returns:
- the alignment of the given field
-
getFieldAlignments
public FieldAlignment[] getFieldAlignments()
Returns a copy of the sequence of alignment settings to apply over each field in the fixed-width record.- Returns:
- the sequence of alignment settings to apply over each field in the fixed-width record.
-
getFieldPaddings
public char[] getFieldPaddings()
Returns a copy of the sequence of padding characters to apply over each field in the fixed-width record. The null character ('\0'
) is used to inform no padding has been explicitly set for a field, and that the default padding character defined inFixedWidthFormat.getPadding()
should be used.- Returns:
- the sequence of padding characters to apply over each field in the fixed-width record.
-
getFieldPaddings
char[] getFieldPaddings(FixedWidthFormat format)
-
setPadding
public void setPadding(char padding, int... positions)
Applies a custom padding character to a given list of fields- Parameters:
padding
- the padding to applypositions
- the positions of the fields that should use the given padding character
-
setPadding
public void setPadding(char padding, java.lang.String... names)
Applies a custom padding character to a given list of fields- Parameters:
padding
- the padding to applynames
- the names of the fields that should use the given padding character
-
setPadding
private void setPadding(int position, char padding)
-
keepPaddingOn
public void keepPaddingOn(int position, int... positions)
Configures a given list of fields to retain the padding character in any parsed values, overriding the any default set for the whole input inFixedWidthParserSettings.getKeepPadding()
- Parameters:
position
- the positions of the fields that should keep the padding characterpositions
- additional positions
-
keepPaddingOn
public void keepPaddingOn(java.lang.String name, java.lang.String... names)
Configures a given list of fields to retain the padding character in any parsed values, overriding the any default set for the whole input inFixedWidthParserSettings.getKeepPadding()
- Parameters:
name
- the name of the first field that should keep the padding characternames
- the names of more fields that should keep the padding character
-
stripPaddingFrom
public void stripPaddingFrom(int position, int... positions)
Configures a given list of fields to remove the padding character in any parsed values, overriding the any default set for the whole input inFixedWidthParserSettings.getKeepPadding()
- Parameters:
position
- the positions of the fields that should keep the padding characterpositions
- additional positions
-
stripPaddingFrom
public void stripPaddingFrom(java.lang.String name, java.lang.String... names)
Configures a given list of fields to remove the padding character in any parsed values, overriding the any default set for the whole input inFixedWidthParserSettings.getKeepPadding()
- Parameters:
name
- the name of the first field that have any padding characters removednames
- the names of the fields that should have any padding characters removed
-
setKeepPaddingFlag
private void setKeepPaddingFlag(boolean keep, int position, int... positions)
-
setKeepPaddingFlag
private void setKeepPaddingFlag(boolean keep, java.lang.String name, java.lang.String... names)
-
setPaddingToKeep
private void setPaddingToKeep(int position, boolean keepPaddingFlag)
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
setHeadersIfPossible
static void setHeadersIfPossible(FixedWidthFields fieldLengths, CommonSettings settings)
-
clone
protected FixedWidthFields clone()
- Overrides:
clone
in classjava.lang.Object
-
-