From 9268ef0edd23474f5677a470d907b6f64cdca2b7 Mon Sep 17 00:00:00 2001 From: Vitalii Ananev Date: Thu, 17 Feb 2022 01:52:55 +0300 Subject: [PATCH 01/10] replace ReportPage.find(String) by ReportPage.findByPrefix() method --- .../table_wrapper/api/AbstractTable.java | 10 ++ .../table_wrapper/api/MutableTableRow.java | 4 +- .../table_wrapper/api/ReportPage.java | 109 ++++++++++++++---- .../table_wrapper/api/ReportPageHelper.java | 29 +++++ .../table_wrapper/api/ReportPageRow.java | 6 +- .../table_wrapper/api/Table.java | 7 +- 6 files changed, 136 insertions(+), 29 deletions(-) create mode 100644 src/main/java/org/spacious_team/table_wrapper/api/ReportPageHelper.java diff --git a/src/main/java/org/spacious_team/table_wrapper/api/AbstractTable.java b/src/main/java/org/spacious_team/table_wrapper/api/AbstractTable.java index de00cc3..e6ed686 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/AbstractTable.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/AbstractTable.java @@ -231,6 +231,16 @@ public TableRow next() { @Override public TableRow findRow(Object value) { TableCellAddress address = reportPage.find(value); + return getMutableTableRow(address); + } + + @Override + public TableRow findRowByPrefix(String prefix) { + TableCellAddress address = reportPage.findByPrefix(prefix); + return getMutableTableRow(address); + } + + private MutableTableRow getMutableTableRow(TableCellAddress address) { if (tableRange.contains(address)) { MutableTableRow tableRow = new MutableTableRow<>(this, getCellDataAccessObject()); tableRow.setRow(reportPage.getRow(address.getRow())); diff --git a/src/main/java/org/spacious_team/table_wrapper/api/MutableTableRow.java b/src/main/java/org/spacious_team/table_wrapper/api/MutableTableRow.java index 5c8f345..d5dc9e3 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/MutableTableRow.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/MutableTableRow.java @@ -65,8 +65,8 @@ public int getLastCellNum() { } @Override - public boolean rowContains(Object value) { - return row.rowContains(value); + public boolean rowContains(Object expected) { + return row.rowContains(expected); } @Override diff --git a/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java b/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java index 2453f68..1d525be 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java @@ -18,64 +18,118 @@ package org.spacious_team.table_wrapper.api; +import java.util.Objects; import java.util.function.BiPredicate; public interface ReportPage { - BiPredicate CELL_STRING_STARTS_WITH = (cell, searchingValue) -> - searchingValue != null && cell.trim().toLowerCase().startsWith(searchingValue.toString().trim().toLowerCase()); - /** - * @return table table cell address or {@link TableCellAddress#NOT_FOUND} + * Finds cell address containing exact value. + * + * @return cell address or {@link TableCellAddress#NOT_FOUND} */ default TableCellAddress find(Object value) { return find(value, 0); } /** - * @return table table cell address or {@link TableCellAddress#NOT_FOUND} + * Finds cell address containing exact value. + * + * @param startRow search rows start from this + * @return cell address or {@link TableCellAddress#NOT_FOUND} */ default TableCellAddress find(Object value, int startRow) { return find(value, startRow, Integer.MAX_VALUE); } /** + * Finds cell address containing exact value. + * * @param startRow search rows start from this * @param endRow search rows excluding this, can handle values greater than real rows count - * @return table table cell address or {@link TableCellAddress#NOT_FOUND} + * @return cell address or {@link TableCellAddress#NOT_FOUND} */ default TableCellAddress find(Object value, int startRow, int endRow) { - return find(value, startRow, endRow, ReportPage.CELL_STRING_STARTS_WITH); + return find(value, startRow, endRow, (cell, _value) -> + cell == _value || (_value != null && Objects.equals(cell, _value.toString()))); + } + + /** + * @param startRow search rows start from this + * @param endRow search rows excluding this, can handle values greater than real rows count + * @param stringCellPredicate predicate for testing string containing cell with 'value' arg + * @return cell address or {@link TableCellAddress#NOT_FOUND} + */ + default TableCellAddress find(Object value, int startRow, int endRow, BiPredicate stringCellPredicate) { + return find(value, startRow, endRow, 0, Integer.MAX_VALUE, stringCellPredicate); + } + + /** + * @param value searching value + * @param startRow search rows start from this + * @param endRow search rows excluding this, can handle values greater than real rows count + * @param startColumn search columns start from this + * @param endColumn search columns excluding this, can handle values greater than real columns count + * @param stringCellPredicate predicate for testing string containing cell with 'value' arg + * @return cell address or {@link TableCellAddress#NOT_FOUND} + */ + TableCellAddress find(Object value, int startRow, int endRow, + int startColumn, int endColumn, + BiPredicate stringCellPredicate); + + /** + * Finds cell address staring with value (ignore case, trims leading spaces). + * + * @return cell address or {@link TableCellAddress#NOT_FOUND} + */ + default TableCellAddress findByPrefix(String prefix) { + return findByPrefix(prefix, 0); } /** - * @param startRow search rows start from this - * @param endRow search rows excluding this, can handle values greater than real rows count - * @param stringPredicate cell and value comparing bi-predicate if cell value type is string - * @return table table cell address or {@link TableCellAddress#NOT_FOUND} + * Finds cell address staring with value (ignore case, trims leading spaces). + * + * @param startRow search rows start from this + * @return cell address or {@link TableCellAddress#NOT_FOUND} + */ + default TableCellAddress findByPrefix(String prefix, int startRow) { + return findByPrefix(prefix, startRow, Integer.MAX_VALUE); + } + + /** + * Finds cell address staring with value (ignore case, trims leading spaces). + * + * @param startRow search rows start from this + * @param endRow search rows excluding this, can handle values greater than real rows count + * @return cell address or {@link TableCellAddress#NOT_FOUND} */ - default TableCellAddress find(Object value, int startRow, int endRow, BiPredicate stringPredicate) { - return find(value, startRow, endRow, 0, Integer.MAX_VALUE, stringPredicate); + default TableCellAddress findByPrefix(String prefix, int startRow, int endRow) { + return findByPrefix(prefix, startRow, endRow, 0, Integer.MAX_VALUE); } /** - * @param value searching value + * Finds cell address staring with value (ignore case, trims leading spaces). + * * @param startRow search rows start from this * @param endRow search rows excluding this, can handle values greater than real rows count * @param startColumn search columns start from this * @param endColumn search columns excluding this, can handle values greater than real columns count - * @return table table cell address or {@link TableCellAddress#NOT_FOUND} + * @return cell address or {@link TableCellAddress#NOT_FOUND} */ - TableCellAddress find(Object value, int startRow, int endRow, - int startColumn, int endColumn, - BiPredicate stringPredicate); - + default TableCellAddress findByPrefix(String prefix, int startRow, int endRow, int startColumn, int endColumn) { + if (prefix != null) { + return find(prefix.trim().toLowerCase(), + startRow, endRow, startColumn, endColumn, + ReportPageHelper.CELL_STARTS_WITH_IGNORE_CASE); + } + return TableCellAddress.NOT_FOUND; + } /** * For vertical table of key-value records (table with two columns), search and return value for requested key. */ - default Object getNextColumnValue(String firstColumnValue) { - TableCellAddress address = find(firstColumnValue); + default Object getNextColumnValue(String firstColumnValuePrefix) { + TableCellAddress address = findByPrefix(firstColumnValuePrefix); for (TableCell cell : getRow(address.getRow())) { if (cell != null && cell.getColumnIndex() > address.getColumn()) { Object value = cell.getValue(); @@ -106,12 +160,17 @@ default TableCell getCell(TableCellAddress address) { * Returns table range, table ends with predefined string in one of the row cells. */ default TableCellRange getTableCellRange(String tableName, int headersRowCount, String tableFooterString) { - TableCellAddress startAddress = find(tableName); + if (tableFooterString == null) { + return TableCellRange.EMPTY_RANGE; + } + TableCellAddress startAddress = findByPrefix(tableName); if (startAddress.equals(TableCellAddress.NOT_FOUND)) { return TableCellRange.EMPTY_RANGE; } - TableCellAddress endAddress = find(tableFooterString, startAddress.getRow() + headersRowCount + 1, - getLastRowNum(), ReportPage.CELL_STRING_STARTS_WITH); + TableCellAddress endAddress = findByPrefix( + tableFooterString, + startAddress.getRow() + headersRowCount + 1, + getLastRowNum()); if (endAddress.equals(TableCellAddress.NOT_FOUND)) { return TableCellRange.EMPTY_RANGE; } @@ -126,7 +185,7 @@ default TableCellRange getTableCellRange(String tableName, int headersRowCount, * Returns table range, table ends with empty row or last row of report page. */ default TableCellRange getTableCellRange(String tableName, int headersRowCount) { - TableCellAddress startAddress = find(tableName); + TableCellAddress startAddress = findByPrefix(tableName); if (startAddress.equals(TableCellAddress.NOT_FOUND)) { return TableCellRange.EMPTY_RANGE; } diff --git a/src/main/java/org/spacious_team/table_wrapper/api/ReportPageHelper.java b/src/main/java/org/spacious_team/table_wrapper/api/ReportPageHelper.java new file mode 100644 index 0000000..cf34bc7 --- /dev/null +++ b/src/main/java/org/spacious_team/table_wrapper/api/ReportPageHelper.java @@ -0,0 +1,29 @@ +/* + * Table Wrapper API + * Copyright (C) 2022 Vitalii Ananev + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package org.spacious_team.table_wrapper.api; + +import java.util.function.BiPredicate; + +class ReportPageHelper { + + static final BiPredicate CELL_STARTS_WITH_IGNORE_CASE = (cell, lowercasePrefix) -> + cell == lowercasePrefix || + (lowercasePrefix != null && cell.trim().toLowerCase().startsWith(lowercasePrefix.toString())); + +} diff --git a/src/main/java/org/spacious_team/table_wrapper/api/ReportPageRow.java b/src/main/java/org/spacious_team/table_wrapper/api/ReportPageRow.java index 3b713a4..9320901 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/ReportPageRow.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/ReportPageRow.java @@ -41,5 +41,9 @@ public interface ReportPageRow extends Iterable { */ int getLastCellNum(); - boolean rowContains(Object value); + /** + * @param expected searching value + * @return true if any cell of this row has exact value, false otherwise + */ + boolean rowContains(Object expected); } \ No newline at end of file diff --git a/src/main/java/org/spacious_team/table_wrapper/api/Table.java b/src/main/java/org/spacious_team/table_wrapper/api/Table.java index 5f81543..204c5ea 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/Table.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/Table.java @@ -58,10 +58,15 @@ List getDataCollection(Object report, Function> r Stream stream(); /** - * @return row containing given value or null if not found + * @return row containing cell with exact value or null if not found */ TableRow findRow(Object value); + /** + * @return row containing cell starting with prefix or null if not found + */ + TableRow findRowByPrefix(String prefix); + Map getHeaderDescription(); /** From 63a1d542617fd948734abe4c3f59156a01434f51 Mon Sep 17 00:00:00 2001 From: Vitalii Ananev Date: Sat, 19 Feb 2022 14:40:54 +0300 Subject: [PATCH 02/10] change ReportPage.find() method signature --- .../table_wrapper/api/ReportPage.java | 52 +++++++++---------- .../table_wrapper/api/ReportPageHelper.java | 29 ----------- 2 files changed, 26 insertions(+), 55 deletions(-) delete mode 100644 src/main/java/org/spacious_team/table_wrapper/api/ReportPageHelper.java diff --git a/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java b/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java index 1d525be..31728c0 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java @@ -18,8 +18,7 @@ package org.spacious_team.table_wrapper.api; -import java.util.Objects; -import java.util.function.BiPredicate; +import java.util.function.Predicate; public interface ReportPage { @@ -50,32 +49,30 @@ default TableCellAddress find(Object value, int startRow) { * @return cell address or {@link TableCellAddress#NOT_FOUND} */ default TableCellAddress find(Object value, int startRow, int endRow) { - return find(value, startRow, endRow, (cell, _value) -> - cell == _value || (_value != null && Objects.equals(cell, _value.toString()))); + return find(value, startRow, endRow, 0, Integer.MAX_VALUE); } /** - * @param startRow search rows start from this - * @param endRow search rows excluding this, can handle values greater than real rows count - * @param stringCellPredicate predicate for testing string containing cell with 'value' arg + * @param value searching value + * @param startRow search rows start from this + * @param endRow search rows excluding this, can handle values greater than real rows count + * @param startColumn search columns start from this + * @param endColumn search columns excluding this, can handle values greater than real columns count * @return cell address or {@link TableCellAddress#NOT_FOUND} */ - default TableCellAddress find(Object value, int startRow, int endRow, BiPredicate stringCellPredicate) { - return find(value, startRow, endRow, 0, Integer.MAX_VALUE, stringCellPredicate); - } + TableCellAddress find(Object value, int startRow, int endRow, int startColumn, int endColumn); /** - * @param value searching value - * @param startRow search rows start from this - * @param endRow search rows excluding this, can handle values greater than real rows count - * @param startColumn search columns start from this - * @param endColumn search columns excluding this, can handle values greater than real columns count - * @param stringCellPredicate predicate for testing string containing cell with 'value' arg + * @param startRow search rows start from this + * @param endRow search rows excluding this, can handle values greater than real rows count + * @param startColumn search columns start from this + * @param endColumn search columns excluding this, can handle values greater than real columns count + * @param cellValuePredicate predicate for testing cell value * @return cell address or {@link TableCellAddress#NOT_FOUND} */ - TableCellAddress find(Object value, int startRow, int endRow, + TableCellAddress find(int startRow, int endRow, int startColumn, int endColumn, - BiPredicate stringCellPredicate); + Predicate cellValuePredicate); /** * Finds cell address staring with value (ignore case, trims leading spaces). @@ -118,9 +115,10 @@ default TableCellAddress findByPrefix(String prefix, int startRow, int endRow) { */ default TableCellAddress findByPrefix(String prefix, int startRow, int endRow, int startColumn, int endColumn) { if (prefix != null) { - return find(prefix.trim().toLowerCase(), - startRow, endRow, startColumn, endColumn, - ReportPageHelper.CELL_STARTS_WITH_IGNORE_CASE); + String lowercasePrefix = prefix.trim().toLowerCase(); + Predicate cellPredicate = (cell) -> (cell instanceof String) && + ((String) cell).trim().toLowerCase().startsWith(lowercasePrefix); + return find(startRow, endRow, startColumn, endColumn, cellPredicate); } return TableCellAddress.NOT_FOUND; } @@ -157,10 +155,11 @@ default TableCell getCell(TableCellAddress address) { } /** - * Returns table range, table ends with predefined string in one of the row cells. + * Returns table range. Table's first row starts with 'tableName' prefix in one of the cells + * and table ends with predefined prefix in one of the last row cells. */ - default TableCellRange getTableCellRange(String tableName, int headersRowCount, String tableFooterString) { - if (tableFooterString == null) { + default TableCellRange getTableCellRange(String tableName, int headersRowCount, String tableFooterPrefix) { + if (tableFooterPrefix == null) { return TableCellRange.EMPTY_RANGE; } TableCellAddress startAddress = findByPrefix(tableName); @@ -168,7 +167,7 @@ default TableCellRange getTableCellRange(String tableName, int headersRowCount, return TableCellRange.EMPTY_RANGE; } TableCellAddress endAddress = findByPrefix( - tableFooterString, + tableFooterPrefix, startAddress.getRow() + headersRowCount + 1, getLastRowNum()); if (endAddress.equals(TableCellAddress.NOT_FOUND)) { @@ -182,7 +181,8 @@ default TableCellRange getTableCellRange(String tableName, int headersRowCount, } /** - * Returns table range, table ends with empty row or last row of report page. + * Returns table range. Table's first row starts with 'tableName' prefix in one of the cells + * and ends with empty row or last row of report page. */ default TableCellRange getTableCellRange(String tableName, int headersRowCount) { TableCellAddress startAddress = findByPrefix(tableName); diff --git a/src/main/java/org/spacious_team/table_wrapper/api/ReportPageHelper.java b/src/main/java/org/spacious_team/table_wrapper/api/ReportPageHelper.java deleted file mode 100644 index cf34bc7..0000000 --- a/src/main/java/org/spacious_team/table_wrapper/api/ReportPageHelper.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Table Wrapper API - * Copyright (C) 2022 Vitalii Ananev - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package org.spacious_team.table_wrapper.api; - -import java.util.function.BiPredicate; - -class ReportPageHelper { - - static final BiPredicate CELL_STARTS_WITH_IGNORE_CASE = (cell, lowercasePrefix) -> - cell == lowercasePrefix || - (lowercasePrefix != null && cell.trim().toLowerCase().startsWith(lowercasePrefix.toString())); - -} From 6840da26ed5c547de8d4db5646deb1767f667c33 Mon Sep 17 00:00:00 2001 From: Vitalii Ananev Date: Sat, 19 Feb 2022 18:59:22 +0300 Subject: [PATCH 03/10] add some ReportPage.find() methods with predicate --- .../table_wrapper/api/ReportPage.java | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java b/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java index 31728c0..7dc3870 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java @@ -53,6 +53,8 @@ default TableCellAddress find(Object value, int startRow, int endRow) { } /** + * Finds cell address containing exact value. + * * @param value searching value * @param startRow search rows start from this * @param endRow search rows excluding this, can handle values greater than real rows count @@ -63,6 +65,39 @@ default TableCellAddress find(Object value, int startRow, int endRow) { TableCellAddress find(Object value, int startRow, int endRow, int startColumn, int endColumn); /** + * Finds cell by predicate. + * + * @param cellValuePredicate predicate for testing cell value + * @return cell address or {@link TableCellAddress#NOT_FOUND} + */ + default TableCellAddress find(Predicate cellValuePredicate) { + return find(0, cellValuePredicate); + } + + /** + * Finds cell by predicate. + * + * @param startRow search rows start from this + * @return cell address or {@link TableCellAddress#NOT_FOUND} + */ + default TableCellAddress find(int startRow, Predicate cellValuePredicate) { + return find(startRow, Integer.MAX_VALUE, cellValuePredicate); + } + + /** + * Finds cell by predicate. + * + * @param startRow search rows start from this + * @param endRow search rows excluding this, can handle values greater than real rows count + * @return cell address or {@link TableCellAddress#NOT_FOUND} + */ + default TableCellAddress find(int startRow, int endRow, Predicate cellValuePredicate) { + return find(startRow, endRow, 0, Integer.MAX_VALUE, cellValuePredicate); + } + + /** + * Finds cell by predicate. + * * @param startRow search rows start from this * @param endRow search rows excluding this, can handle values greater than real rows count * @param startColumn search columns start from this @@ -117,7 +152,7 @@ default TableCellAddress findByPrefix(String prefix, int startRow, int endRow, i if (prefix != null) { String lowercasePrefix = prefix.trim().toLowerCase(); Predicate cellPredicate = (cell) -> (cell instanceof String) && - ((String) cell).trim().toLowerCase().startsWith(lowercasePrefix); + ((String) cell).trim().toLowerCase().startsWith(lowercasePrefix); return find(startRow, endRow, startColumn, endColumn, cellPredicate); } return TableCellAddress.NOT_FOUND; From 6f4d963f4e57c1d5b6628da8ce920a6db3455456 Mon Sep 17 00:00:00 2001 From: Vitalii Ananev Date: Sat, 19 Feb 2022 19:10:19 +0300 Subject: [PATCH 04/10] refactor methods position --- .../table_wrapper/api/ReportPage.java | 12 +++++------ .../table_wrapper/api/TableFactory.java | 20 +++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java b/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java index 7dc3870..7c16a61 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java @@ -304,6 +304,12 @@ default Table createNameless(String firstLineText, .createNameless(this, firstLineText, lastRowString, headerDescription); } + default Table createNameless(String firstLineText, + Class headerDescription) { + return TableFactoryRegistry.get(this) + .createNameless(this, firstLineText, headerDescription); + } + default Table createNameless(String providedTableName, String firstLineText, String lastRowString, @@ -313,12 +319,6 @@ default Table createNameless(String providedTableName, .createNameless(this, providedTableName, firstLineText, lastRowString, headerDescription, headersRowCount); } - default Table createNameless(String firstLineText, - Class headerDescription) { - return TableFactoryRegistry.get(this) - .createNameless(this, firstLineText, headerDescription); - } - default Table createNameless(String providedTableName, String firstLineText, Class headerDescription, diff --git a/src/main/java/org/spacious_team/table_wrapper/api/TableFactory.java b/src/main/java/org/spacious_team/table_wrapper/api/TableFactory.java index d1247f6..894a942 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/TableFactory.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/TableFactory.java @@ -79,6 +79,16 @@ default Table createNameless(ReportPage reportPage, return createNameless(reportPage, "undefined", firstRowString, lastRowString, headerDescription, 1); } + /** + * Creates table without name which starts with header and ends with empty row or last row of report page. + * @param firstRowString table first row contains cell which starts with given text + */ + default Table createNameless(ReportPage reportPage, + String firstRowString, + Class headerDescription) { + return createNameless(reportPage, "undefined", firstRowString, headerDescription, 1); + } + /** * Creates table without name which starts with header and ends with row containing cell with text starting with * given string. @@ -93,16 +103,6 @@ Table createNameless(ReportPage reportPage, Class headerDescription, int headersRowCount); - /** - * Creates table without name which starts with header and ends with empty row or last row of report page. - * @param firstRowString table first row contains cell which starts with given text - */ - default Table createNameless(ReportPage reportPage, - String firstRowString, - Class headerDescription) { - return createNameless(reportPage, "undefined", firstRowString, headerDescription, 1); - } - /** * Creates table without name which starts with header and ends with empty row or last row of report page. * @param providedTableName predefined (not existing in reportPage) table name From e55ef0e433c0b348d518d89573ee3a094d5bc75a Mon Sep 17 00:00:00 2001 From: Vitalii Ananev Date: Sat, 19 Feb 2022 19:56:31 +0300 Subject: [PATCH 05/10] add methods for finding table with predicate --- .../table_wrapper/api/ReportPage.java | 133 +++++++++++++++-- .../table_wrapper/api/TableFactory.java | 140 ++++++++++++++++-- 2 files changed, 246 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java b/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java index 7c16a61..713878c 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java @@ -190,21 +190,18 @@ default TableCell getCell(TableCellAddress address) { } /** - * Returns table range. Table's first row starts with 'tableName' prefix in one of the cells + * Returns table range. Table's first row starts with 'firstRowPrefix' prefix in one of the cells * and table ends with predefined prefix in one of the last row cells. */ - default TableCellRange getTableCellRange(String tableName, int headersRowCount, String tableFooterPrefix) { - if (tableFooterPrefix == null) { + default TableCellRange getTableCellRange(String firstRowPrefix, int headersRowCount, String lastRowPrefix) { + if (firstRowPrefix == null || lastRowPrefix == null) { return TableCellRange.EMPTY_RANGE; } - TableCellAddress startAddress = findByPrefix(tableName); + TableCellAddress startAddress = findByPrefix(firstRowPrefix); if (startAddress.equals(TableCellAddress.NOT_FOUND)) { return TableCellRange.EMPTY_RANGE; } - TableCellAddress endAddress = findByPrefix( - tableFooterPrefix, - startAddress.getRow() + headersRowCount + 1, - getLastRowNum()); + TableCellAddress endAddress = findByPrefix(lastRowPrefix, startAddress.getRow() + headersRowCount + 1); if (endAddress.equals(TableCellAddress.NOT_FOUND)) { return TableCellRange.EMPTY_RANGE; } @@ -216,11 +213,65 @@ default TableCellRange getTableCellRange(String tableName, int headersRowCount, } /** - * Returns table range. Table's first row starts with 'tableName' prefix in one of the cells - * and ends with empty row or last row of report page. + * Returns table range. First and last row will be found by predicate. */ - default TableCellRange getTableCellRange(String tableName, int headersRowCount) { - TableCellAddress startAddress = findByPrefix(tableName); + default TableCellRange getTableCellRange(Predicate firstRowFinder, + int headersRowCount, + Predicate lastRowFinder) { + if (firstRowFinder == null || lastRowFinder == null) { + return TableCellRange.EMPTY_RANGE; + } + TableCellAddress startAddress = find(firstRowFinder); + if (startAddress.equals(TableCellAddress.NOT_FOUND)) { + return TableCellRange.EMPTY_RANGE; + } + TableCellAddress endAddress = find(startAddress.getRow() + headersRowCount + 1, lastRowFinder); + if (endAddress.equals(TableCellAddress.NOT_FOUND)) { + return TableCellRange.EMPTY_RANGE; + } + return new TableCellRange( + startAddress.getRow(), + endAddress.getRow(), + getRow(startAddress.getRow()).getFirstCellNum(), + getRow(endAddress.getRow()).getLastCellNum()); + } + + /** + * Returns table range. First row starts with 'firstRowPrefix' prefix in one of the cells, + * range ends with empty row or last row of report page. + */ + default TableCellRange getTableCellRange(String firstRowPrefix, int headersRowCount) { + if (firstRowPrefix == null) { + return TableCellRange.EMPTY_RANGE; + } + TableCellAddress startAddress = findByPrefix(firstRowPrefix); + if (startAddress.equals(TableCellAddress.NOT_FOUND)) { + return TableCellRange.EMPTY_RANGE; + } + int lastRowNum = findEmptyRow(startAddress.getRow() + headersRowCount + 1); + if (lastRowNum == -1) { + lastRowNum = getLastRowNum(); // empty row not found + } else if (lastRowNum <= getLastRowNum()) { + lastRowNum--; // exclude last row from table + } + if (lastRowNum < startAddress.getRow()) { + lastRowNum = startAddress.getRow(); + } + return new TableCellRange( + startAddress.getRow(), + lastRowNum, + getRow(startAddress.getRow()).getFirstCellNum(), + getRow(lastRowNum).getLastCellNum()); + } + + /** + * Returns table range. First row will be found by predicate, range ends with empty row or last row of report page. + */ + default TableCellRange getTableCellRange(Predicate firstRowFinder, int headersRowCount) { + if (firstRowFinder == null) { + return TableCellRange.EMPTY_RANGE; + } + TableCellAddress startAddress = find(firstRowFinder); if (startAddress.equals(TableCellAddress.NOT_FOUND)) { return TableCellRange.EMPTY_RANGE; } @@ -297,6 +348,34 @@ default Table create(String tableName, .create(this, tableName, headerDescription, headersRowCount); } + default Table create(Predicate tableNameFinder, + Predicate tableFooterFinder, + Class headerDescription) { + return TableFactoryRegistry.get(this) + .create(this, tableNameFinder, tableFooterFinder, headerDescription); + } + + default Table create(Predicate tableNameFinder, + Class headerDescription) { + return TableFactoryRegistry.get(this) + .create(this, tableNameFinder, headerDescription); + } + + default Table create(Predicate tableNameFinder, + Predicate tableFooterFinder, + Class headerDescription, + int headersRowCount) { + return TableFactoryRegistry.get(this) + .create(this, tableNameFinder, tableFooterFinder, headerDescription, headersRowCount); + } + + default Table create(Predicate tableNameFinder, + Class headerDescription, + int headersRowCount) { + return TableFactoryRegistry.get(this) + .create(this, tableNameFinder, headerDescription, headersRowCount); + } + default Table createNameless(String firstLineText, String lastRowString, Class headerDescription) { @@ -326,4 +405,34 @@ default Table createNameless(String providedTableName, return TableFactoryRegistry.get(this) .createNameless(this, providedTableName, firstLineText, headerDescription, headersRowCount); } + + default Table createNameless(Predicate firstLineFinder, + Predicate lastRowFinder, + Class headerDescription) { + return TableFactoryRegistry.get(this) + .createNameless(this, firstLineFinder, lastRowFinder, headerDescription); + } + + default Table createNameless(Predicate firstLineFinder, + Class headerDescription) { + return TableFactoryRegistry.get(this) + .createNameless(this, firstLineFinder, headerDescription); + } + + default Table createNameless(String providedTableName, + Predicate firstLineFinder, + Predicate lastRowFinder, + Class headerDescription, + int headersRowCount) { + return TableFactoryRegistry.get(this) + .createNameless(this, providedTableName, firstLineFinder, lastRowFinder, headerDescription, headersRowCount); + } + + default Table createNameless(String providedTableName, + Predicate firstLineFinder, + Class headerDescription, + int headersRowCount) { + return TableFactoryRegistry.get(this) + .createNameless(this, providedTableName, firstLineFinder, headerDescription, headersRowCount); + } } diff --git a/src/main/java/org/spacious_team/table_wrapper/api/TableFactory.java b/src/main/java/org/spacious_team/table_wrapper/api/TableFactory.java index 894a942..90a3b09 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/TableFactory.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/TableFactory.java @@ -18,6 +18,8 @@ package org.spacious_team.table_wrapper.api; +import java.util.function.Predicate; + public interface TableFactory { boolean canHandle(ReportPage reportPage); @@ -25,8 +27,9 @@ public interface TableFactory { /** * Creates table which starts with name followed by header and ends with row containing cell with text starting with * given string. - * @param tableName table name's row contains cell which starts with given text - * @param lastRowString table last row contains cell which starts with given text + * + * @param tableName table name's row should contain cell which starts with given text + * @param lastRowString table's last row should contain cell which starts with given text */ default Table create(ReportPage reportPage, String tableName, @@ -37,7 +40,8 @@ default Table create(ReportPage reportPage, /** * Creates table which starts with name followed by header and ends with empty row or last row of report page. - * @param tableName table name's row contains cell which starts with given text + * + * @param tableName table name's row should contain cell which starts with given text */ default Table create(ReportPage reportPage, String tableName, @@ -48,8 +52,9 @@ default Table create(ReportPage reportPage, /** * Creates table which starts with name followed by header and ends with row containing cell with text starting with * given string. - * @param tableName table name's row contains cell which starts with given text - * @param lastRowString table last row contains cell which starts with given text + * + * @param tableName table name's row should contain cell which starts with given text + * @param lastRowString table's last row should contain cell which starts with given text */ Table create(ReportPage reportPage, String tableName, @@ -59,18 +64,68 @@ Table create(ReportPage reportPage, /** * Creates table which starts with name followed by header and ends with empty row or last row of report page. - * @param tableName table name's row contains cell which starts with given text + * + * @param tableName table name's row should contain cell which starts with given text */ Table create(ReportPage reportPage, String tableName, Class headerDescription, int headersRowCount); + /** + * Creates table. Table name containing row and last row will be found by predicate. + * + * @param tableNameFinder table name containing row should contain cell satisfying predicate + * @param lastRowFinder table's last row should contain cell satisfying predicate + */ + default Table create(ReportPage reportPage, + Predicate tableNameFinder, + Predicate lastRowFinder, + Class headerDescription) { + return create(reportPage, tableNameFinder, lastRowFinder, headerDescription, 1); + } + + /** + * Creates table. Table name containing row will be found by predicate, table ends by empty row + * or last row of report page. + * + * @param tableNameFinder table name containing row should contain cell satisfying predicate + */ + default Table create(ReportPage reportPage, + Predicate tableNameFinder, + Class headerDescription) { + return create(reportPage, tableNameFinder, headerDescription, 1); + } + + /** + * Creates table. Table name containing row and last row will be found by predicate. + * + * @param tableNameFinder table name containing row should contain cell satisfying predicate + * @param lastRowFinder table's last row should contain cell satisfying predicate + */ + Table create(ReportPage reportPage, + Predicate tableNameFinder, + Predicate lastRowFinder, + Class headerDescription, + int headersRowCount); + + /** + * Creates table. Table name containing row will be found by predicate, table ends by empty row + * or last row of report page. + * + * @param tableNameFinder table name containing row should contain cell satisfying predicate + */ + Table create(ReportPage reportPage, + Predicate tableNameFinder, + Class headerDescription, + int headersRowCount); + /** * Creates table without name which starts with header and ends with row containing cell with text starting with * given string. - * @param firstRowString table first row contains cell which starts with given text - * @param lastRowString table last row contains cell which starts with given text + * + * @param firstRowString table first row should contain cell which starts with given text + * @param lastRowString table's last row should contain cell which starts with given text */ default Table createNameless(ReportPage reportPage, String firstRowString, @@ -81,7 +136,8 @@ default Table createNameless(ReportPage reportPage, /** * Creates table without name which starts with header and ends with empty row or last row of report page. - * @param firstRowString table first row contains cell which starts with given text + * + * @param firstRowString table first row should contain cell which starts with given text */ default Table createNameless(ReportPage reportPage, String firstRowString, @@ -90,11 +146,12 @@ default Table createNameless(ReportPage reportPage, } /** - * Creates table without name which starts with header and ends with row containing cell with text starting with - * given string. + * Creates table with predefined name which starts with header and ends with row containing cell with text starting + * with given string. + * * @param providedTableName predefined (not existing in reportPage) table name - * @param firstRowString table first row contains cell which starts with given text - * @param lastRowString table last row contains cell which starts with given text + * @param firstRowString table first row should contain cell which starts with given text + * @param lastRowString table's last row should contain cell which starts with given text */ Table createNameless(ReportPage reportPage, String providedTableName, @@ -104,13 +161,66 @@ Table createNameless(ReportPage reportPage, int headersRowCount); /** - * Creates table without name which starts with header and ends with empty row or last row of report page. + * Creates table with predefined name which starts with header and ends with empty row or last row of report page. + * * @param providedTableName predefined (not existing in reportPage) table name - * @param firstRowString table first row contains cell which starts with given text + * @param firstRowString table first row should contain cell which starts with given text */ Table createNameless(ReportPage reportPage, String providedTableName, String firstRowString, Class headerDescription, int headersRowCount); + + /** + * Creates table without name. Table first and last row will be found by predicate. + * + * @param firstRowFinder table first row should contain cell satisfying predicate + * @param lastRowFinder table's last row should contain cell satisfying predicate + */ + default Table createNameless(ReportPage reportPage, + Predicate firstRowFinder, + Predicate lastRowFinder, + Class headerDescription) { + return createNameless(reportPage, "undefined", firstRowFinder, lastRowFinder, headerDescription, 1); + } + + /** + * Creates table without name. Table first row will be found by predicate, table ends by empty row + * or last row of report page. + * + * @param firstRowFinder table first row should contain cell satisfying predicate + */ + default Table createNameless(ReportPage reportPage, + Predicate firstRowFinder, + Class headerDescription) { + return createNameless(reportPage, "undefined", firstRowFinder, headerDescription, 1); + } + + /** + * Creates table with predefined name. Table first and last row will be found by predicate. + * + * @param providedTableName predefined (not existing in reportPage) table name + * @param firstRowFinder table first row should contain cell satisfying predicate + * @param lastRowFinder table's last row should contain cell satisfying predicate + */ + Table createNameless(ReportPage reportPage, + String providedTableName, + Predicate firstRowFinder, + Predicate lastRowFinder, + Class headerDescription, + int headersRowCount); + + /** + * Creates table with predefined name. Table first row will be found by predicate, table ends by empty row + * or last row of report page. + * + * @param providedTableName predefined (not existing in reportPage) table name + * @param firstRowFinder table first row should contain cell satisfying predicate + */ + Table createNameless(ReportPage reportPage, + String providedTableName, + Predicate firstRowFinder, + Class headerDescription, + int headersRowCount); } From f418b8a0c34ab533f8d0908dfc79919cad6f8586 Mon Sep 17 00:00:00 2001 From: Vitalii Ananev Date: Sat, 19 Feb 2022 20:07:59 +0300 Subject: [PATCH 06/10] refactor ReportPage --- .../table_wrapper/api/ReportPage.java | 50 +++++-------------- .../table_wrapper/api/ReportPageHelper.java | 30 +++++++++++ 2 files changed, 42 insertions(+), 38 deletions(-) create mode 100644 src/main/java/org/spacious_team/table_wrapper/api/ReportPageHelper.java diff --git a/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java b/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java index 713878c..042f24b 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java @@ -20,6 +20,8 @@ import java.util.function.Predicate; +import static org.spacious_team.table_wrapper.api.ReportPageHelper.getCellStringValueIgnoreCasePrefixPredicate; + public interface ReportPage { /** @@ -149,13 +151,9 @@ default TableCellAddress findByPrefix(String prefix, int startRow, int endRow) { * @return cell address or {@link TableCellAddress#NOT_FOUND} */ default TableCellAddress findByPrefix(String prefix, int startRow, int endRow, int startColumn, int endColumn) { - if (prefix != null) { - String lowercasePrefix = prefix.trim().toLowerCase(); - Predicate cellPredicate = (cell) -> (cell instanceof String) && - ((String) cell).trim().toLowerCase().startsWith(lowercasePrefix); - return find(startRow, endRow, startColumn, endColumn, cellPredicate); - } - return TableCellAddress.NOT_FOUND; + return prefix == null ? + TableCellAddress.NOT_FOUND : + find(startRow, endRow, startColumn, endColumn, getCellStringValueIgnoreCasePrefixPredicate(prefix)); } /** @@ -197,19 +195,10 @@ default TableCellRange getTableCellRange(String firstRowPrefix, int headersRowCo if (firstRowPrefix == null || lastRowPrefix == null) { return TableCellRange.EMPTY_RANGE; } - TableCellAddress startAddress = findByPrefix(firstRowPrefix); - if (startAddress.equals(TableCellAddress.NOT_FOUND)) { - return TableCellRange.EMPTY_RANGE; - } - TableCellAddress endAddress = findByPrefix(lastRowPrefix, startAddress.getRow() + headersRowCount + 1); - if (endAddress.equals(TableCellAddress.NOT_FOUND)) { - return TableCellRange.EMPTY_RANGE; - } - return new TableCellRange( - startAddress.getRow(), - endAddress.getRow(), - getRow(startAddress.getRow()).getFirstCellNum(), - getRow(endAddress.getRow()).getLastCellNum()); + return getTableCellRange( + getCellStringValueIgnoreCasePrefixPredicate(firstRowPrefix), + headersRowCount, + getCellStringValueIgnoreCasePrefixPredicate(lastRowPrefix)); } /** @@ -244,24 +233,9 @@ default TableCellRange getTableCellRange(String firstRowPrefix, int headersRowCo if (firstRowPrefix == null) { return TableCellRange.EMPTY_RANGE; } - TableCellAddress startAddress = findByPrefix(firstRowPrefix); - if (startAddress.equals(TableCellAddress.NOT_FOUND)) { - return TableCellRange.EMPTY_RANGE; - } - int lastRowNum = findEmptyRow(startAddress.getRow() + headersRowCount + 1); - if (lastRowNum == -1) { - lastRowNum = getLastRowNum(); // empty row not found - } else if (lastRowNum <= getLastRowNum()) { - lastRowNum--; // exclude last row from table - } - if (lastRowNum < startAddress.getRow()) { - lastRowNum = startAddress.getRow(); - } - return new TableCellRange( - startAddress.getRow(), - lastRowNum, - getRow(startAddress.getRow()).getFirstCellNum(), - getRow(lastRowNum).getLastCellNum()); + return getTableCellRange( + getCellStringValueIgnoreCasePrefixPredicate(firstRowPrefix), + headersRowCount); } /** diff --git a/src/main/java/org/spacious_team/table_wrapper/api/ReportPageHelper.java b/src/main/java/org/spacious_team/table_wrapper/api/ReportPageHelper.java new file mode 100644 index 0000000..772c854 --- /dev/null +++ b/src/main/java/org/spacious_team/table_wrapper/api/ReportPageHelper.java @@ -0,0 +1,30 @@ +/* + * Table Wrapper API + * Copyright (C) 2022 Vitalii Ananev + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package org.spacious_team.table_wrapper.api; + +import java.util.function.Predicate; + +class ReportPageHelper { + + static Predicate getCellStringValueIgnoreCasePrefixPredicate(String prefix) { + String lowercasePrefix = prefix.trim().toLowerCase(); + return (cell) -> (cell instanceof String) && + ((String) cell).trim().toLowerCase().startsWith(lowercasePrefix); + } +} From 7ff4a431fd086b455c1a53872b55ef3cb572445c Mon Sep 17 00:00:00 2001 From: Vitalii Ananev Date: Sat, 19 Feb 2022 20:34:13 +0300 Subject: [PATCH 07/10] refactor TableFactory --- .../table_wrapper/api/TableFactory.java | 139 +++++++++++++----- .../table_wrapper/api/TableRow.java | 1 + 2 files changed, 100 insertions(+), 40 deletions(-) diff --git a/src/main/java/org/spacious_team/table_wrapper/api/TableFactory.java b/src/main/java/org/spacious_team/table_wrapper/api/TableFactory.java index 90a3b09..db31d3c 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/TableFactory.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/TableFactory.java @@ -56,21 +56,33 @@ default Table create(ReportPage reportPage, * @param tableName table name's row should contain cell which starts with given text * @param lastRowString table's last row should contain cell which starts with given text */ - Table create(ReportPage reportPage, - String tableName, - String lastRowString, - Class headerDescription, - int headersRowCount); + default Table create(ReportPage reportPage, + String tableName, + String lastRowString, + Class headerDescription, + int headersRowCount) { + return create(reportPage, + tableName, + reportPage.getTableCellRange(tableName, headersRowCount, lastRowString), + headerDescription, + headersRowCount); + } /** * Creates table which starts with name followed by header and ends with empty row or last row of report page. * * @param tableName table name's row should contain cell which starts with given text */ - Table create(ReportPage reportPage, - String tableName, - Class headerDescription, - int headersRowCount); + default Table create(ReportPage reportPage, + String tableName, + Class headerDescription, + int headersRowCount) { + return create(reportPage, + tableName, + reportPage.getTableCellRange(tableName, headersRowCount), + headerDescription, + headersRowCount); + } /** * Creates table. Table name containing row and last row will be found by predicate. @@ -103,11 +115,17 @@ default Table create(ReportPage reportPage, * @param tableNameFinder table name containing row should contain cell satisfying predicate * @param lastRowFinder table's last row should contain cell satisfying predicate */ - Table create(ReportPage reportPage, - Predicate tableNameFinder, - Predicate lastRowFinder, - Class headerDescription, - int headersRowCount); + default Table create(ReportPage reportPage, + Predicate tableNameFinder, + Predicate lastRowFinder, + Class headerDescription, + int headersRowCount) { + return create(reportPage, + reportPage.getCell(reportPage.find(tableNameFinder)).getStringValue(), + reportPage.getTableCellRange(tableNameFinder, headersRowCount, lastRowFinder), + headerDescription, + headersRowCount); + } /** * Creates table. Table name containing row will be found by predicate, table ends by empty row @@ -115,10 +133,16 @@ Table create(ReportPage reportPage, * * @param tableNameFinder table name containing row should contain cell satisfying predicate */ - Table create(ReportPage reportPage, - Predicate tableNameFinder, - Class headerDescription, - int headersRowCount); + default Table create(ReportPage reportPage, + Predicate tableNameFinder, + Class headerDescription, + int headersRowCount) { + return create(reportPage, + reportPage.getCell(reportPage.find(tableNameFinder)).getStringValue(), + reportPage.getTableCellRange(tableNameFinder, headersRowCount), + headerDescription, + headersRowCount); + } /** * Creates table without name which starts with header and ends with row containing cell with text starting with @@ -153,12 +177,19 @@ default Table createNameless(ReportPage reportPage, * @param firstRowString table first row should contain cell which starts with given text * @param lastRowString table's last row should contain cell which starts with given text */ - Table createNameless(ReportPage reportPage, - String providedTableName, - String firstRowString, - String lastRowString, - Class headerDescription, - int headersRowCount); + default Table createNameless(ReportPage reportPage, + String providedTableName, + String firstRowString, + String lastRowString, + Class headerDescription, + int headersRowCount) { + return create(reportPage, + providedTableName, + reportPage.getTableCellRange(firstRowString, headersRowCount, lastRowString) + .addRowsToTop(1), // add fantom first line for provided table name + headerDescription, + headersRowCount); + } /** * Creates table with predefined name which starts with header and ends with empty row or last row of report page. @@ -166,11 +197,18 @@ Table createNameless(ReportPage reportPage, * @param providedTableName predefined (not existing in reportPage) table name * @param firstRowString table first row should contain cell which starts with given text */ - Table createNameless(ReportPage reportPage, - String providedTableName, - String firstRowString, - Class headerDescription, - int headersRowCount); + default Table createNameless(ReportPage reportPage, + String providedTableName, + String firstRowString, + Class headerDescription, + int headersRowCount) { + return create(reportPage, + providedTableName, + reportPage.getTableCellRange(firstRowString, headersRowCount) + .addRowsToTop(1), // add fantom first line for provided table name + headerDescription, + headersRowCount); + } /** * Creates table without name. Table first and last row will be found by predicate. @@ -204,12 +242,19 @@ default Table createNameless(ReportPage reportPage, * @param firstRowFinder table first row should contain cell satisfying predicate * @param lastRowFinder table's last row should contain cell satisfying predicate */ - Table createNameless(ReportPage reportPage, - String providedTableName, - Predicate firstRowFinder, - Predicate lastRowFinder, - Class headerDescription, - int headersRowCount); + default Table createNameless(ReportPage reportPage, + String providedTableName, + Predicate firstRowFinder, + Predicate lastRowFinder, + Class headerDescription, + int headersRowCount) { + return create(reportPage, + providedTableName, + reportPage.getTableCellRange(firstRowFinder, headersRowCount, lastRowFinder) + .addRowsToTop(1), // add fantom first line for provided table name + headerDescription, + headersRowCount); + } /** * Creates table with predefined name. Table first row will be found by predicate, table ends by empty row @@ -218,9 +263,23 @@ Table createNameless(ReportPage reportPage, * @param providedTableName predefined (not existing in reportPage) table name * @param firstRowFinder table first row should contain cell satisfying predicate */ - Table createNameless(ReportPage reportPage, - String providedTableName, - Predicate firstRowFinder, - Class headerDescription, - int headersRowCount); + default Table createNameless(ReportPage reportPage, + String providedTableName, + Predicate firstRowFinder, + Class headerDescription, + int headersRowCount) { + return create(reportPage, + providedTableName, + reportPage.getTableCellRange(firstRowFinder, headersRowCount) + .addRowsToTop(1), // add fantom first line for provided table name + headerDescription, + headersRowCount); + } + + + Table create(ReportPage reportPage, + String tableName, + TableCellRange tableRange, + Class headerDescription, + int headersRowCount); } diff --git a/src/main/java/org/spacious_team/table_wrapper/api/TableRow.java b/src/main/java/org/spacious_team/table_wrapper/api/TableRow.java index caea255..e50c724 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/TableRow.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/TableRow.java @@ -47,6 +47,7 @@ public interface TableRow extends ReportPageRow, Cloneable { * @throws RuntimeException if can't extract Double value */ double getDoubleCellValue(TableColumnDescription column); + /** * @throws RuntimeException if can't extract BigDecimal value */ From 3450f8af02a89b2880cfbcac3395ac2862bc7f93 Mon Sep 17 00:00:00 2001 From: Vitalii Ananev Date: Wed, 23 Feb 2022 22:56:12 +0300 Subject: [PATCH 08/10] fix NPE --- .../table_wrapper/api/ReportPage.java | 2 +- .../table_wrapper/api/TableFactory.java | 26 ++++++++++++------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java b/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java index 042f24b..16192fb 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/ReportPage.java @@ -267,7 +267,7 @@ default TableCellRange getTableCellRange(Predicate firstRowFinder, int h /** * Returns zero-based index of empty row. - * This implementation generates a huge amount of garbage. May be override for improve performance. + * This implementation generates a huge amount of garbage. May be overridden for improve performance. * * @param startRow first row for check * @return index of first empty row or -1 if not found diff --git a/src/main/java/org/spacious_team/table_wrapper/api/TableFactory.java b/src/main/java/org/spacious_team/table_wrapper/api/TableFactory.java index db31d3c..1286713 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/TableFactory.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/TableFactory.java @@ -120,11 +120,14 @@ default Table create(ReportPage reportPage, Predicate lastRowFinder, Class headerDescription, int headersRowCount) { - return create(reportPage, - reportPage.getCell(reportPage.find(tableNameFinder)).getStringValue(), - reportPage.getTableCellRange(tableNameFinder, headersRowCount, lastRowFinder), - headerDescription, - headersRowCount); + String tableName = ""; + TableCellRange tableCellRange = TableCellRange.EMPTY_RANGE; + TableCellAddress tableNameCell = reportPage.find(tableNameFinder); + if (!tableNameCell.equals(TableCellAddress.NOT_FOUND)) { + tableName = reportPage.getCell(tableNameCell).getStringValue(); + tableCellRange = reportPage.getTableCellRange(tableNameFinder, headersRowCount, lastRowFinder); + } + return create(reportPage, tableName, tableCellRange, headerDescription, headersRowCount); } /** @@ -137,11 +140,14 @@ default Table create(ReportPage reportPage, Predicate tableNameFinder, Class headerDescription, int headersRowCount) { - return create(reportPage, - reportPage.getCell(reportPage.find(tableNameFinder)).getStringValue(), - reportPage.getTableCellRange(tableNameFinder, headersRowCount), - headerDescription, - headersRowCount); + String tableName = ""; + TableCellRange tableCellRange = TableCellRange.EMPTY_RANGE; + TableCellAddress tableNameCell = reportPage.find(tableNameFinder); + if (!tableNameCell.equals(TableCellAddress.NOT_FOUND)) { + tableName = reportPage.getCell(tableNameCell).getStringValue(); + tableCellRange = reportPage.getTableCellRange(tableNameFinder, headersRowCount); + } + return create(reportPage, tableName, tableCellRange, headerDescription, headersRowCount); } /** From 3c66877d6037823a08fefee91a0350a18d75f399 Mon Sep 17 00:00:00 2001 From: Vitalii Ananev Date: Wed, 23 Feb 2022 23:05:53 +0300 Subject: [PATCH 09/10] exclude double report page scan for table range first row --- .../table_wrapper/api/TableFactory.java | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/spacious_team/table_wrapper/api/TableFactory.java b/src/main/java/org/spacious_team/table_wrapper/api/TableFactory.java index 1286713..d29790f 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/TableFactory.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/TableFactory.java @@ -121,13 +121,14 @@ default Table create(ReportPage reportPage, Class headerDescription, int headersRowCount) { String tableName = ""; - TableCellRange tableCellRange = TableCellRange.EMPTY_RANGE; - TableCellAddress tableNameCell = reportPage.find(tableNameFinder); - if (!tableNameCell.equals(TableCellAddress.NOT_FOUND)) { - tableName = reportPage.getCell(tableNameCell).getStringValue(); - tableCellRange = reportPage.getTableCellRange(tableNameFinder, headersRowCount, lastRowFinder); + TableCellRange range = reportPage.getTableCellRange(tableNameFinder, headersRowCount, lastRowFinder); + if (!range.equals(TableCellRange.EMPTY_RANGE)) { + TableCellAddress tableNameCell = + reportPage.find(range.getFirstRow(), range.getFirstRow() + 1, tableNameFinder); + tableName = tableNameCell.equals(TableCellAddress.NOT_FOUND) ? "" : + reportPage.getCell(tableNameCell).getStringValue(); } - return create(reportPage, tableName, tableCellRange, headerDescription, headersRowCount); + return create(reportPage, tableName, range, headerDescription, headersRowCount); } /** @@ -141,13 +142,14 @@ default Table create(ReportPage reportPage, Class headerDescription, int headersRowCount) { String tableName = ""; - TableCellRange tableCellRange = TableCellRange.EMPTY_RANGE; - TableCellAddress tableNameCell = reportPage.find(tableNameFinder); - if (!tableNameCell.equals(TableCellAddress.NOT_FOUND)) { - tableName = reportPage.getCell(tableNameCell).getStringValue(); - tableCellRange = reportPage.getTableCellRange(tableNameFinder, headersRowCount); + TableCellRange range = reportPage.getTableCellRange(tableNameFinder, headersRowCount); + if (!range.equals(TableCellRange.EMPTY_RANGE)) { + TableCellAddress tableNameCell = + reportPage.find(range.getFirstRow(), range.getFirstRow() + 1, tableNameFinder); + tableName = tableNameCell.equals(TableCellAddress.NOT_FOUND) ? "" : + reportPage.getCell(tableNameCell).getStringValue(); } - return create(reportPage, tableName, tableCellRange, headerDescription, headersRowCount); + return create(reportPage, tableName, range, headerDescription, headersRowCount); } /** From c1dcbd10b95d9c45ceba31ff4757d231f70d8188 Mon Sep 17 00:00:00 2001 From: Vitalii Ananev Date: Sun, 20 Mar 2022 20:25:54 +0300 Subject: [PATCH 10/10] update version to 2022.4 --- pom.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 7405ca6..5464af8 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,7 @@ org.spacious-team table-wrapper-api - 2022.2 + 2022.4 jar Table Wrapper API @@ -56,14 +56,14 @@ org.projectlombok lombok - 1.18.18 + 1.18.22 provided true org.slf4j slf4j-api - 1.7.30 + 1.7.36 provided