Skip to content

Commit e52f6ac

Browse files
mlopezFCpaodb
authored andcommitted
refactor: reduce cognitive complexity of buildCell method
1 parent 3194a8c commit e52f6ac

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

src/main/java/com/flowingcode/vaadin/addons/gridexporter/ExcelStreamResourceWriter.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -341,20 +341,9 @@ private CellStyle createOrRetrieveCellStyle(HorizontalAlignment alignment, Cell
341341

342342
@SuppressWarnings("unchecked")
343343
private void buildCell(Object value, Cell cell, Column<T> column, T item) {
344-
ValueProvider<T,String> provider = null;
345-
provider = (ValueProvider<T, String>) ComponentUtil.getData(column, GridExporter.COLUMN_EXCEL_FORMAT_DATA_PROVIDER);
346-
String excelFormat;
347-
Map<String,CellStyle> cellStyles = (Map<String, CellStyle>) ComponentUtil.getData(column, COLUMN_CELLSTYLE_MAP);
348-
if (cellStyles==null) {
349-
cellStyles = new HashMap<>();
350-
ComponentUtil.setData(column, COLUMN_CELLSTYLE_MAP, cellStyles);
351-
}
352-
if (provider!=null) {
353-
excelFormat = provider.apply(item);
354-
} else {
355-
excelFormat =
356-
(String) ComponentUtil.getData(column, GridExporter.COLUMN_EXCEL_FORMAT_DATA);
357-
}
344+
ValueProvider<T,String> provider = (ValueProvider<T, String>) ComponentUtil.getData(column, GridExporter.COLUMN_EXCEL_FORMAT_DATA_PROVIDER);
345+
Map<String, CellStyle> cellStyles = getCellStyles(column);
346+
String excelFormat = getExcelFormat(column, item, provider);
358347
if (value == null) {
359348
PoiHelper.setBlank(cell);
360349
if(excelFormat != null) {
@@ -378,6 +367,27 @@ private void buildCell(Object value, Cell cell, Column<T> column, T item) {
378367
}
379368
}
380369

370+
private String getExcelFormat(Column<T> column, T item, ValueProvider<T, String> provider) {
371+
String excelFormat;
372+
if (provider!=null) {
373+
excelFormat = provider.apply(item);
374+
} else {
375+
excelFormat =
376+
(String) ComponentUtil.getData(column, GridExporter.COLUMN_EXCEL_FORMAT_DATA);
377+
}
378+
return excelFormat;
379+
}
380+
381+
@SuppressWarnings("unchecked")
382+
private Map<String, CellStyle> getCellStyles(Column<T> column) {
383+
Map<String,CellStyle> cellStyles = (Map<String, CellStyle>) ComponentUtil.getData(column, COLUMN_CELLSTYLE_MAP);
384+
if (cellStyles==null) {
385+
cellStyles = new HashMap<>();
386+
ComponentUtil.setData(column, COLUMN_CELLSTYLE_MAP, cellStyles);
387+
}
388+
return cellStyles;
389+
}
390+
381391
private void applyExcelFormat(Cell cell, String excelFormat, Map<String, CellStyle> cellStyles) {
382392
DataFormat format = cell.getSheet().getWorkbook().createDataFormat();
383393
if (excelFormat!=null && cellStyles.get(excelFormat)==null) {

0 commit comments

Comments
 (0)