Total Pageviews

2016/10/08

[Project Lombok] @Cleanup

Project Lombok provide an easy way to ensure a given resource is automatically cleaned up before the code execution path exits your current scope. All you need to do is to annotate any local variable declaration with the @Cleanup annotation.


As-Is (Do not annotate @Cleanup)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
private void readExcelTest1(String excelFile) throws IOException {
     List<Issue> issues = new ArrayList<Issue>();

     InputStream inputStream = null;
     Workbook workbook = null;

     try {
         // 1. Create a Workbook.
         inputStream = new FileInputStream(new File(excelFile));

         // 2. Get first sheet
         workbook = new HSSFWorkbook(inputStream);

         Sheet sheet = workbook.getSheetAt(0);

         // 3. Iterate through each rows from first sheet
         Iterator<Row> rowIterator = sheet.iterator();
         int rowCount = 1;
         while (rowIterator.hasNext()) {
            // (1) ignore header row
            if (rowCount == 1) {
              rowIterator.next();
              rowCount++;
             }
             // (2) start to read each row from second row
             else {
                 Row row = rowIterator.next();
                 Integer id = Double.valueOf(row.getCell(0).getNumericCellValue()).intValue();
                 String subject = row.getCell(1).getStringCellValue();
                 String status = row.getCell(2).getStringCellValue();
                 String priority = row.getCell(3).getStringCellValue();
                 String notes = row.getCell(4).getStringCellValue();

                 Issue issue = new Issue();
                 issue.setId(id);
                 issue.setSubject(subject);
                 issue.setStatus(status);
                 issue.setPriority(priority);
                 issue.setNotes(notes);

                 issues.add(issue);
              }
           }

          // print collection
          issues.stream().forEach(issue -> log.debug(issue.toString()));
     } catch (FileNotFoundException e) {
         throw e;
     } catch (IOException e) {
         throw e;
     } finally {
         if (workbook != null) {
           workbook.close();
         }

         if (inputStream != null) {
           inputStream.close();
         }
     }
}



To-Be(Annotte with @Cleanup)
It will ensure the variable declaration that you annotate will be cleaned up by calling its close method, regardless of what happens. Implemented by wrapping all statements following the local variable declaration to the end of your scope into a try block that, as a finally action, closes the resource. 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
 private void readExcelTest2(String excelFile) throws Exception {
     try {
         // 1. Create a Workbook.
         @Cleanup
         InputStream inputStream = new FileInputStream(new File(excelFile));

         // 2. Get first sheet
         @Cleanup
         Workbook workbook = new HSSFWorkbook(inputStream);

         List<Issue> issues = new ArrayList<Issue>();

         Sheet sheet = workbook.getSheetAt(0);
 
         // 3. Iterate through each rows from first sheet
         Iterator<Row> rowIterator = sheet.iterator();
         int rowCount = 1;
         while (rowIterator.hasNext()) {
              // (1) ignore header row
              if (rowCount == 1) {
              rowIterator.next();
              rowCount++;
              }
              // (2) start to read each row from second row
              else {
                  Row row = rowIterator.next();
                  Integer id = Double.valueOf(row.getCell(0).getNumericCellValue()).intValue();
                  String subject = row.getCell(1).getStringCellValue();
                  String status = row.getCell(2).getStringCellValue();
                  String priority = row.getCell(3).getStringCellValue();
                  String notes = row.getCell(4).getStringCellValue();

                  Issue issue = new Issue();
                  issue.setId(id);
                  issue.setSubject(subject);
                  issue.setStatus(status);
                  issue.setPriority(priority);
                  issue.setNotes(notes);
    
                  issues.add(issue);
              }
         }

         // print collection
         issues.stream().forEach(issue -> log.debug(issue.toString()));
    
     } catch (FileNotFoundException e) {
         throw e;
     } catch (Exception e) {
         throw e;
     }
 }



Reference
[1] https://projectlombok.org/features/Cleanup.html

No comments: