I have a tsv
file which has fixed rows but each row is mapped to different Java Class.
For example.
recordType recordValue1
recordType recordValue1 recordValue2
for First row I have follofing class:
public class FirstRow implements ItsvRecord {
@Parsed(index = 0)
private String recordType;
@Parsed(index = 1)
private String recordValue1;
public FirstRow() {
}
}
and for second row I have:
public class SecondRow implements ItsvRecord {
@Parsed(index = 0)
private String recordType;
@Parsed(index = 1)
private String recordValue1;
public SecondRow() {
}
}
I want to parse the TSV file directly to the respective objects but I am falling short of ideas.
Use an
InputValueSwitch
. This will match a value in a particular column of each row to determine whatRowProcessor
to use. Example:Create two (or more) processors for each type of record you need to process:
Create an
InputValueSwitch
:Parse as usual:
Get the parsed objects from your processors:
The output will be*:
If you want to manage associations among the objects that are parsed:
If your
FirstRow
should contain the elements parsed for records of typeSecondRow
, simply override therowProcessorSwitched
method:FirstRow
class has aaddRowsOfOtherType
method that takes a list ofSecondRow
as parameter.And that's it!
You can even mix and match other types of
RowProcessor
. There's another example here that demonstrates this.Hope this helps.