I am using Apache POI to read an Excel file having many columns. I have a POJO in which there is a field for each column in Excel sheet.
How do I populate my POJO with values from Excel achieving minimum cyclomatic complexity?
I had implemented really crude approach of having switch..case
for populating my POJO, but this results in tight coupling and gives rise to high cyclomatic complexity, giving below my implementation for reference (this is I feel very dirty implementation) :
// Iterate over cells in a row
for (Cell cell : row) {
cellValue = getCellValue(cell);
if (cellValue == null) {
continue;
}
// Populate model
data = populateModel(data, cell.getColumnIndex(), cellValue);
}
called method:
/*
* Populates pojo
*/
private MyData populateModel(MyData data, int columnIndex, Object cellValue) {
switch (columnIndex) {
case 0:
data.setAccount((long) cellValue);
break;
case 1:
data.setDocNumber((long) cellValue);
break;
case 2:
data.setType((String) cellValue);
break;
// more cases for subsequent excel columns
default:
// do nothing
}
return data;
}