Skip to content

Commit 3fd86f9

Browse files
committed
Repetitive checks and repetitive code have been removed
1 parent d248ff1 commit 3fd86f9

File tree

4 files changed

+45
-87
lines changed

4 files changed

+45
-87
lines changed

src/main/java/tools/Converter.java

+8-16
Original file line numberDiff line numberDiff line change
@@ -319,15 +319,11 @@ public static List<?> excelToObjects(File file, Class<?> clazz) throws Extension
319319
* @throws HeaderNotPresentException If the first row is empty and does not contain the header
320320
*/
321321
public static List<?> excelToObjects(File file, Class<?> clazz, String sheetName) throws ExtensionNotValidException, IOException, OpenWorkbookException, InvocationTargetException, IllegalAccessException, NoSuchMethodException, InstantiationException, SheetNotFoundException, HeaderNotPresentException {
322-
/* Check extension */
323-
String extension = ExcelUtility.checkExcelExtension(file.getName());
324-
325322
/* Open file excel */
326-
FileInputStream fileInputStream = new FileInputStream(file);
327-
Workbook workbook = WorkbookUtility.open(fileInputStream, extension);
323+
Workbook workbook = WorkbookUtility.open(file);
328324
Sheet sheet = (sheetName == null || sheetName.isEmpty())
329-
? SheetUtility.open(workbook)
330-
: SheetUtility.open(workbook, sheetName);
325+
? SheetUtility.get(workbook)
326+
: SheetUtility.get(workbook, sheetName);
331327

332328
/* Retrieving header names */
333329
Field[] fields = clazz.getDeclaredFields();
@@ -346,7 +342,7 @@ public static List<?> excelToObjects(File file, Class<?> clazz, String sheetName
346342
}
347343

348344
/* Close file */
349-
WorkbookUtility.close(workbook, fileInputStream);
345+
WorkbookUtility.close(workbook);
350346

351347
return resultList;
352348
}
@@ -413,15 +409,11 @@ public static File excelToCsv(File fileInput, String path, String filename) thro
413409
* @throws IOException If an I/O error has occurred
414410
*/
415411
public static File excelToCsv(File fileInput, String path, String filename, String sheetName) throws ExtensionNotValidException, IOException, OpenWorkbookException, SheetNotFoundException, FileAlreadyExistsException {
416-
/* Check extension */
417-
String extension = ExcelUtility.checkExcelExtension(fileInput.getName());
418-
419412
/* Open file excel */
420-
FileInputStream fileInputStream = new FileInputStream(fileInput);
421-
Workbook workbook = WorkbookUtility.open(fileInputStream, extension);
413+
Workbook workbook = WorkbookUtility.open(fileInput);
422414
Sheet sheet = (sheetName == null || sheetName.isEmpty())
423-
? SheetUtility.open(workbook)
424-
: SheetUtility.open(workbook, sheetName);
415+
? SheetUtility.get(workbook)
416+
: SheetUtility.get(workbook, sheetName);
425417

426418
/* Create output file */
427419
String pathname = getPathname(path, filename, Extension.CSV.getExt());
@@ -445,7 +437,7 @@ public static File excelToCsv(File fileInput, String path, String filename, Stri
445437
}
446438

447439
/* Close file */
448-
WorkbookUtility.close(workbook, fileInputStream, csvWriter);
440+
WorkbookUtility.close(workbook, csvWriter);
449441

450442
return csvFile;
451443
}

src/main/java/tools/ExcelUtility.java

