View Javadoc
1   package de.aikiit.bilanzanalyser.reader;
2   
3   import de.aikiit.bilanzanalyser.entity.BilanzRow;
4   
5   import java.util.List;
6   
7   /**
8    * Result of parsing a spreadsheet file with
9    *
10   * @param errorCount error row count.
11   * @param rowCount   row count equals the number seen, not parsed correctly.
12   * @param rows       extracted and parsed rows. May be less than rowCount.
13   */
14  public record BilanzRowParserResult(int errorCount, int rowCount, List<BilanzRow> rows) {
15  
16      public BilanzRowParserResult {
17          rows = rows == null ? List.of() : List.copyOf(rows);
18      }
19  
20      public static BilanzRowParserResult empty() {
21          return new BilanzRowParserResult(0, 0, List.of());
22      }
23  
24      /**
25       * Check whether this result is empty.
26       *
27       * @return {@code true} if no rows and no errors are found yet.
28       */
29      public boolean isEmpty() {
30          return rows == null || rows.isEmpty() || errorCount == 0;
31      }
32  
33      /**
34       * Increment errorCount.
35       *
36       * @return copy with incremented value.
37       */
38      public BilanzRowParserResult withError() {
39          return new BilanzRowParserResult(errorCount + 1, rowCount, rows);
40      }
41  
42      /**
43       * Increment rowCount.
44       *
45       * @return copy with incremented value.
46       */
47      public BilanzRowParserResult withRow() {
48          return new BilanzRowParserResult(errorCount, rowCount + 1, rows);
49      }
50  
51      /**
52       * Allows adding a list of rows.
53       *
54       * @param newRows a list of rows to replace any existing rows.
55       * @return copy with added row.
56       */
57      public BilanzRowParserResult withRows(List<BilanzRow> newRows) {
58          return new BilanzRowParserResult(errorCount, rowCount, newRows);
59      }
60  
61      /**
62       * Allows adding a single row incrementally.
63       *
64       * @param row a single row.
65       * @return copy with added row.
66       */
67      public BilanzRowParserResult addRow(BilanzRow row) {
68          var newList = new java.util.ArrayList<>(rows);
69          newList.add(row);
70          return new BilanzRowParserResult(errorCount, rowCount, newList);
71      }
72  }