Skip to content

Commit b37e32e

Browse files
committed
[util]: refactored blank String checks with Strings.isBlank util
1 parent 128ec7e commit b37e32e

File tree

6 files changed

+26
-13
lines changed

6 files changed

+26
-13
lines changed

src/main/java/io/github/millij/poi/ss/handler/AbstractSheetContentsHandler.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
import java.util.HashMap;
44
import java.util.Map;
5-
import java.util.Objects;
65

76
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler;
87
import org.apache.poi.xssf.usermodel.XSSFComment;
98
import org.slf4j.Logger;
109
import org.slf4j.LoggerFactory;
1110

1211
import io.github.millij.poi.util.Spreadsheet;
12+
import io.github.millij.poi.util.Strings;
1313

1414

1515
abstract class AbstractSheetContentsHandler implements SheetContentsHandler {
@@ -30,7 +30,6 @@ abstract class AbstractSheetContentsHandler implements SheetContentsHandler {
3030
abstract void afterRowEnd(int rowNum, Map<String, Object> rowObj);
3131

3232

33-
3433
// SheetContentsHandler Implementations
3534
// ------------------------------------------------------------------------
3635

@@ -53,12 +52,12 @@ public void endRow(final int rowNum) {
5352
@Override
5453
public void cell(final String cellRef, final String cellVal, final XSSFComment comment) {
5554
// Sanity Checks
56-
if (Objects.isNull(cellRef) || cellRef.isBlank()) {
55+
if (Strings.isBlank(cellRef)) {
5756
LOGGER.error("Row[#] {} : Cell reference is empty - {}", currentRow, cellRef);
5857
return;
5958
}
6059

61-
if (Objects.isNull(cellVal) || cellVal.isBlank()) {
60+
if (Strings.isBlank(cellVal)) {
6261
LOGGER.warn("Row[#] {} - Cell[ref] formatted value is empty : {} - {}", currentRow, cellRef, cellVal);
6362
return;
6463
}

src/main/java/io/github/millij/poi/ss/reader/XlsReader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ protected void processSheet(final HSSFSheet sheet, final RowListener<Map<String,
179179
}
180180

181181
final Map<String, Object> rowDataMap = this.extractRowDataAsMap(row);
182-
if (rowDataMap == null || rowDataMap.isEmpty()) {
182+
if (Objects.isNull(rowDataMap) || rowDataMap.isEmpty()) {
183183
continue;
184184
}
185185

@@ -209,7 +209,7 @@ protected <T> void processSheet(final Class<T> beanClz, final HSSFSheet sheet, f
209209
}
210210

211211
final Map<String, Object> rowDataMap = this.extractRowDataAsMap(row);
212-
if (rowDataMap == null || rowDataMap.isEmpty()) {
212+
if (Objects.isNull(rowDataMap) || rowDataMap.isEmpty()) {
213213
continue;
214214
}
215215

src/main/java/io/github/millij/poi/ss/writer/AbstractSpreadsheetWriter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import io.github.millij.poi.ss.model.Column;
2323
import io.github.millij.poi.util.Spreadsheet;
24+
import io.github.millij.poi.util.Strings;
2425

2526

2627
/**
@@ -77,7 +78,7 @@ public <T> void addSheet(final Class<T> beanType, final List<T> rowObjects, fina
7778
}
7879

7980
// Create sheet
80-
final Sheet sheet = Objects.isNull(sheetName) || sheetName.isBlank() //
81+
final Sheet sheet = Strings.isBlank(sheetName) //
8182
? workbook.createSheet() //
8283
: workbook.createSheet(sheetName);
8384
LOGGER.debug("Added new Sheet[name] to the workbook : {}", sheet.getSheetName());
@@ -128,7 +129,7 @@ public void addSheet(final String inSheetName, final List<String> inHeaders,
128129
}
129130

130131
// Create sheet
131-
final Sheet sheet = Objects.isNull(inSheetName) || inSheetName.isBlank() //
132+
final Sheet sheet = Strings.isBlank(inSheetName) //
132133
? workbook.createSheet() //
133134
: workbook.createSheet(inSheetName);
134135
LOGGER.debug("Added new Sheet[name] to the workbook : {}", sheet.getSheetName());

src/main/java/io/github/millij/poi/util/Beans.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public static void setProperty(final Object target, final String propName, final
210210
private static void setDateTimeProperty(final Object target, final String propName, final Class<?> propType,
211211
final Object propValue, final String format, final DateTimeType dateTimeType) throws Exception {
212212
// Input value Format
213-
final String dateFormatStr = Objects.isNull(format) || format.isBlank() ? "dd/MM/yyyy" : format;
213+
final String dateFormatStr = Strings.isBlank(format) ? "dd/MM/yyyy" : format;
214214

215215
// Parse
216216
final SimpleDateFormat dateFmt = new SimpleDateFormat(dateFormatStr);

src/main/java/io/github/millij/poi/util/Spreadsheet.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static String getSheetName(final Class<?> beanType) {
6767
public static String getSheetColumnName(final SheetColumn sheetColumn, final String defaultName) {
6868
// Name
6969
final String scValue = sheetColumn.value();
70-
final String colName = Objects.isNull(scValue) || scValue.isBlank() ? defaultName : scValue;
70+
final String colName = Strings.isBlank(scValue) ? defaultName : scValue;
7171

7272
return colName;
7373
}
@@ -216,7 +216,7 @@ public static Map<String, Object> rowAsMap(final Map<String, String> headerCellR
216216
for (final String propColName : headerCellRefsMap.keySet()) {
217217
// Get the Header Cell Ref
218218
final String propCellRef = headerCellRefsMap.get(propColName);
219-
if (Objects.isNull(propCellRef) || propCellRef.isBlank()) {
219+
if (Strings.isBlank(propCellRef)) {
220220
continue;
221221
}
222222

@@ -263,7 +263,7 @@ public static <T> T rowAsBean(Class<T> beanClz, Map<String, Column> propColumnMa
263263
// Get the Header Cell Ref
264264
final String normalizedColName = Strings.normalize(propColName);
265265
final String propCellRef = headerCellRefsMap.get(normalizedColName);
266-
if (Objects.isNull(propCellRef) || propCellRef.isBlank()) {
266+
if (Strings.isBlank(propCellRef)) {
267267
LOGGER.debug("{} :: No Cell Ref found [Prop - Col] : [{} - {}]", beanClz, propName, propColName);
268268
continue;
269269
}
@@ -310,7 +310,7 @@ private static boolean validateRowData(final Map<String, Object> rowDataMap,
310310
final String propCellRef = headerCellRefsMap.containsKey(propColName) //
311311
? headerCellRefsMap.get(propColName) //
312312
: headerCellRefsMap.get(normalizedColName);
313-
if (Objects.isNull(propCellRef) || propCellRef.isBlank()) {
313+
if (Strings.isBlank(propCellRef)) {
314314
continue;
315315
}
316316

src/main/java/io/github/millij/poi/util/Strings.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,17 @@ public static String normalize(final String inStr) {
5252
}
5353

5454

55+
// Checks
56+
57+
/**
58+
* Check if a String is <code>null</code> or contains only white spaces
59+
*
60+
* @param str input string
61+
*
62+
* @return <code>true</code> if the input String is blank
63+
*/
64+
public static boolean isBlank(final String str) {
65+
return Objects.isNull(str) || str.trim().isEmpty();
66+
}
67+
5568
}

0 commit comments

Comments
 (0)