+8-16
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,8 @@ public static List<Integer> countAllRowsOfAllSheets(File file) throws ExtensionN
4646
* @throws OpenWorkbookException If an error occurred while opening the workbook
4747
*/
4848
public static List<Integer> countAllRowsOfAllSheets(File file, Boolean alsoEmptyRows) throws ExtensionNotValidException, IOException, OpenWorkbookException {
49-
/* Check extension */
50-
String extension = checkExcelExtension(file.getName());
51-
5249
/* Open file excel */
53-
FileInputStream fileInputStream = new FileInputStream(file);
54-
Workbook workbook = WorkbookUtility.open(fileInputStream, extension);
50+
Workbook workbook = WorkbookUtility.open(file);
5551

5652
List<Integer> values = new LinkedList<>();
5753
for (Sheet sheet : workbook) {
@@ -64,7 +60,7 @@ public static List<Integer> countAllRowsOfAllSheets(File file, Boolean alsoEmpty
6460
}
6561

6662
/* Close file */
67-
WorkbookUtility.close(workbook, fileInputStream);
63+
WorkbookUtility.close(workbook);
6864

6965
return values;
7066
}
@@ -96,23 +92,19 @@ public static Integer countAllRows(File file, String sheetName) throws OpenWorkb
9692
* @throws IOException If an I/O error occurs
9793
*/
9894
public static Integer countAllRows(File file, String sheetName, Boolean alsoEmptyRows) throws ExtensionNotValidException, IOException, OpenWorkbookException, SheetNotFoundException {
99-
/* Check extension */
100-
String extension = checkExcelExtension(file.getName());
101-
10295
/* Open file excel */
103-
FileInputStream fileInputStream = new FileInputStream(file);
104-
Workbook workbook = WorkbookUtility.open(fileInputStream, extension);
96+
Workbook workbook = WorkbookUtility.open(file);
10597
Sheet sheet = (sheetName == null || sheetName.isEmpty())
106-
? SheetUtility.open(workbook)
107-
: SheetUtility.open(workbook, sheetName);
98+
? SheetUtility.get(workbook)
99+
: SheetUtility.get(workbook, sheetName);
108100

109101
/* Count all rows */
110102
int numRows = alsoEmptyRows
111103
? sheet.getLastRowNum() + 1
112104
: countOnlyRowsNotEmpty(sheet);
113105

114106
/* Close file */
115-
WorkbookUtility.close(workbook, fileInputStream);
107+
WorkbookUtility.close(workbook);
116108

117109
return numRows;
118110
}
@@ -165,7 +157,7 @@ public static Integer getIndexLastRow(File file) throws OpenWorkbookException, S
165157
* @throws IOException If an I/O error occurs
166158
*/
167159
public static Integer getIndexLastRow(File file, String sheetName) throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException {
168-
Sheet sheet = (sheetName == null || sheetName.isEmpty()) ? SheetUtility.open(file) : SheetUtility.open(file, sheetName);
160+
Sheet sheet = (sheetName == null || sheetName.isEmpty()) ? SheetUtility.get(file) : SheetUtility.get(file, sheetName);
169161
return sheet.getLastRowNum() + 1;
170162
}
171163

@@ -225,7 +217,7 @@ public static Integer getIndexLastColumn(File file, Integer indexRow) throws Ope
225217
* @throws IOException If an I/O error occurs
226218
*/
227219
public static Integer getIndexLastColumn(File file, String sheetName, Integer indexRow) throws OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException {
228-
Sheet sheet = (sheetName == null || sheetName.isEmpty()) ? SheetUtility.open(file) : SheetUtility.open(file, sheetName);
220+
Sheet sheet = (sheetName == null || sheetName.isEmpty()) ? SheetUtility.get(file) : SheetUtility.get(file, sheetName);
229221
return (int) sheet.getRow(indexRow).getLastCellNum();
230222
}
231223

src/main/java/tools/SheetUtility.java

+28-52
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,13 @@ public class SheetUtility {
2929
* @throws OpenWorkbookException If an error occurred while opening the workbook
3030
*/
3131
public static Integer length(File file) throws ExtensionNotValidException, IOException, OpenWorkbookException {
32-
/* Check extension */
33-
String extension = ExcelUtility.checkExcelExtension(file.getName());
34-
3532
/* Open file excel */
36-
FileInputStream fileInputStream = new FileInputStream(file);
37-
Workbook workbook = WorkbookUtility.open(fileInputStream, extension);
33+
Workbook workbook = WorkbookUtility.open(file);
3834

3935
Integer totalSheets = workbook.getNumberOfSheets();
4036

4137
/* Close file */
42-
WorkbookUtility.close(workbook, fileInputStream);
38+
WorkbookUtility.close(workbook);
4339

4440
return totalSheets;
4541
}
@@ -53,12 +49,8 @@ public static Integer length(File file) throws ExtensionNotValidException, IOExc
5349
* @throws OpenWorkbookException If an error occurred while opening the workbook
5450
*/
5551
public static List<String> getNames(File file) throws ExtensionNotValidException, IOException, OpenWorkbookException {
56-
/* Check extension */
57-
String extension = ExcelUtility.checkExcelExtension(file.getName());
58-
5952
/* Open file excel */
60-
FileInputStream fileInputStream = new FileInputStream(file);
61-
Workbook workbook = WorkbookUtility.open(fileInputStream, extension);
53+
Workbook workbook = WorkbookUtility.open(file);
6254

6355
/* Iterate all the sheets */
6456
Iterator<Sheet> sheetIterator = workbook.iterator();
@@ -69,7 +61,7 @@ public static List<String> getNames(File file) throws ExtensionNotValidException
6961
}
7062

7163
/* Close file */
72-
WorkbookUtility.close(workbook, fileInputStream);
64+
WorkbookUtility.close(workbook);
7365

7466
return sheetNames;
7567
}
@@ -85,17 +77,13 @@ public static List<String> getNames(File file) throws ExtensionNotValidException
8577
* @throws SheetNotFoundException If the sheet to open is not found
8678
*/
8779
public static Integer getIndex(File file, String sheetName) throws ExtensionNotValidException, IOException, OpenWorkbookException, SheetNotFoundException {
88-
/* Check extension */
89-
String extension = ExcelUtility.checkExcelExtension(file.getName());
90-
9180
/* Open file excel */
92-
FileInputStream fileInputStream = new FileInputStream(file);
93-
Workbook workbook = WorkbookUtility.open(fileInputStream, extension);
81+
Workbook workbook = WorkbookUtility.open(file);
9482

9583
int sheetIndex = workbook.getSheetIndex(sheetName);
9684

9785
/* Close file */
98-
WorkbookUtility.close(workbook, fileInputStream);
86+
WorkbookUtility.close(workbook);
9987

10088
if (sheetIndex < 0) {
10189
throw new SheetNotFoundException("No sheet was found");
@@ -114,12 +102,8 @@ public static Integer getIndex(File file, String sheetName) throws ExtensionNotV
114102
* @throws SheetNotFoundException If the sheet to open is not found
115103
*/
116104
public static String getName(File file, Integer position) throws ExtensionNotValidException, IOException, OpenWorkbookException, SheetNotFoundException {
117-
/* Check extension */
118-
String extension = ExcelUtility.checkExcelExtension(file.getName());
119-
120105
/* Open file excel */
121-
FileInputStream fileInputStream = new FileInputStream(file);
122-
Workbook workbook = WorkbookUtility.open(fileInputStream, extension);
106+
Workbook workbook = WorkbookUtility.open(file);
123107

124108
String sheetName;
125109
try {
@@ -129,7 +113,7 @@ public static String getName(File file, Integer position) throws ExtensionNotVal
129113
}
130114

131115
/* Close file */
132-
WorkbookUtility.close(workbook, fileInputStream);
116+
WorkbookUtility.close(workbook);
133117

134118
return sheetName;
135119
}
@@ -187,7 +171,7 @@ public static Sheet create(Workbook workbook, String sheetName) {
187171
}
188172

189173
/**
190-
* Opens the sheet of the Excel file<p>
174+
* Gets the sheet of the Excel file<p>
191175
* If not specified, the first sheet will be opened
192176
* @param file Excel file
193177
* @return The sheet in the workbook
@@ -196,12 +180,12 @@ public static Sheet create(Workbook workbook, String sheetName) {
196180
* @throws OpenWorkbookException If an error occurred while opening the workbook
197181
* @throws SheetNotFoundException If the sheet to open is not found
198182
*/
199-
public static Sheet open(File file) throws ExtensionNotValidException, IOException, OpenWorkbookException, SheetNotFoundException {
200-
return open(file, 0);
183+
public static Sheet get(File file) throws ExtensionNotValidException, IOException, OpenWorkbookException, SheetNotFoundException {
184+
return get(file, 0);
201185
}
202186

203187
/**
204-
* Opens the sheet of the Excel file
188+
* Gets the sheet of the Excel file
205189
* @param file Excel file
206190
* @param sheetName The sheet name in the workbook
207191
* @return The sheet in the workbook
@@ -210,27 +194,23 @@ public static Sheet open(File file) throws ExtensionNotValidException, IOExcepti
210194
* @throws OpenWorkbookException If an error occurred while opening the workbook
211195
* @throws SheetNotFoundException If the sheet to open is not found
212196
*/
213-
public static Sheet open(File file, String sheetName) throws ExtensionNotValidException, IOException, OpenWorkbookException, SheetNotFoundException {
214-
/* Check extension */
215-
String extension = ExcelUtility.checkExcelExtension(file.getName());
216-
197+
public static Sheet get(File file, String sheetName) throws ExtensionNotValidException, IOException, OpenWorkbookException, SheetNotFoundException {
217198
/* Open file excel */
218-
FileInputStream fileInputStream = new FileInputStream(file);
219-
Workbook workbook = WorkbookUtility.open(fileInputStream, extension);
199+
Workbook workbook = WorkbookUtility.open(file);
220200

221201
/* Open sheet */
222202
Sheet sheet = workbook.getSheet(sheetName);
223203
if (sheet == null)
224204
throw new SheetNotFoundException();
225205

226206
/* Close workbook */
227-
WorkbookUtility.close(workbook, fileInputStream);
207+
WorkbookUtility.close(workbook);
228208

229209
return sheet;
230210
}
231211

232212
/**
233-
* Opens the sheet of the Excel file
213+
* Gets the sheet of the Excel file
234214
* @param file Excel file
235215
* @param position The index in the workbook
236216
* @return The sheet in the workbook
@@ -239,44 +219,40 @@ public static Sheet open(File file, String sheetName) throws ExtensionNotValidEx
239219
* @throws OpenWorkbookException If an error occurred while opening the workbook
240220
* @throws SheetNotFoundException If the sheet to open is not found
241221
*/
242-
public static Sheet open(File file, Integer position) throws ExtensionNotValidException, IOException, OpenWorkbookException, SheetNotFoundException {
243-
/* Check extension */
244-
String extension = ExcelUtility.checkExcelExtension(file.getName());
245-
222+
public static Sheet get(File file, Integer position) throws ExtensionNotValidException, IOException, OpenWorkbookException, SheetNotFoundException {
246223
/* Open file excel */
247-
FileInputStream fileInputStream = new FileInputStream(file);
248-
Workbook workbook = WorkbookUtility.open(fileInputStream, extension);
224+
Workbook workbook = WorkbookUtility.open(file);
249225

250226
/* Open sheet */
251227
Sheet sheet = workbook.getSheetAt(position);
252228
if (sheet == null)
253229
throw new SheetNotFoundException();
254230

255231
/* Close workbook */
256-
WorkbookUtility.close(workbook, fileInputStream);
232+
WorkbookUtility.close(workbook);
257233

258234
return sheet;
259235
}
260236

261237
/**
262-
* Opens the sheet in the workbook.<p>
238+
* Gets the sheet in the workbook.<p>
263239
* If not specified, the first sheet will be opened
264240
* @param workbook The {@code Workbook} where there is the sheet
265241
* @return The sheet in the workbook in first position
266242
* @throws SheetNotFoundException If the sheet to open is not found
267243
*/
268-
public static Sheet open(Workbook workbook) throws SheetNotFoundException {
269-
return open(workbook, 0);
244+
public static Sheet get(Workbook workbook) throws SheetNotFoundException {
245+
return get(workbook, 0);
270246
}
271247

272248
/**
273-
* Opens the sheet in the workbook.
249+
* Gets the sheet in the workbook.
274250
* @param workbook The {@code Workbook} where there is the sheet
275251
* @param sheetName The sheet name in the workbook
276252
* @return The sheet in the workbook
277253
* @throws SheetNotFoundException If the sheet to open is not found
278254
*/
279-
public static Sheet open(Workbook workbook, String sheetName) throws SheetNotFoundException {
255+
public static Sheet get(Workbook workbook, String sheetName) throws SheetNotFoundException {
280256
/* Open sheet */
281257
Sheet sheet = workbook.getSheet(sheetName);
282258
if (sheet == null)
@@ -285,13 +261,13 @@ public static Sheet open(Workbook workbook, String sheetName) throws SheetNotFou
285261
}
286262

287263
/**
288-
* Opens the sheet in the workbook.
264+
* Gets the sheet in the workbook.
289265
* @param workbook The {@code Workbook} where there is the sheet
290266
* @param position The index in the workbook
291267
* @return The sheet in the workbook
292268
* @throws SheetNotFoundException If the sheet to open is not found
293269
*/
294-
public static Sheet open(Workbook workbook, Integer position) throws SheetNotFoundException {
270+
public static Sheet get(Workbook workbook, Integer position) throws SheetNotFoundException {
295271
/* Open sheet */
296272
Sheet sheet = workbook.getSheetAt(position);
297273
if (sheet == null)
@@ -300,12 +276,12 @@ public static Sheet open(Workbook workbook, Integer position) throws SheetNotFou
300276
}
301277

302278
/**
303-
* Opens the sheet in the workbook. If it doesn't find it, it creates it.
279+
* Gets the sheet in the workbook. If it doesn't find it, it creates it.
304280
* @param workbook The {@code Workbook} where there is the sheet
305281
* @param sheetName The sheet name in the workbook
306282
* @return The sheet in the workbook or a new one
307283
*/
308-
public static Sheet openOrCreate(Workbook workbook, String sheetName) {
284+
public static Sheet getOrCreate(Workbook workbook, String sheetName) {
309285
/* Open sheet */
310286
Sheet sheet = workbook.getSheet(sheetName);
311287
return sheet == null ? workbook.createSheet(sheetName) : sheet;

src/main/java/tools/WorkbookUtility.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,11 @@ public static void close(Workbook workbook, FileOutputStream fileOutputStream, F
155155
/**
156156
* Close a workbook
157157
* @param workbook The {@code Workbook} to close
158-
* @param fileInputStream The {@code FileInputStream} to close
159158
* @param writer The {@code CSVWriter} to close
160159
* @throws IOException If an I/O error has occurred
161160
*/
162-
public static void close(Workbook workbook, FileInputStream fileInputStream, CSVWriter writer) throws IOException {
161+
public static void close(Workbook workbook, CSVWriter writer) throws IOException {
163162
workbook.close();
164-
fileInputStream.close();
165163
writer.close();
166164
}
167165

0 commit comments

Comments
 (0)