Skip to content

Commit

Permalink
refactor: reduce cognitive complexity of buildCell method
Browse files Browse the repository at this point in the history
  • Loading branch information
mlopezFC committed Nov 11, 2024
1 parent 310afd3 commit 98491e1
Showing 1 changed file with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -341,20 +341,9 @@ private CellStyle createOrRetrieveCellStyle(HorizontalAlignment alignment, Cell

@SuppressWarnings("unchecked")
private void buildCell(Object value, Cell cell, Column<T> column, T item) {
ValueProvider<T,String> provider = null;
provider = (ValueProvider<T, String>) ComponentUtil.getData(column, GridExporter.COLUMN_EXCEL_FORMAT_DATA_PROVIDER);
String excelFormat;
Map<String,CellStyle> cellStyles = (Map<String, CellStyle>) ComponentUtil.getData(column, COLUMN_CELLSTYLE_MAP);
if (cellStyles==null) {
cellStyles = new HashMap<>();
ComponentUtil.setData(column, COLUMN_CELLSTYLE_MAP, cellStyles);
}
if (provider!=null) {
excelFormat = provider.apply(item);
} else {
excelFormat =
(String) ComponentUtil.getData(column, GridExporter.COLUMN_EXCEL_FORMAT_DATA);
}
ValueProvider<T,String> provider = (ValueProvider<T, String>) ComponentUtil.getData(column, GridExporter.COLUMN_EXCEL_FORMAT_DATA_PROVIDER);
Map<String, CellStyle> cellStyles = getCellStyles(column);
String excelFormat = getExcelFormat(column, item, provider);
if (value == null) {
PoiHelper.setBlank(cell);
if(excelFormat != null) {
Expand All @@ -378,6 +367,27 @@ private void buildCell(Object value, Cell cell, Column<T> column, T item) {
}
}

private String getExcelFormat(Column<T> column, T item, ValueProvider<T, String> provider) {
String excelFormat;
if (provider!=null) {
excelFormat = provider.apply(item);
} else {
excelFormat =
(String) ComponentUtil.getData(column, GridExporter.COLUMN_EXCEL_FORMAT_DATA);
}
return excelFormat;
}

@SuppressWarnings("unchecked")
private Map<String, CellStyle> getCellStyles(Column<T> column) {
Map<String,CellStyle> cellStyles = (Map<String, CellStyle>) ComponentUtil.getData(column, COLUMN_CELLSTYLE_MAP);
if (cellStyles==null) {
cellStyles = new HashMap<>();
ComponentUtil.setData(column, COLUMN_CELLSTYLE_MAP, cellStyles);
}
return cellStyles;
}

private void applyExcelFormat(Cell cell, String excelFormat, Map<String, CellStyle> cellStyles) {
DataFormat format = cell.getSheet().getWorkbook().createDataFormat();
if (excelFormat!=null && cellStyles.get(excelFormat)==null) {
Expand Down

0 comments on commit 98491e1

Please sign in to comment.